【问题标题】:Why do I get KeyError: 'Target_Variable' when I perform Mean Squared Error for XGBoost?当我为 XGBoost 执行均方误差时,为什么会出现 KeyError: 'Target_Variable'?
【发布时间】:2019-09-01 10:59:33
【问题描述】:

我正在对我的航班延误数据集执行 XGBoost。我执行并训练了数据集,但是当我试图找到均方误差测试时,我得到了上述错误。

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df_late.drop(['DELAY_YN','ARR_DELAY'],axis=1), 
                                                    df_late['ARR_DELAY'], test_size=0.30, random_state=101)

print('Training...')
xg_reg = xgb.XGBRegressor(n_estimators= 2000, max_depth= 5,learning_rate =0.1)
xg_reg.fit(X_train,y_train)

print('Predicting on test set...')
predictions = xg_reg.predict(X_test)

y_test['predicted']=[np.exp(p) for p in predictions]

from sklearn import metrics
print('MAE:', metrics.mean_absolute_error(y_test['ARR_DELAY'],y_test['predicted']))
print('MSE:', metrics.mean_squared_error(y_test['ARR_DELAY'],y_test['predicted']))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test['ARR_DELAY'],y_test['predicted'])))

我收到以下错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-141-b9b5e43dd55b> in <module>
      2 
      3 from sklearn import metrics
----> 4 print('MAE:', metrics.mean_absolute_error(y_test['ARR_DELAY'],y_test['predicted']))
      5 print('MSE:', metrics.mean_squared_error(y_test['ARR_DELAY'],y_test['predicted']))
      6 print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test['ARR_DELAY'],y_test['predicted'])))

~/anaconda3/envs/myenv/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    866         key = com.apply_if_callable(key, self)
    867         try:
--> 868             result = self.index.get_value(self, key)
    869 
    870             if not is_scalar(result):

~/anaconda3/envs/myenv/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4318         try:
   4319             return self._engine.get_value(s, k,
-> 4320                                           tz=getattr(series.dtype, 'tz', None))
   4321         except KeyError as e1:
   4322             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'ARR_DELAY'

可能是什么问题?我对数据科学非常陌生,因此我们将不胜感激。

【问题讨论】:

  • 我将首先检查y_test.columns 以确保您尝试使用的列在数据框中被称为相同。 KeyError 表示找不到您要索引的键。

标签: python pandas data-science xgboost mean-square-error


【解决方案1】:

在此处进行训练/测试拆分后:

X_train, X_test, y_train, y_test = train_test_split(df_late.drop(['DELAY_YN','ARR_DELAY'],axis=1), 
                                                    df_late['ARR_DELAY'], test_size=0.30, random_state=101)

y_test 实际上不是 DataFrame 而是 Series,由单列组成。所以y_test['predicted']=[np.exp(p) for p in predictions] 并不是你真正想要的。相反,我建议将预测保存在单独的数组或系列中:

predictions = xg_reg.predict(X_test)

predictions = np.exp(predictions)

from sklearn import metrics
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2020-10-04
    • 2019-07-07
    • 2012-03-15
    • 2020-07-16
    • 2023-02-04
    • 1970-01-01
    相关资源
    最近更新 更多