Which features does a trained model actually rely on? Permutation importance answers it for any model: shuffle one feature column, breaking its link to the target, and measure how much the score drops.
Implement permutation_importance(predict, X, y, metric, n_repeats=5, seed=0):
predict(X) returns predictions, and metric(y, y_pred) returns a score where higher is better.j, in column order, repeat n_repeats times: copy X, shuffle column j with rng.permutation, and record baseline − score. Use one rng = np.random.default_rng(seed) created at the start, so results are reproducible.X.