【问题标题】:R Neural Network Package - what does net.result show?R 神经网络包 - net.result 显示什么?
【发布时间】:2023-03-18 02:19:01
【问题描述】:

使用以下代码我构建了一个神经网络模型来预测收盘价:

library(neuralnet)
myformula <- close ~ High+Low+Open 
nn_close <- neuralnet(myformula,data=train_,hidden=c(5,3),linear.output=T)
nn_close$net.result[[1]]

有人可以向我解释nn_close$net.result[[1]] 行的作用吗?我已经检查了CRAN documentation,但这对我来说仍然不清楚。

【问题讨论】:

    标签: r machine-learning statistics neural-network


    【解决方案1】:

    通过一个例子你会更好地理解这一点(我稍微修改了一下,但我从here得到它):

    itrain <- iris[sample(1:150, 50),]
    
    itrain$setosa <- c(itrain$Species == 'setosa')
    
    itrain$versicolor <- c(itrain$Species == 'versicolor')
    
    itrain$virginica <- c(itrain$Species == 'virginica')
    
    itrain$Species <- NULL
    
    inet <- neuralnet(setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width + 
                      Petal.Length + Petal.Width, itrain, hidden=3, lifesign="full")
    
    #make a prediction on the training set and then compare to 
    #inet$net.result[[1]]
    predict <- compute(inet, itrain[1:4])
    

    现在看看结果:

    head(predict$net.result)
    #                [,1]            [,2]            [,3]
    #80   0.0167232688257  0.995316738272 -0.011840391533
    #112 -0.0008289388986 -0.006814451178  1.007637170495
    #17   1.0028534166840  0.004240124926 -0.007115290101
    #104 -0.0002256650283 -0.031771967776  1.031855488316
    #149  0.0019424886784  0.007205356060  0.990892583485
    #82  -0.0061699713404  0.957656929739  0.048564910023
    head(inet$net.result[[1]])
    #                [,1]            [,2]            [,3]
    #80   0.0167232688257  0.995316738272 -0.011840391533
    #112 -0.0008289388986 -0.006814451178  1.007637170495
    #17   1.0028534166840  0.004240124926 -0.007115290101
    #104 -0.0002256650283 -0.031771967776  1.031855488316
    #149  0.0019424886784  0.007205356060  0.990892583485
    #82  -0.0061699713404  0.957656929739  0.048564910023
    

    我使用compute 使用神经网络模型对训练集进行预测。

    如您所见,inet$net.result[[1]]predict$net.result 是相同的。因此,inet$net.result[[1]] 只是对您用于训练模型的数据集的预测(它与 lm 模型的 fit.values 相同)。

    根据@sebastianmm 的真正有用的评论,net.result 是一个列表是有原因的。基本上,neuralnet 有一个参数rep,它可以在一次调用中训练多个模型。当rep 大于 1 时,net.result 将大于 1(其他组件(weightsresult.matrixstart.weights)也是如此)。

    【讨论】:

    • 非常感谢。奇怪的是 inet$net.result[[1]] 的结果是一个包含 1 个元素的列表。这是否表明在某些情况下可能有超过 1 个结果集,例如 inet$net.result[[2]]?
    • 非常欢迎您@edb500。老实说,我从未使用过inet$net.result[[2]] 或任何其他元素,所以我不确定。
    • @LyzanderR neuralnet 有一个参数rep,这使得一次调用训练多个模型成为可能。当rep 大于 1 时,net.result 将大于 1(其他组件(weightsresult.matrixstart.weights)也是如此)。
    • 谢谢@sebastianmm。这真的很有用。正如 OP 所怀疑的那样,net.result 的设计是有原因的。我会将其添加到答案中。非常感谢!
    【解决方案2】:

    compute()$net.result 的结果仅包含一个级别,这给出了每个样本成为给定物种的概率(在本例中)。换句话说,行的总和(大致)等于 1。在以下示例中,我使用此信息来预测数据验证子集中的物种,并使用 @987654322 将它们与它们的真实值进行比较@:

    # install.packages("neuralnet")
    library(neuralnet)
    
    # adapted iris
    data(iris)
    iris2 <- iris
    iris2$setosa <- c(iris2$Species == 'setosa')
    iris2$versicolor <- c(iris2$Species == 'versicolor')
    iris2$virginica <- c(iris2$Species == 'virginica')
    # iris2$Species <- NULL
    
    # training and validation subsets
    train.samples <- sample(nrow(iris), nrow(iris)*0.5)
    train <- iris2[train.samples,]
    valid <- iris2[-train.samples,]
    
    # fit model
    inet <- neuralnet(setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width + 
      Petal.Length + Petal.Width, train, hidden=3, lifesign="full")
    
    # prediction 
    pred <- compute(inet, valid[,1:4])
    head(pred$net.result) # only one level (probability of each category)
    predspp <- factor(c("setosa" , "versicolor", "virginica"))[apply(pred$net.result, MARGIN=1, FUN=which.max)]
    table(predspp, valid$Species)
    # predspp      setosa versicolor virginica
    #   setosa         19          0         0
    #   versicolor      0         24         4
    #   virginica       0          2        26
    

    在我的例子中,所有 setosa 样本都被正确预测。 versicolor 和 virginica 分别有 2 个和 4 个错误预测。一般来说,92% 的验证样本 (69/75 * 100) 的预测是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 2013-09-18
      相关资源
      最近更新 更多