Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for sklearn Pipelines #171

Open
MyNameIsFu opened this issue Feb 5, 2024 · 1 comment
Open

Support for sklearn Pipelines #171

MyNameIsFu opened this issue Feb 5, 2024 · 1 comment

Comments

@MyNameIsFu
Copy link

MyNameIsFu commented Feb 5, 2024

MCA is currently not able to be part of a sklearn Pipeline containing any preceding steps.
In my case I need an Imputer to fill any NaN values.

Working Example:

from sklearn.impute import SimpleImputer
from prince.mca import MCA

test_data = pd.DataFrame(data=np.random.random((10, 5)))
test = Pipeline(steps=[
    ("mca", MCA()),
])
test.fit_transform(test_data)

But including a SimpleImputer results in a numpy array that is being forwarded to the MCA:

from sklearn.impute import SimpleImputer
from prince.mca import MCA

test_data = pd.DataFrame(data=np.random.random((10, 5)))
test = Pipeline(steps=[
    ("impute", SimpleImputer()), # This Breaks the Pipeline since it returns an ndarray
    ("mca", MCA()),
])
test.fit_transform(test_data)

I've tried including a dummy transformer step betwen the imputer and MCA that forwards an arbitrary DataFrame with generic index and column labels, but it results in a KeyError with unknown Index labels being searched in the column list:

KeyError: "None of [Index(['Col_0_0.0', 'Col_0_1.0', 'Col_0_2.0', 'Col_0_3.0', 'Col_0_4.0',\n       'Col_0_5.0', 'Col_1_0.0', 'Col_1_1.0', 'Col_1_2.0', 'Col_2_0.0',\n       'Col_2_1.0', 'Col_3_0.0', 'Col_3_1.0'],\n      dtype='object')] are in the [columns]"

Any suggestions?

@MaxHalford
Copy link
Owner

Hey there @MyNameIsFu!

I believe you can make this work using sklearn's set_output API:

from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from prince.mca import MCA
import numpy as np

test_data = pd.DataFrame(data=np.random.random((10, 5)))
test = Pipeline(steps=[
    ("impute", SimpleImputer()), # This Breaks the Pipeline since it returns an ndarray
    ("mca", MCA()),
])
test[0].set_output(transform="pandas")
test.fit_transform(test_data)

I hope this works for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants