xgboost使用的时候经常想看一看特征的重要程度, 结果
from xgboost import plot_importance
from matplotlib import pyplot
import pickle
# load model from file
model = pickle.load(open("xgbmodel/xgb-model-0927", "rb"))
# plot feature importance
plot_importance(model)
pyplot.show()
改变了函数使用还是这样
import matplotlib.pyplot as plt
import pickle
# load model from file
loaded_model = pickle.load(open("xgbmodel/xgb-model-0927", "rb"))
feat_imp = pd.Series(loaded_model.get_booster().get_score(importance_type='weight')).sort_values(ascending=False)
feat_imp.plot(kind='bar', title='Feature Importances')
plt.ylabel('Feature Importance Score')
plt.show()
原因是: XGBClassifier本身就暂时没有这个办法
This is the expected behaviour- sklearn.OneHotEncoder.transform() returns a numpy 2d array instead of the input pd.DataFrame (i assume that’s the type of your dataset). So it is not a bug, but a feature. It doesn’t look like there is a way to pass feature names manually in the sklearn API (it is possible to set those in xgb.Dmatrix creation in the native training API).