【问题标题】:How to show original feature names in the feature importance plot?如何在特征重要性图中显示原始特征名称?
【发布时间】:2018-12-06 12:13:27
【问题描述】:

我创建的 XGBoost 模型如下:

y = XY.DELAY_MIN
X = standardized_df

train_X, test_X, train_y, test_y = train_test_split(X.as_matrix(), y.as_matrix(), test_size=0.25)

my_imputer = preprocessing.Imputer()
train_X = my_imputer.fit_transform(train_X)
test_X = my_imputer.transform(test_X)

xgb_model = XGBRegressor()

# Add silent=True to avoid printing out updates with each cycle
xgb_model = XGBRegressor(n_estimators=1000, learning_rate=0.05)
xgb_model.fit(train_X, train_y, early_stopping_rounds=5, 
             eval_set=[(test_X, test_y)], verbose=False)

创建特征重要性图时,特征名称显示为“f1”、“f2”等。如何显示原始特征名称?

fig, ax = plt.subplots(figsize=(12,18))
xgb.plot_importance(xgb_model, max_num_features=30, height=0.8, ax=ax)
plt.show()

【问题讨论】:

    标签: python pandas xgboost


    【解决方案1】:

    问题是Imputer 不返回pd.DataFrame 作为transform() 的输出,因此,当你这样做时,你的列名会丢失

    train_X = my_imputer.fit_transform(train_X)
    test_X = my_imputer.transform(test_X)
    

    简单的解决方案,将 imputer 输出包装到数据帧中,例如这样:

    train_X = pd.DataFrame(my_imputer.fit_transform(train_X), columns=train_X.columns)
    test_X  = pd.DataFrame(my_imputer.transform(test_X), columns=test_X.columns)
    

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 2012-12-17
      • 2018-07-07
      • 1970-01-01
      • 2019-08-10
      • 2022-06-20
      • 2018-02-11
      • 2019-12-04
      相关资源
      最近更新 更多