forked from rucliyang/Intro2ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap07-AI.R
115 lines (77 loc) · 2.93 KB
/
chap07-AI.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
ï»?
setwd("D:\\ai")
# 读入糖尿病数æ?
library(readxl)
d1 <- read_excel("diabetes.xlsx")
X <- d1[, names(d1) != "class"]
y <- factor(d1$class)
# 神ç»ç½‘络建模
library(RSNNS)
library(caret)
m1 <- train(X, y, method = "mlp", size = c(3, 2))
weightMatrix(m1$finalModel)
p1 <- predict(m1, newdata = X)
confusionMatrix(y, p1, positive = "pos")
prob1 <- predict(m1, newdata = X, type = "prob")$pos
library(pROC)
roc1 <- roc(y, prob1)
roc1
plot(roc1, print.auc = TRUE, print.thres = TRUE)
# 使用 neuralnet �
library(neuralnet)
m1 <- neuralnet(class~pregnant+glucose+pressure+triceps+insulin+mass+pedigree+age, d1, hidden=c(2, 2))
m1$result.matrix
plot(m1)
prob1 <- compute(m1, d1[, 1:8])$net.result[, 2]
library(pROC)
roc1 <- roc(y, prob1)
roc1
plot(roc1, print.auc = TRUE, print.thres = TRUE)
p1 <- factor(c("neg", "pos")[as.numeric(prob1 > 0.419) + 1])
confusionMatrix(y, p1, positive = "pos")
# MXNet 的基础æ“作
#cran <- getOption("repos")
#cran["dmlc"] <- "https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/CRAN/"
#options(repos = cran)
#install.packages("mxnet")
library(mxnet)
a <- mx.nd.zeros(c(2, 3))
a * 2 + 1
x = mx.nd.ones(12)
y <- mx.nd.array(0:11)
dim(x)
x1 <- mx.nd.reshape(x, c(3, 4))
y1 <- mx.nd.reshape(y, c(4, 3))
x1 * x1
mx.nd.dot(x1, y1)
mx.nd.concat(list(x1, x1), dim = 1)
mx.nd.concat(list(x1, x1), dim = 0)
mx.nd.slice.axis(y1, axis = 1, begin = 1, end = 3) #å‰é—åŽå¼€
x2 <- mx.nd.array(matrix(1:6, 2, 3))
class(x2)
x3 <- as.array(x2)
class(x3)
x4 <- mx.nd.ones(c(3, 4), ctx = mx.gpu())
x4
# 深度å¦ä¹ 建模
X1 <- as.matrix(X)
y1 <- as.numeric(y) - 1
#m1 <- mx.mlp(X1, y1, hidden_node= c(2, 3), out_node=2, out_activation="softmax", num.round=20, array.batch.size=15, learning.rate=0.1, momentum=0.9, eval.metric = mx.metric.accuracy)
data1 <- mx.symbol.Variable("data")
fc1 <- mx.symbol.FullyConnected(data = data1, num_hidden = 2, name = "fc1")
act1 <- mx.symbol.Activation(data = fc1, act_type = "tanh", name = "act1")
fc2 <- mx.symbol.FullyConnected(data = act1, num_hidden = 3, name = "fc2")
act2 <- mx.symbol.Activation(data = fc2, act_type = "tanh", name = "act2")
fc3 <- mx.symbol.FullyConnected(data = act2, num_hidden = 2, name = "fc3")
mlp <- mx.symbol.SoftmaxOutput(data = fc3, name = "softmax")
graph.viz(mlp, direction = "LR", type = "vis")
mx.set.seed(0)
m1 <- mx.model.FeedForward.create(mlp, X = X1, y = y1, ctx = mx.cpu(), num.round=200, array.batch.size=5, learning.rate=0.1, optimizer = "sgd", eval.metric = mx.metric.accuracy)
prob1 = predict(m1, X1)[2, ]
library(pROC)
roc1 <- roc(y1, prob1)
roc1
plot(roc1, print.auc = TRUE, print.thres = TRUE)
library(caret)
p1 <- factor(c("neg", "pos")[as.numeric(prob1 > 0.5) + 1])
confusionMatrix(y, p1, positive = "pos")