How to use plot_importance function with MultiOutputRegressor?

Viewed 608

I use the code blow to get multi output.

But I got the error message when I want to plot importance.

"ValueError: tree must be Booster, XGBModel or dict instance"

How to fix this problem? Or is there any other way to get the feature importance?

import numpy as np
import xgboost as xgb
from xgboost import plot_importance

X = np.array([[0,1,2,3,4],[2,3,4,5,6],[3,4,5,6,7]])
y = np.array([[2,3,4],[3,4,5],[4,5,6]])

model_ = MultiOutputRegressor(xgb.XGBRegressor(objective='reg:linear',n_jobs=-1))
model_.fit(X, y)
pred = model_.predict(X)

fig,ax = plt.subplots(figsize=(15,15))
plot_importance(model_,height=0.5,ax=ax,max_num_features=3)
plt.show()
1 Answers

I found the solution.

fig,ax = plt.subplots(ncols=3,figsize=(15,6))
plot_importance(model.estimators_[0],height=0.5,ax=ax[0],max_num_features=20)
plot_importance(model.estimators_[1],height=0.5,ax=ax[1],max_num_features=20)
plot_importance(model.estimators_[2],height=0.5,ax=ax[2],max_num_features=20)
plt.show()
Related