【问题标题】:how to predict new cases using the neuralnet package如何使用神经网络包预测新病例
【发布时间】:2011-06-20 23:38:13
【问题描述】:

使用 RGUI。我有一个名为 Data 的数据集。我感兴趣的响应变量包含在Data 的第一列中。

我有 Data 的训练集,称为 DataTrainDataTest

通过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

任何帮助将不胜感激(如果有任何解决方案的话)。

【问题讨论】:

    标签: r neural-network


    【解决方案1】:

    compute 方法可以满足您的需求,我从帮助文件中复制了这个示例并添加了一些 cmets:

     # 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.
    

    【讨论】:

    • 我在使用计算时遇到了这个错误:没有适用于“计算”的方法应用于“nn”类的对象。这与 dplyr 中的计算方法有冲突。我通过这样调用来修复:neuralnet::compute()。
    • 抱歉,在神经网络中我如何选择更好的预测器?
    • 此代码失败,Error in if (ncol(newdata) == length(object$model.list$variables)) { : argument is of length zero
    • @baxx: as.data.frame(...) 需要在1:10... 周围
    【解决方案2】:

    您应该使用神经网络的预测版本,即

    DataPred <- compute(DataNN, DataTest)
    

    如果您使用 dplyr 进行任何操作,那么您需要专门声明库,然后像这样声明函数名称

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

    顺便说一句,在为变量赋值时从不使用等号,不幸的是,这是不好的做法。

    【讨论】:

    • 你能解释一下为什么等号是不好的做法吗
    • 我使用的约定符合 Google 的 R 样式指南。但主要原因是
    【解决方案3】:

    答案是计算(nn, test)

    【讨论】:

      【解决方案4】:

      预测函数是prediction,而不是predict

      所以试试DataPred = prediction(DataNN, DataTest) 而不是DataPred = predict(DataNN, DataTest)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        • 2011-02-14
        • 2013-09-18
        • 1970-01-01
        • 2017-12-12
        • 2012-05-06
        • 2017-12-28
        相关资源
        最近更新 更多