R言語ニューラルネットワークneuralnetとNNET

いいえマイクロチャネル世間の注目ません:オンライン小さな道

ウォッチCSDNブログ:スティーブンCHINGのブログを

neuralnet():BPネットワークを構築

ニューラルネットワークのビジュアルグラフィック変数の重要度:gwplot機能

計算機能:予測するニューラルネットワークを使用して

NNET機能:BPネットワークを確立するために、

> Setwd( 'G:\\ R言語\\ \\半分ジュニアデータマイニング:R実用的言語\\')
>ライブラリ( "neuralnet")
> BuyOrNot <-read.table(「G:\\ R&LT言語\\ \\ R&後半にアプリケーションおよびデータをマイニングLTジュニア言語データ\\消費決定が」.txtで、ヘッダ= TRUE)
>ヘッド(BuyOrNot)
  購入年齢性別所得
。1 41 2 1 0
2 47 0 2 1
3 1 41は。1 2
。4 39 2. 1. 1
。5 1 0 32 2
。6 0 32 2 1
>ニューラルネットワークの構築########## neurealnet
> set.seed(12345)
> BPnet1 <-neuralnet(購入〜+年齢性別収入+、データ= BuyOrNot、隠れ= 2、err.fct = "CE"、linear.output = FALSE)
>#接続重み、およびその他の情報
>#隠れ層、2つの隠しノード。15076の反復回数(ステップ)
> $ BPnet1結果。

エラー2.707708e + 02
reached.threshold 9.283383e-03
のステップ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 $重み
[[1]]
[[1]] [[1]]
            [1] [2]
[1] 85.1622178 2.275544
[2,1] 0.8915386 -3.665990
[3] - 45.2921436 22.279522
[4] -29.8806883 10.734723

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

 


> ########重みパラメータの可視化
>プロット(BPnet1)

 #######入力変数と可視化の重要性

#重み付けは年齢が、弱い変数の重要度はほぼゼロであることがわかる
ヘッド(BPnet1 $のgeneralized.weights [[1]])>
             [1] [2] [3]
[1] -0.184425690 9.36923561 6.18118700
[2,1] 0.07347532 -0.001446301 0.04847404
[3] -0.184425690 9.36923561 6.18118700
[4] 12.61695365 -0.248354346 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 = "年齢")
> gwplot(BPnet1、selected.covariate = "性別")
> gwplot(BPnet1、selected.covariate = "利益")


 

> ##########不同输入变量水平组合下的预测
> 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)

 

发布了103 篇原创文章 · 获赞 111 · 访问量 38万+

おすすめ

転載: blog.csdn.net/c1z2w3456789/article/details/104671852