1 RBF神经网络
在统计学章节里面,博主介绍了RBF神经网络的原理,通过把线性不可分的数据,通过径向基距离转化为线性可分数据,在使用径向基函数进行分类。
本章里面将使用R代码实现RBF网络
rm(list=ls())
install.packages("mlbench")
install.packages("RSNNS")
install.packages("mlbench")
library(mlbench)
library(RSNNS)
library(ROCR)
相关的包依赖
此次章节承接上一章节的代码
链接: link.
2 建立模型与预估
由于数据使用R自带的数据集,并进行过清洗转化,直接建模
#############################RBF neural network##################################
myrbf<- rbf(Sonar$inputsTrain, Sonar$targetsTrain, size=100,
maxit=1000,linOut=F)
0.5阈值估计,一般阈值根据业务设计
model predict
out<-predict(myrbf, Sonar$inputsTest)
out[out<0.5543928]=0
out[out>=0.5543928]=1
calculation accuracy
rate<-sum(out==Sonar$targetsTest)/length(Sonar$targetsTest)
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/6c9d266ceda043038eca5103072e0b2c.png)
# 3 绘制ROC曲线
方法与上一章节一致,绘制ROC曲线,找出auc值
Prediction probability
tr_rbf<-predict(myrbf,Sonar$inputsTrain)
te_rbf<-predict(myrbf,Sonar$inputsTest)
draw ROC curve
tr_pred<-prediction(tr_rbf,Sonar$targetsTrain)
tr_perf<-performance(tr_pred,"tpr","fpr")
te_pred<-prediction(te_rbf,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)
![4](https://ucc.alicdn.com/images/user-upload-01/260f7d1cfa7f4dd98febb66a83fa9ffd.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVEhFIE9SREVS,size_20,color_FFFFFF,t_70,g_se,x_16)
# 4 发现最佳阈值
library(colorspace)
library(pROC)
modelroc=roc(Sonar$targetsTest,te_rbf,plot=T)
plot(modelroc,print.auc=T,auc.polygon=T,grid=c(0.1,0.2),grid.col=c("green","red"),max.auc.polygon=T,auc.polygon.col="skyblue",print.thres=T)
modelauc=auc(modelroc)
modelauc
rocbest=coords(modelroc,"best")
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/1fddc251ef0f4bcc8227ca46ac5f9f7c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVEhFIE9SREVS,size_20,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/41fff1d87bd3409db59a1481ed984ddc.png)