python - R 中的 XGBoost 预测器对所有行预测相同的值

标签 python r xgboost kaggle

我在 Python 中查看了关于相同内容的帖子,但我想要 R 中的解决方案。 我正在处理来自 Kaggle 的泰坦尼克号数据集,它看起来像这样:

    '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 = data.matrix(train)

主题是:

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会起作用,但事实并非如此。我做错了什么?

最佳答案

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

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

它应该可以解决你的问题。

关于python - R 中的 XGBoost 预测器对所有行预测相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38054887/

相关文章:

python - Numpy-y 从点集构建多项式数组的方法

python - 如何加速400万组路口?

python-3.x - XGBRegressor : change random_state no effect

python - XGBoost 提前停止给出 KeyError : 'best_msg'

python - 如何在不重新训练模型的情况下在 XGBoost 特征重要性图中获取实际特征名称?

python - 根据同一行中另一列的值填充缺失值

python - 在 Anaconda python 中配置根路径

r - 有什么解决方法可以找到基于 R 中的相关矩阵过滤原始特征的最佳阈值吗?

r - read.table() 中的一个字节分隔符参数

sql-server - Sql server 和 R、数据挖掘