R笔记-神经网络

神经网络

全称人工神经网络,即Artificial Neural Networks,简记为ANN.它是由大量类似于生物神经元的处理单元相互连接而成的非线性复杂网络系统.(维基百科英文解释,维基百科中文解释)
R中神经网络包有AMORE,NNET,Neuralnet,RSNNS.本文使用neuralnet.

neuralnet

https://github.com/cran/neuralnet

需求

1
2
3
require(neuralnet)  # for neuralnet()
# require(nnet) # for class.ind() 将分裂转为数值,比如1,0
require(caret) # for train() ,可用来寻求最佳隐层组合,不过运行时间较长.

其中,caret帮助文档见https://topepo.github.io/caret/available-models.html.

R code

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
normalize <- function(x) {
return ((x-min(x))/(max(x)-min(x)))
}

neufun_f <- function() {
datatr <- read.csv("/home/lm/projects/RProjects/train_f.csv", head = FALSE)
datate <- read.csv("/home/lm/projects/RProjects/test_f.csv", head = FALSE)
datatr.target <- datatr[,1]
datate.target <- datate[,1]
datatr[,1] <- ifelse(datatr[,1]=='Y',1,0) # 将分类Y,N转为1,0
datate[,1] <- ifelse(datate[,1]=='Y',1,0)
datatr[2:96] <- lapply(datatr[2:96],normalize) # 标准化
datate[2:96] <- lapply(datate[2:96],normalize)

library(neuralnet)
set.seed(5)
formula <- as.formula(paste("V1 ~",paste(setdiff(names(datatr), "V1"),collapse = "+"),sep=""))
#datatr <- as.matrix(datatr)
#datate <- as.matrix(datate)
neu.model <- neuralnet(formula=formula,data=datatr,threshold=1e-8,hidden=c(80,50,25,15),
stepmax=1e+08,err.fct="ce",linear.output=FALSE,lifesign = "full",
algorithm = 'rprop+')
# plot(neu.model)
neu.com <- neuralnet::compute(neu.model,datate[,2:96])
neu.predicted <- ifelse(neu.com$net.result>0.5,"Y","N") # 将数值转为标签
# datate[,1] <- ifelse(datate[,1]=="1","Y","N")

#neu.table <- table(datate.target,neu.predicted,dnn=c("真实值","预测值"))
neu.table <- table(datate.target,neu.predicted,dnn=c("True values","Predicted values"))
library(caret)
neu.cfMatrix <- caret::confusionMatrix(neu.table,positive="Y")
neu.cfMatrix

neu.TP <- neu.table[2,2]
neu.FN <- neu.table[2,1]
neu.FP <- neu.table[1,2]
neu.TN <- neu.table[1,1]

e.ACC <- (neu.TP+neu.TN)/(neu.TP+neu.TN+neu.FP+neu.FN)
e.SN <- neu.TP/(neu.TP+neu.FN)
e.SP <- neu.TN/(neu.TN+neu.FP)
e.PPV <- neu.TP/(neu.TP+neu.FP)
e.FRP <- neu.FP/(neu.FP+neu.TN)
# Kappa
pe <- ((neu.TP+neu.FN)*(neu.TP+neu.FP)+(neu.FP+neu.TN)*(neu.FN+neu.TN))/(neu.TP+neu.FN+neu.FP+neu.TN)^2
e.Kappa <- (e.ACC-pe)/(1-pe)

library(ROCR)
neu.pred <- ROCR::prediction(predictions = neu.com$net.result, datate[,1])
neu.perf <- ROCR::performance(neu.pred, measure="tpr",x.measure = "fpr")
# plot(neu.perf,col="blue",add=T)
#plot(neu.perf,col="blue",xlab="假正率",ylab="真正率",main="ROC曲线")
plot(neu.perf,col="blue",main="ROC Plot",xlab="False Positive Rate(1-Specificity)",ylab="True Positive Rate(Sensitivity)")
neu.auc <- ROCR::performance(neu.pred,"auc")@y.values[[1]]
neu.auc
legend(x=0,legend=c(paste("AUC=",neu.auc)),xjust = 1,yjust=-0.2)
}

参考