Search code examples
machine-learningcatboost

Not working when value that to be predicted is categorical


Problem: Not working when value that to be predicted is categorical catboost version: 0.8 Operating System: Windows CPU: intel

When my value ( Y ) that too be predicted is categorical, got error of "can not convet to float". Do I have to do one hot encoding of Y value ?

Thanks for help.

Python code:

from sklearn.model_selection import train_test_split
train_set , test_set = train_test_split(trainPLData, test_size=0.2, random_state=42)

for col in ['product_line','a_plant', 'a_pa', 'b_plant','b_pa',
'c_plant', 'c_pa', 'D_plant', 'D_pa', 'fam', 'pkg','defect']:
train_set[col] = train_set[col].astype('category')
test_set[col] = test_set[col].astype('category')

x_train = train_set[['product_line','a_plant', 'a_pa', 'b_plant','b_pa',
'c_plant', 'c_pa', 'D_plant', 'D_pa', 'fam', 'pkg']]
y_train = train_set[['defect']]

x_test = test_set[['product','a_plant', 'a_pa', 'b_plant','b_pa',
'c_plant', 'c_pa', 'D_plant', 'D_pa', 'fam', 'pkg']]
y_test = test_set[['defect']]

from catboost import CatBoostClassifier
model=CatBoostClassifier(iterations=50, depth=3, learning_rate=0.1,one_hot_max_size=10)

categorical_features_indices = np.where(x_train.dtypes != np.float)[0]
print(categorical_features_indices)

model.fit(x_train, y_train,cat_features=categorical_features_indices,
eval_set=(x_test, y_test))

Then Error is:

ValueError: could not convert string to float: 'some defect'


Solution

  • Catboost tries to convert it to float because it needs it to be a number. Use LabelEncoder to do it, it works out fine, I've used it in a MultiClass problem without a hitch.