【问题标题】:Difference in size of training sample size and test prediction训练样本大小和测试预测的大小差异
【发布时间】:2021-03-13 13:39:06
【问题描述】:

我已经在 2120x10 的样本大小上训练了一个模型。现在我正在尝试将相同的模型应用于测试数据集,但在推导混淆矩阵时遇到了麻烦。

test_predictions <- predict(train_obj, test_data)
test_predictions <- ifelse(test_predictions > 5, 1, 0)
confusionMatrix(as.factor(test_predictions), test_data$outcome, positive="1")

在计算混淆矩阵时出现错误,因为 test_data$outcome 有 2135 个值。如果我使用test_data$outcome[1:2120],一切正常。

有没有更好的方法来计算混淆矩阵而不限制值的数量。?限制test_data$outcome中的值个数是否正确?

【问题讨论】:

    标签: r prediction


    【解决方案1】:

    这听起来不对。如果test_data 只有 2120 行,test_data$outcome 怎么会有 2135 个值?即使test_data的预测器中有NA,它们也会被预测为NA,然后被confusionMatrix忽略。

    dat=data.frame(a=rnorm(1000), b=rnorm(1000))
    dat=dat %>% 
      mutate(c=5*(a+b)) %>%
      mutate(d=ifelse(c>5, 1, 0))
    set.seed(1)
    i=sample(1:1000, 750, replace=FALSE)
    train_data=dat[i,]
    test_data=dat[-i,]
    test_data[sample(1:250, 3),1:2]=NA
    lr=lm(c ~ a + b, data=train_data)
    test_predictions=predict(lr, test_data)
    test_predictions=ifelse(test_predictions>5, 1, 0)
    confusionMatrix(test_predictions, test_data$d)
    
              Reference
    Prediction   0   1
             0 187   0
             1   0  60
    

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2013-06-18
      • 2020-10-21
      • 2020-11-29
      • 2014-02-13
      • 2011-01-04
      • 2020-02-13
      • 2023-03-12
      • 2021-02-28
      相关资源
      最近更新 更多