[Python] Using Ordinal Encoding with Pipeline when there is new data in test set

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to use pipeline of One-Hot/Ordinal encoding with different models using cross_val_score. When there is unseen data in test set I handle it like this for OHE:

    ohe = make_column_transformer((OneHotEncoder(handle_unknown='ignore'), cate_cols),
    remainder='passthrough')


    however for ordinal encoder I cannot figure out how... I've tried:

    oe = make_column_transformer((OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=np.nan), cate_cols),
    remainder='passthrough')


    when I run:

    dt_reg = DecisionTreeRegressor()
    model = ("DT_reg_oe", make_pipeline(oe, dt_reg))
    cv_results = cross_val_score(model, X, y, cv=5, scoring=scoring)


    this outputs: ValueError: Input contains NaN, infinity or a value too large for dtype('float32') which does not make sense because I've selected only columns that have dtypes category and object. I need to change category dtype to float(isn't this what ordinal encoding is supposed to do)? how can I use ordinal encoding for categorical columns inside pipeline?

    Continue reading...

Compartilhe esta Página