python-3.x - 为什么 xgboost 在使用整个数据集时对特征产生相同的预测和 nan 值?

标签 python-3.x xgboost boosting

总结

我使用的是 Python v3.7 和 xgboost v0.81。从 2015 年到 2019 年,我每周都有美国州级的连续数据 (y)。我正在尝试将以下特征回归到 y:年、月、周、地区(编码)。我已将火车设置为 2018 年 8 月及之前,测试为 2018 年 9 月及以后。当我以这种方式训练模型时,会发生两件奇怪的事情:

  • feature_importances 都是 nan
  • 预测都是一样的(0.5,0.5....)

我尝试过的

将任何特征固定到单个变量可以让模型进行适当的训练,并且之前遇到的两个奇怪问题都消失了。前任。年份==2017 或地区==28

代码

(我知道这是一个时间问题,但这个一般情况也显示了这个问题)

X = df[['year', 'month', 'week', 'region_encoded']]
display(X)
y = df.target
display(y)
X_train, X_test, y_train, y_test = train_test_split(X.values, y.values, test_size=0.1)

model = XGBRegressor(n_jobs=-1, n_estimators=1000).fit(X_train, y_train)

display(model.predict(X_test)[:20])

display(model.feature_importances_)

结果——一些预测和特征重要性

year    month   week    region_encoded
0   2015    10  40  0
1   2015    10  40  1
2   2015    10  40  2
3   2015    10  40  3
4   2015    10  40  4

0    272.0
1     10.0
2    290.0
3     46.0
4    558.0
Name: target, dtype: float64

array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], dtype=float32)

array([nan, nan, nan, nan], dtype=float32)

最佳答案

如果目标变量中有 NaN,即使只有一个,也足以让许多机器学习算法崩溃。这通常是因为在许多 ML 算法(例如计算导数)的更新步骤中,当目标变量中存在未处理的 NaN 时,NaN 会传播。不过,关于 XGBoost 中的哪一步执行此操作,我不能说太多。

例如,线性回归的解析解。

import numpy as np
import numpy.linalg as la
from scipy import stats

y = np.array([0, 1, 2, 3, np.nan, 5, 6, 7, 8, 9])
x = stats.norm().rvs((len(y), 3))

# Main effects estimate
m_hat = la.inv(x.T @ x) @ x.T @ y
>>> [nan nan nan]

关于python-3.x - 为什么 xgboost 在使用整个数据集时对特征产生相同的预测和 nan 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56095719/

相关文章:

python - RandomizedSearchCV 和 XGBoost + 提前停止

machine-learning - xgboost 多类工作中 base_score 的用途是什么?

r - 如何在 gbm 函数中打印变量重要性?

python 调用类方法而不初始化类

python - 每 64 个字符插入换行符

python - XGBoost的多类别分类是如何工作的?

python - XGBoost特征重要性: How do I get original variable names after encoding

Python 子进程无法从 Bat 文件获取环境变量

python-3.x - Mac PyQt5 菜单栏在取消聚焦 - 重新聚焦应用程序之前不处于事件状态

r - R : what is the tolerance for xgb. cv 的 early_stopping_rounds 中的 xgboost?