RSNNS包 BP神经网络
【摘要】 RSNNS包 BP神经网络
1 BP神经网络
################BP network and RBF network of rsnns package###############################
##-----------------------BP neural network----------------------------------------
清除环境
rm(list=ls())
install.packages("mlbench")
install.packages("RSNNS")
install.packages("mlbench")
library(mlbench)
library(RSNNS)
library(ROCR)
data(Sonar)
2 打乱顺序并且确定x与y
##random simpling
Sonar<-Sonar[sample(1:nrow(Sonar),nrow(Sonar)),]
##Define input and output
SonarValues<-Sonar[,1:60]
SonarTargets<-as.numeric(Sonar[,61])-1
把字符类型的y转变为数值型变量
##The data is divided into training set and test set
Sonar<-splitForTrainingAndTest(SonarValues,SonarTargets,ratio=0.3)
##Standardization
Sonar<-normTrainingAndTestSet(Sonar)
#Sonar$
使用splitForTrainAndTest去划分训练集和测试集
3 建立模型,初始阈值设置为0.5
##多层感知器训练:mlp()
#Multilayer perceptron training
mymlp<-mlp(Sonar$inputsTrain,Sonar$targetsTrain,size= c(4,2),
learnFuncParams=0.2,maxit=500)
##model predict
out<-predict(mymlp, Sonar$inputsTest)
out[out<0.5]=0
out[out>=0.5]=1
##calculation accuracy
rate<-sum(out==Sonar$targetsTest)/length(Sonar$targetsTest)
4 绘制ROC曲线
##Predict training and testing respectively
tr_mlp<-predict(mymlp,Sonar$inputsTrain)
te_mlp<-predict(mymlp,Sonar$inputsTest)
##Draw ROC curve
tr_pred<-prediction(tr_mlp,Sonar$targetsTrain)
tr_perf<-performance(tr_pred,"tpr","fpr")
te_pred<-prediction(te_mlp,Sonar$targetsTest)
te_perf<-performance(te_pred,"tpr","fpr")
plot(tr_perf,col='green',main="ROC of Models")
plot(te_perf, col='black',lty=2,add=TRUE);
abline(0,1,lty=2,col='red')
tr_auc<-round(as.numeric(performance(tr_pred,'auc')@y.values),3)
tr_str<-paste("Train-AUC:",tr_auc,sep="")
legend(0.3,0.45,c(tr_str),2:8)
te_auc<-round(as.numeric(performance(te_pred,'auc')@y.values),3)
te_ste<-paste("Test-AUC:",te_auc,sep="")
legend(0.3,0.25,c(te_ste),2:8)
可以发现训练集过拟合,测试集最佳的阈值准确率是0.907
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)