Feature selection, Converting Boxplot to a barH

19 views Asked by At

I am trying to create a feature importance plot, and I got the code below from this Scikit-Learn documentation because anytime I try other means, either the randomizedsearchCV is giving an error or the pipeline is, so i got this https://scikit-learn.org/stable/auto_examples/inspection/plot_permutation_importance.html#tree-s-feature-importance-from-mean-decrease-in-impurity-mdi but all my efforts to convert it from a boxplot to a barh have been futile.

Here is my model

X_train, X_test, y_train, y_test = train_test_split(mckeee_feautures, mckeee_poro, test_size=0.4, random_state=42)
preprocessor = ColumnTransformer([
    ('numeric', num_pipe(scaling='robust', transform='yeo-johnson', poly=2), mckeee_feautures.columns)
])

pipeline = Pipeline([
    ('prep', preprocessor),
    ('algo', XGBRegressor(n_jobs=-1, random_state=42))
])


model = RandomizedSearchCV(pipeline, rsp.xgb_poly_params, cv=10, n_jobs=-1, verbose=1, random_state=42)
model.fit(X_train, y_train)

print(model.best_params_)
print(model.score(X_train, y_train), model.best_score_, model.score(X_test, y_test))
from sklearn.inspection import permutation_importance

result = permutation_importance(model, X_test, y_test, n_repeats=10,
                                random_state=42, n_jobs=2)
sorted_idx = result.importances_mean.argsort()

fig, ax = plt.subplots()
ax.boxplot(result.importances[sorted_idx].T,
           vert=False, labels=X_test.columns[sorted_idx])
ax.set_title("Permutation Importances (test set)")
ax.axvline(x=0, color="k", linestyle="--")
fig.tight_layout()
plt.show()

With the code above i get this plot enter image description here

plt.barh(mckeee_feautures.columns[sorted_idx], result.importances[sorted_idx].T)

plt.barh(X_test.columns[sorted_idx], result.importances[sorted_idx].T)

but when I tried converting, it kept giving varying errors.

like TypeError: only size-1 arrays can be converted to Python scalars

0

There are 0 answers