[英]Using package ' neuralnet ' in R to predict Weekly_Sales
[英]how to predict new cases using the neuralnet package
使用 RGUI。 我有一個名為 Data 的數據集。 我感興趣的響應變量包含在Data
的第一列中。
我有名為DataTrain
和DataTest
的Data
訓練集。
通過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
的幫助,我在 文檔的第 12 頁找到了一種稱為prediction
的方法。 我認為這根本不是我想要的,或者至少我不知道如何將它應用於我的Data
。
任何幫助將不勝感激(如果有任何解決方案)。
計算方法執行您所追求的操作,我從幫助文件中復制了此示例並添加了一些注釋:
# 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, as.data.frame((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.
預測的功能是prediction
,而不是predict
。
所以嘗試DataPred = prediction(DataNN, DataTest)
而不是DataPred = predict(DataNN, DataTest)
。
答案是計算(nn,測試)
您應該使用神經網絡版本的 predict ie
DataPred <- compute(DataNN, DataTest)
如果您使用 dplyr 進行任何操作,那么您需要專門聲明庫,然后是函數名稱,如下所示
DataPred <- neuralnet::compute(DataNN, DataTest)
順便說一句,在為變量賦值時從不使用等號,不幸的是,這是不好的做法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.