[Python] sklearn ColumnTransformer with MultilabelBinarizer

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I wonder if it is possible to use a MultilabelBinarizer within a ColumnTransformer.

    I have a toy pandas dataframe like:

    df = pd.DataFrame({"id":[1,2,3],
    "text": ["some text", "some other text", "yet another text"],
    "label": [["white", "cat"], ["black", "cat"], ["brown", "dog"]]})

    preprocess = ColumnTransformer(
    [
    ('vectorizer', CountVectorizer(), 'text'),
    ('binarizer', MultiLabelBinarizer(), ['label']),

    ],
    remainder='drop')


    this code, however, throws an exception:

    ~/lib/python3.7/site-packages/sklearn/pipeline.py in _fit_transform_one(transformer, X, y, weight, message_clsname, message, **fit_params)
    714 with _print_elapsed_time(message_clsname, message):
    715 if hasattr(transformer, 'fit_transform'):
    --> 716 res = transformer.fit_transform(X, y, **fit_params)
    717 else:
    718 res = transformer.fit(X, y, **fit_params).transform(X)

    TypeError: fit_transform() takes 2 positional arguments but 3 were given


    With OneHotEncoder the ColumnTransformer does work.

    Continue reading...

Compartilhe esta Página