[Python] HistGradientBoostingClassifier Using Categorical Variables

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

  1. Stack

    Stack Membro Participativo

    I landed on the HistGradientBoostingClassifier because, according to the docs, it has native support for categorical variables without the need for one-hot encoding or label encoding. I tried to run categorical variables through the model but when I fit the model I got the error:

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


    here is a basic code example of what I am trying to do:

    X = pd.DataFrame({
    'feature_1': [1, 2, 3, 1, 2],
    'feature_2': ['A', 'B', 'C', 'A', 'B']
    })

    y = pd.Series([0, 1, 0, 1, 0])

    X['feature_2'] = X['feature_2'].astype('category')

    categorical_features = [1]

    model = HistGradientBoostingClassifier(categorical_features=categorical_features)

    model.fit(X, y)


    I do not have ordinal categorical variables, so I am hesitant to do label encoding. Is there something I am missing here?

    Continue reading...

Compartilhe esta Página