[Python] ValueError when fitting scikit-learn pipeline with OneHotEncoder and...

Discussão em 'Python' iniciado por Stack, Setembro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to build a simple ML pipeline in Python using scikit-learn.
    My dataset has both categorical and numerical features. I want to apply OneHotEncoder to the categorical columns and then fit a LogisticRegression model.

    Here’s my code:

    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import OneHotEncoder
    from sklearn.compose import ColumnTransformer
    from sklearn.pipeline import Pipeline
    from sklearn.linear_model import LogisticRegression

    data = pd.DataFrame({
    "age": [25, 32, 40, 29],
    "city": ["Delhi", "Mumbai", "Delhi", "Bangalore"],
    "purchased": [0, 1, 0, 1]
    })

    X = data[["age", "city"]]
    y = data["purchased"]

    categorical_features = ["city"]
    numeric_features = ["age"]

    preprocessor = ColumnTransformer(
    transformers=[
    ("cat", OneHotEncoder(), categorical_features),
    ("num", "passthrough", numeric_features)
    ]
    )

    pipeline = Pipeline(steps=[
    ("preprocessor", preprocessor),
    ("model", LogisticRegression())
    ])

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

    pipeline.fit(X_train, y_train) # <-- Error here


    When I run this, I get:

    ValueError: could not convert string to float: 'Delhi'


    But I thought OneHotEncoder should handle categorical strings.


    Checked scikit-learn version (1.3.0).

    Tried using handle_unknown='ignore' in OneHotEncoder.

    Verified that city column is object dtype.

    Why am I still getting this error, and how can I correctly fit this pipeline with categorical and numeric features?

    Continue reading...

Compartilhe esta Página