Permutation importance, computed correctly
On the test set, with the metric you actually care about. Running it on training data measures memorisation instead.
from sklearn.inspection import permutation_importance
import numpy as np
r = permutation_importance(
model, X_test, y_test, # TEST, not train
n_repeats=20,
scoring="average_precision", # the metric that matters here
n_jobs=-1, random_state=42,
)
order = r.importances_mean.argsort()[::-1]
for i in order[:12]:
mean, std = r.importances_mean[i], r.importances_std[i]
flag = "" if mean - 2 * std > 0 else " (not distinguishable from 0)"
print(f" {feature_names[i]:28} {mean:+.4f} +/- {std:.4f}{flag}")
# Features whose importance is within two standard deviations of zero
# are not doing measurable work. That is a defensible reason to drop
# them, and it reads well in a model document.
#
# Caveat: with two highly correlated features, permuting either alone
# does little because the other carries the signal, so BOTH look
# unimportant. Check correlations before concluding.