![](/img/trans.png)
[英]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.