我是靠谱客的博主 安详大侠,这篇文章主要介绍81 RSNNS包 BP神经网络1 BP神经网络2 打乱顺序并且确定x与y3 建立模型,初始阈值设置为0.54 绘制ROC曲线,现在分享给大家,希望可以做个参考。

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

最后

以上就是安详大侠最近收集整理的关于81 RSNNS包 BP神经网络1 BP神经网络2 打乱顺序并且确定x与y3 建立模型,初始阈值设置为0.54 绘制ROC曲线的全部内容,更多相关81内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(142)

评论列表共有 0 条评论

立即
投稿
返回
顶部