【问题标题】:error in roc: Neural Network in Rroc 中的错误:R 中的神经网络
【发布时间】:2016-11-05 05:54:12
【问题描述】:

我想在 R 中使用神经网络来预测汽车的价格,它有 144 个自变量。 在我的代码下面。一切正常,除了最后 2 行:AUC 和绘图。

这是我得到的错误:

roc(predNN, yTEST) 中的错误: 没有足够的不同预测来计算 ROC 曲线下的面积。

我已经计算了因变量作为一个因素,但这个错误仍然存​​在。 我应该如何解决这个问题?

 allind <- sample(x=1:nrow(data_price2),size=nrow(data_price2))

 trainind <- allind[1:round(length(allind)/3)]
 valind <- allind[(round(length(allind)/3)+1):round(length(allind)*(2/3))]
 testind <- allind[round(length(allind)*(2/3)+1):length(allind)]

 BasetableTRAIN <- data_price2[trainind,]
 BasetableVAL <- data_price2[valind,]
 Basetablebig <-rbind(BasetableTRAIN,BasetableVAL)
 BasetableTEST <- data_price2[testind,]

 #Create a separate response variable
 yTRAIN <- BasetableTRAIN$Price
 BasetableTRAIN$Price <- NULL

 yVAL <- BasetableVAL$Price
 BasetableVAL$Price <- NULL

 yTEST <- BasetableTEST$Price
 BasetableTEST$Price <- NULL

 yBIG <- Basetablebig$Price
 Basetablebig$Price <- NULL

 yTRAIN <- as.factor(yTRAIN)
 yVAL <- as.factor(yVAL)
 yTEST <- as.factor(yTEST)
 yBIG <- as.factor(yBIG)

 if (require("nnet")==FALSE) install.packages("nnet") ; library(nnet)
 if (require("AUC")==FALSE) install.packages("AUC") ; library(AUC)

 size <- 5 #number of units in the hidden layer
 decay <- 0.1 #weight decay. Same as lambda in regularized LR. Controls for
               overfitting. 
 rang <- 0.5 #the range of the initial random weights parameter
 maxit <- 2000 #set high in order not to run into early stopping 

 NN <- nnet(yBIG ~ ., Basetablebig, size = size, 
       rang = rang, decay = decay, maxit = maxit,MaxNWts= Inf)

 predNN <- as.numeric(predict(NN,BasetableTEST,type="raw"))
 AUC::auc(roc(predNN,yTEST))
 plot(roc(predNN,yTEST))

【问题讨论】:

    标签: r neural-network roc


    【解决方案1】:

    您很可能会遇到与不良模型相关的问题。查看模型的预测。根据概率阈值 0.5,您可能会得到全 0 或 1。神经网络技术非常容易受到不同列之间的尺度差异的影响,因此数据标准化 [mean =0 std =1] 是一种很好的做法。我建议您为此使用 R 函数 scale()。请提供数据以重现您的问题。

    【讨论】:

    • 其他人赞成这个答案。我尝试缩放基表,但是在运行神经网络时会发生此错误: if (nconn[i + 1] == ns) cadd
    • 在所有这些代码之前执行scale(data_price2) 就是这样。我不知道你是怎么得到你提到的错误的。
    • 我在代码开头使用 scale(data_price2) 进行了缩放。但仍然出现此错误: roc(predNN, yTEST) 中的错误:没有足够的不同预测来计算 ROC 曲线下的面积。
    • 长度(yTEST)= 554
    • 从图中看起来你的所有预测都低于 0.5。这是真的?我的意思是如果length(unique(round(yTEST))) 是1?如果是这样,那么阈值 = 0.5 的简单舍入将产生所有分类预测类 0。这就是错误的全部意义。
    猜你喜欢
    • 2018-05-30
    • 1970-01-01
    • 2015-09-10
    • 2017-11-20
    • 2013-11-20
    • 2016-11-22
    • 1970-01-01
    • 2018-11-01
    • 2021-03-11
    相关资源
    最近更新 更多