81RSNNSパッケージBPニューラルネットワーク

1BPニューラルネットワーク

################rsnnsパッケージのBPネットワークとRBFネットワーク########################## ##########

##-----------------------BPニューラルネットワーク---------------------- ------------------
クリアな環境

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)

ここに画像の説明を挿入

4ROC曲線をプロットします

##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であることがわかります。

おすすめ

転載: blog.csdn.net/weixin_44498127/article/details/124231297