2011-02-03 71 views
7

使用RGUI。我有一個名爲Data的數據集。我感興趣的響應變量包含在Data的第一列中。如何使用神經網絡軟件包預測新病例

我有Data的訓練集叫做DataTrainDataTest

With DataTrain我使用軟件包和函數neuralnet訓練了一個神經網絡模型(稱爲DataNN)。

> DataNN = neuralnet(DataTrain[,1] ~ DataTrain[,2] + DataTrain[,3], hidden = 1, 
    data = DataTrain) 

有誰知道如何創建使用測試集(DataTest)這個模型的預測?

通常(對於其他型號),我會使用predict()。例如。

> DataPred = predict(DataNN, DataTest) 

但是這樣做對於neuralnet當我:

> DataPred = predict(DataNN, DataTest) 

Error in UseMethod("predict") : 
no applicable method for 'predict' applied to an object of class "nn" 

很明顯,我不能在這個模型上運行predict()。有誰知道任何替代品?

我檢查了neuralnet的幫助,並在documentation的第12頁找到了一個名爲prediction的方法。雖然我不認爲這是我想要的,或者至少我不知道如何將它應用於我的Data

任何幫助將不勝感激(如果有任何解決方案的話)。

回答

21

計算方法做了你是什麼之後,我複製從幫助文件這個例子並增加了一些意見:

# Make Some Training Data 
Var1 <- runif(50, 0, 100) 
# create a vector of 50 random values, min 0, max 100, uniformly distributed 
sqrt.data <- data.frame(Var1, Sqrt=sqrt(Var1)) 
# create a dataframe with two columns, with Var1 as the first column 
# and square root of Var1 as the second column 

# Train the neural net 
print(net.sqrt <- neuralnet(Sqrt~Var1, sqrt.data, hidden=10, threshold=0.01)) 
# train a neural net, try and predict the Sqrt values based on Var1 values 
# 10 hidden nodes 

# Compute or predict for test data, (1:10)^2 
compute(net.sqrt, (1:10)^2)$net.result 
# What the above is doing is using the neural net trained (net.sqrt), 
# if we have a vector of 1^2, 2^2, 3^2 ... 10 ^2 (i.e. 1, 4, 9, 16, 25 ... 100), 
# what would net.sqrt produce? 

Output: 
$net.result 
      [,1] 
[1,] 1.110635110 
[2,] 1.979895765 
[3,] 3.013604598 
[4,] 3.987401275 
[5,] 5.004621316 
[6,] 5.999245742 
[7,] 6.989198741 
[8,] 8.007833571 
[9,] 9.016971015 
[10,] 9.944642147 
# The first row corresponds to the square root of 1, second row is square root 
# of 2 and so on. . . So from that you can see that net.sqrt is actually 
# pretty close 
# Note: Your results may vary since the values of Var1 is generated randomly. 
+6

我在使用compute時遇到了這個錯誤:沒有適用於'compute'的方法應用於類「nn」的對象。這與dplyr中的計算方法有衝突。我通過調用像這樣來修復:neuralnet :: compute()。 – 2016-01-13 04:04:10

2

的功能預測prediction,不predict。因此請嘗試DataPred = prediction(DataNN, DataTest)而不是DataPred = predict(DataNN, DataTest)

1

答案是計算(NN,試)

1

你應該使用的地預測即

DataPred <- compute(DataNN, DataTest) 

如果您使用dplyr做任何操作,那麼你就需要將neuralnet的版本特別聲明庫,然後函數名像這樣

DataPred <- neuralnet::compute(DataNN, DataTest) 

BTW從來不使用等於變量賦值時簽署,不幸的是這是很糟糕的做法。