【问题标题】:XGBoost predictor in R predicts the same value for all rows [duplicate]R中的XGBoost预测器为所有行预测相同的值[重复]
【发布时间】:2016-10-29 12:32:12
【问题描述】:

我在 Python 中查看了关于同一件事的帖子,但我想要 R 中的解决方案。 我正在研究来自 Kaggle 的 Titanic 数据集,它看起来像这样:

    'data.frame':   891 obs. of  13 variables:
 $ PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Survived   : num  0 1 1 1 0 0 0 0 1 1 ...
 $ Pclass     : Factor w/ 3 levels "1","2","3": 3 1 3 1 3 3 1 3 3 2 ...
 $ Age        : num  22 38 26 35 35 ...
 $ SibSp      : int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parch      : int  0 0 0 0 0 0 0 1 2 0 ...
 $ Fare       : num  7.25 71.28 7.92 53.1 8.05 ...
 $ Child      : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
 $ Embarked.C : num  0 1 0 0 0 0 0 0 0 1 ...
 $ Embarked.Q : num  0 0 0 0 0 1 0 0 0 0 ...
 $ Embarked.S : num  1 0 1 1 1 0 1 1 1 0 ...
 $ Sex.female : num  0 1 1 1 0 0 0 0 1 1 ...
 $ Sex.male   : num  1 0 0 0 1 1 1 1 0 0 ...

这是在我使用虚拟变量之后。我的测试集:

'data.frame':   418 obs. of  12 variables:
 $ PassengerId: int  892 893 894 895 896 897 898 899 900 901 ...
 $ Pclass     : Factor w/ 3 levels "1","2","3": 3 3 2 3 3 3 3 2 3 3 ...
 $ Age        : num  34.5 47 62 27 22 14 30 26 18 21 ...
 $ SibSp      : int  0 1 0 0 1 0 0 1 0 2 ...
 $ Parch      : int  0 0 0 0 1 0 0 1 0 0 ...
 $ Fare       : num  7.83 7 9.69 8.66 12.29 ...
 $ Child      : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
 $ Embarked.C : num  0 0 0 0 0 0 0 0 1 0 ...
 $ Embarked.Q : num  1 0 1 0 0 0 1 0 0 0 ...
 $ Embarked.S : num  0 1 0 1 1 1 0 1 0 1 ...
 $ Sex.female : num  0 1 0 0 1 0 1 0 1 0 ...
 $ Sex.male   : num  1 0 1 1 0 1 0 1 0 1 ...

我使用以下代码运行 xgboost:

> param <- list("objective" = "multi:softprob",
    +               "max.depth" = 25)
    > xgb = xgboost(param, data = trmat, label = y, nround = 7)
    [0] train-rmse:0.350336
    [1] train-rmse:0.245470
    [2] train-rmse:0.171994
    [3] train-rmse:0.120511
    [4] train-rmse:0.084439
    [5] train-rmse:0.059164
    [6] train-rmse:0.041455

trmat 是:

trmat = data.matrix(train)

而 temat 是:

temat = data.matrix(test)

而 y 是幸存的变量:

y = train$Survived

但是当我运行预测函数时:

> x = predict(xgb, newdata = temat)
> x[1:10]
 [1] 0.9584613 0.9584613 0.9584613 0.9584613 0.9584613 0.9584613 0.9584613
 [8] 0.9584613 0.9584613 0.9584613

所有概率都被预测为相同。在 python 问题中,有人说增加 max.depth 会起作用,但事实并非如此。我做错了什么?

【问题讨论】:

    标签: python r xgboost kaggle


    【解决方案1】:

    我可能回答迟了,我第一次使用 xgboost 时遇到了同样的问题。从火车组中删除“幸存”列应该可以解决您的问题。如果我们在训练集中有用于 xgboost 中标签的列,那么算法最终会预测所有概率都是相同的。

    【讨论】:

    • 感谢您的贡献,但这与 jlesuffleur 上面提供的答案有什么不同吗?
    • 该列应从训练集中删除。 Jlesuffleur 提到它是测试集,尽管她给出的代码是针对训练集的。
    【解决方案2】:

    您必须删除测试集中的 Survived 变量才能使用 xgboost,因为这是您要预测的变量。

    trmat = data.matrix(train[, colnames(train) != "Survived"])
    

    应该可以解决你的问题。

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 2020-12-02
      • 2018-12-23
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 2019-07-17
      • 2018-09-24
      相关资源
      最近更新 更多