其他分享
首页 > 其他分享> > R语言 神经网络neuralnet和nnet

R语言 神经网络neuralnet和nnet

作者:互联网

关注微信公共号:小程在线

关注CSDN博客:程志伟的博客

neuralnet():建立B-P网络

gwplot函数:神经网络变量重要性的可视化图形

compute函数:利用神经网络进行预测

nnet函数:建立B-P网络

> setwd('G:\\R语言\\大三下半年\\数据挖掘:R语言实战\\')
> library("neuralnet")
> BuyOrNot<-read.table("G:\\R语言\\大三下半年\\R语言数据挖掘方法及应用\\消费决策数据.txt",header=TRUE)
> head(BuyOrNot)
  Purchase Age Gender Income
1        0  41      2      1
2        0  47      2      1
3        1  41      2      1
4        1  39      2      1
5        0  32      2      1
6        0  32      2      1
> ##########neurealnet建立神经网络
> set.seed(12345)
> BPnet1<-neuralnet(Purchase~Age+Gender+Income,data=BuyOrNot,hidden=2,err.fct="ce",linear.output=FALSE)
> #连接权重及其它信息
> #一个隐层,两个隐节点。迭代15076次(steps)
> BPnet1$result.matrix
                               [,1]
error                  2.707708e+02
reached.threshold      9.283383e-03
steps                  1.507600e+04
Intercept.to.1layhid1  8.516222e+01
Age.to.1layhid1        8.915386e-01
Gender.to.1layhid1    -4.529214e+01
Income.to.1layhid1    -2.988069e+01
Intercept.to.1layhid2  2.275544e+00
Age.to.1layhid2       -3.665990e+00
Gender.to.1layhid2     2.227952e+01
Income.to.1layhid2     1.073472e+01
Intercept.to.Purchase -1.298572e-01
1layhid1.to.Purchase  -1.195226e+00
1layhid2.to.Purchase   8.741901e+02


> #连接权重列表
> BPnet1$weight
[[1]]
[[1]][[1]]
            [,1]      [,2]
[1,]  85.1622178  2.275544
[2,]   0.8915386 -3.665990
[3,] -45.2921436 22.279522
[4,] -29.8806883 10.734723

[[1]][[2]]
            [,1]
[1,]  -0.1298572
[2,]  -1.1952260
[3,] 874.1900845

 


> ########权值参数可视化
> plot(BPnet1)

 #######输入变量重要性及可视化

#可以看出年龄的权重几乎为0,变量的重要性很弱
> head(BPnet1$generalized.weights[[1]])
             [,1]        [,2]       [,3]
[1,] -0.184425690  9.36923561 6.18118700
[2,] -0.001446301  0.07347532 0.04847404
[3,] -0.184425690  9.36923561 6.18118700
[4,] -0.248354346 12.61695365 8.32381137
[5,] -0.001215887  0.06176978 0.04075152
[6,] -0.001215887  0.06176978 0.04075152
> par(mfrow=c(2,2))
> gwplot(BPnet1,selected.covariate="Age")
> gwplot(BPnet1,selected.covariate="Gender")
> gwplot(BPnet1,selected.covariate="Income")


 

> ##########不同输入变量水平组合下的预测
> newData<-matrix(c(39,1,1,39,1,2,39,1,3,39,2,1,39,2,2,39,2,3),nrow=6,ncol=3,byrow=TRUE)
> new.output<-compute(BPnet1,covariate=newData)
> new.output$net.result
          [,1]
[1,] 0.2099738
[2,] 0.2099739
[3,] 0.4675811
[4,] 0.3607890
[5,] 0.4675812
[6,] 0.4675812

可以看出女性比男性的购买率高;高收入购买率高;
> ############确定概率分割值

> library("ROCR")
> detach("package:neuralnet")
> summary(BPnet1$net.result[[1]])
       V1        
 Min.   :0.2100  
 1st Qu.:0.2100  
 Median :0.4676  
 Mean   :0.3759  
 3rd Qu.:0.4676  
 Max.   :0.9867  
> pred<-prediction(predictions=as.vector(BPnet1$net.result),labels=BPnet1$response)
> par(mfrow=c(2,1))
> perf<-performance(pred,measure="tpr",x.measure="fpr")
> plot(perf,colorize=TRUE,print.cutoffs.at=c(0.2,0.45,0.46,0.47))
> perf<-performance(pred,measure="acc")
> plot(perf)


> Out<-cbind(BPnet1$response,BPnet1$net.result[[1]])
> Out<-cbind(Out,ifelse(Out[,2]>0.468,1,0))
> (ConfM.BP<-table(Out[,1],Out[,3]))
   
      0   1
  0 268   1
  1 161   1
> (Err.BP<-(sum(ConfM.BP)-sum(diag(ConfM.BP)))/sum(ConfM.BP))
[1] 0.3758701
> library("nnet")
> set.seed(1000)
> (BPnet2<-nnet(Purchase~Age+Gender+Income,data=BuyOrNot,size=2,entropy=TRUE,abstol=0.01))
# weights:  11
initial  value 287.993985 
final  value 285.324638 
converged
a 3-2-1 network with 11 weights
inputs: Age Gender Income 
output(s): Purchase 
options were - entropy fitting 
> predict(BPnet2,BuyOrNot,type="class")
  
> set.seed(1000)

> library("neuralnet")
> BPnet3<-neuralnet(Purchase~Age+Gender+Income,data=BuyOrNot,
+   algorithm="backprop",learningrate=0.01,hidden=2,err.fct="ce",linear.output=FALSE)

 

标签:Purchase,01,Age,nnet,BPnet1,to.1,神经网络,Income,neuralnet
来源: https://blog.csdn.net/c1z2w3456789/article/details/104671852