Skip to content

permutation_importance with polars warns on invalid feature names #28488

Open

Description

@Julian-J-S

Describe the workflow you want to enable

using polars data (DataFrame, Series) is already supported in many places which is awesome, thank you!!

But in many places there are still

  • errors / crashes -> required conversion to numpy/pandas
  • warnings -> requires conversion to numpy/pandas
  • linting/type problems -> requires updates to typing signalture?

Examples

Code

import pandas as pd
import polars as pl
from sklearn.datasets import make_classification
from sklearn.inspection import permutation_importance
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score, cross_validate

X, y = make_classification()

# X = pd.DataFrame(X)
# y = pd.Series(y)

# X = pl.DataFrame(X)
# y = pl.Series(y)

clf = LogisticRegression()

clf.fit(
    X=X,
    y=y,
)

clf.score(
    X=X,  # Lint/Type Problem
    y=y,
)

cross_val_score(
    estimator=clf,
    X=X,  # Lint/Type Problem
    y=y,  # ERROR with polars
)

cross_validate(
    estimator=clf,
    X=X,  # Lint/Type Problem
    y=y,  # ERROR with polars
)

permutation_importance(
    estimator=clf,
    X=X,  # WARNING with polars + Lint/Type Problem
    y=y,
)

clf.predict(X)

Errors / crashes using polars

Both cross_val_score and cross_validate crash using polars Series with message:

  • TypeError: cannot use getitem on Series of dtype Int32 with argument (array([18, 21, 22, 23, 24, 25,...

Temporary solution

use

  • to_numpy() -> works with numpy array
  • to_pandas() -> works with pandas Series

Warnings

permutation_importance creates "UserWarnings" using polars DataFrame with message:

  • UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names

Temporary Solution

use

  • to_pandas() -> works with pandas DataFrame

Linting / Type problems

numpy arrays and pandas DataFrame have "full support" while polars looks a little sad 😆

image

Describe your proposed solution

would love to see further support for polars DataFrame / Series types.

Not sure why some things error, because I thought polars provides all the required apis.

Any way to assist / help?

Describe alternatives you've considered, if relevant

No response

Additional context

No response

Activity

  1. glemaitre commented on Feb 20, 2024

    @glemaitre
    Member

    Wild guess would be that _safe_indexing does not support the polars maybe. I need to check deeper.

  2. lesteve commented on Feb 21, 2024

    @lesteve
    Member

    About linting / typing issues you mentioned, the type annotations are not maintained by the scikit-learn project so they come from somewhere else, maybe https://github.com/microsoft/python-type-stubs/tree/main/stubs/sklearn?

  3. Charlie-XIAO commented on Feb 21, 2024

    @Charlie-XIAO
    Contributor

    It seems that _safe_indexing uses polars indexing only for polars dataframes but not for polars series, which causes cross_validate and cross_val_score (that uses cross_validate) to fail.

    The warning of permutation_importance is caused by converting polars DataFrame to numpy array (while we detect pandas DataFrame and avoid such a conversion).

  4. added and removed
    Needs Triage
    on Mar 11, 2024
  5. changed the title [-]Improve "polars" integration (error, warning & linting examples)[/-] [+]permutation_importance with polars warns on invalid feature names[/+] on Apr 7, 2026
  6. jeremiedbb commented on Apr 7, 2026

    @jeremiedbb
    Member

    The only remaining bug here is the warning on invalid feature names raised by permuation_importance for polars dataframes. I updated the title of this issue accordingly.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions