问题:当预测值为分类变量时无法工作
CatBoost版本:0.8
操作系统:Windows
CPU:Intel
当我的预测值(Y)为分类变量时,出现了“无法转换为浮点数”的错误。我是否需要对Y值进行独热编码?
感谢帮助。
Python代码:
from sklearn.model_selection import train_test_splittrain_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 CatBoostClassifiermodel=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))
然后错误是:
ValueError: 无法将字符串转换为浮点数:’some defect’
回答:
CatBoost尝试将其转换为浮点数,因为它需要它是一个数字。使用LabelEncoder来处理它,效果很好,我在多类问题中使用它没有遇到任何问题。