Skip to content

Add Permutation Importance#202

Merged
mayer79 merged 67 commits intolorentzenchr:mainfrom
mayer79:enh-permutation-importance
Feb 28, 2026
Merged

Add Permutation Importance#202
mayer79 merged 67 commits intolorentzenchr:mainfrom
mayer79:enh-permutation-importance

Conversation

@mayer79
Copy link
Copy Markdown
Collaborator

@mayer79 mayer79 commented May 10, 2025

Implements #201

@mayer79 mayer79 self-assigned this May 10, 2025
@mayer79 mayer79 added the enhancement New feature or request label May 10, 2025
@mayer79 mayer79 marked this pull request as draft May 10, 2025 10:04
@mayer79 mayer79 changed the title Add compute_permutation_importance() Add Permutation Importance May 10, 2025
@mayer79
Copy link
Copy Markdown
Collaborator Author

mayer79 commented May 16, 2025

This is the current basic call:

import numpy as np
import polars as pl
from sklearn.linear_model import LinearRegression

from model_diagnostics.xai import plot_permutation_importance

rng = np.random.default_rng(1)
n = 1000

X = pl.DataFrame(
    {
        "area": rng.uniform(30, 120, n),
        "rooms": rng.choice([2.5, 3.5, 4.5], n),
        "age": rng.uniform(0, 100, n),
    }
)

y = X["area"] + 20 * X["rooms"] + rng.normal(0, 1, n)

model = LinearRegression()
model.fit(X, y)

_ = plot_permutation_importance(
    predict_function=model.predict,
    X=X,
    y=y,
)

image

The extended feature API allows to permute groups like this:

_ = plot_permutation_importance(
    predict_function=model.predict,
    features={"size": ["area", "rooms"], "age": "age"},
    X=X,
    y=y,
)

image

@mayer79
Copy link
Copy Markdown
Collaborator Author

mayer79 commented Feb 1, 2026

2 important points:

  • Merge with main and resolve merge conflicts. No updates of package versions should be needed.

-> Done

  • Is it possible to construct a simple test where we know the answer (=importance values) and check against that answer?

-> Ideally, we can compare with Scikit-Learn's implementation. But I am still working on this

Comment thread src/model_diagnostics/_utils/array.py Outdated
Comment thread src/model_diagnostics/_utils/array.py Outdated
Comment thread src/model_diagnostics/_utils/array.py Outdated
Comment thread src/model_diagnostics/_utils/array.py Outdated
Comment thread src/model_diagnostics/xai/tests/test_permutation_importance.py Outdated
Comment thread src/model_diagnostics/xai/permutation_importance.py Outdated
Comment thread src/model_diagnostics/xai/permutation_importance.py Outdated
Comment thread src/model_diagnostics/xai/permutation_importance.py Outdated
Comment thread src/model_diagnostics/xai/tests/test_permutation_importance.py
@lorentzenchr
Copy link
Copy Markdown
Owner

I think only the np.split point is open. If this is merged, an example or addition to an existing one would be nice.

@mayer79
Copy link
Copy Markdown
Collaborator Author

mayer79 commented Feb 8, 2026

I think only the np.split point is open. If this is merged, an example or addition to an existing one would be nice.

The condition now is like this:

        # np.split() does not work on pyarrow arrays and should not be used on Pandas
        if (
            is_pyarrow_array(predictions)
            or is_pandas_df(predictions)
            or is_pandas_series(predictions)
        ):
            predictions = predictions.to_numpy()

Open issues:

  1. comparison with another implementation. I was unable to exactly reproduce with Scikit-Learn, even with a single feature / single shuffle situation.
  2. Test failures in calling the scorer pre shuffle

@lorentzenchr
Copy link
Copy Markdown
Owner

  1. comparison with another implementation. I was unable to exactly reproduce with Scikit-Learn, even with a single feature / single shuffle situation.

I guess we can live without it. Do you have any clue as to where this difference might stem from?

  1. Test failures in calling the scorer pre shuffle

CI/CD must be green before merge.

@mayer79
Copy link
Copy Markdown
Collaborator Author

mayer79 commented Feb 14, 2026

  1. comparison with another implementation. I was unable to exactly reproduce with Scikit-Learn, even with a single feature / single shuffle situation.

I guess we can live without it. Do you have any clue as to where this difference might stem from?

I was unable to bring the random number generator to the same state.

  1. Test failures in calling the scorer pre shuffle

CI/CD must be green before merge.

test.py3.12 (locally) gives different failtures than I see on github. Will look into it as soon as I have enough time.

@lorentzenchr
Copy link
Copy Markdown
Owner

@mayer79 ready to merge?

@mayer79 mayer79 merged commit 92e1d9f into lorentzenchr:main Feb 28, 2026
5 checks passed
@mayer79 mayer79 deleted the enh-permutation-importance branch February 28, 2026 20:35
@lorentzenchr
Copy link
Copy Markdown
Owner

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants