介紹
CatBoost的一大特點是可以很好的處理類別特征(Categorical Features)。當我們將其結合到Sklearn的Pipeline中時,會發生如下報錯:
_catboost.CatBoostError: 'data' is numpy array of floating point numerical type, it means no categorical features, but 'cat_features' parameter specifies nonzero number of categorical features
因為CatBoost需要檢查輸入訓練數據pandas.DataFrame中對應的cat_features
。如果我們使用Pipeline后,輸入給.fit()
的數據是被修改過的,DataFrame中的columns的名字變為了數字。
解決方案
我們提前在數據上使用Pipeline,然后將原始數據轉換為Pipeline處理后的數據,然后檢索出其中包含的類別特征,將其傳輸給Catboost。
# define your pipeline
pipeline = Pipeline(steps=[('preprocessor', preprocessor),('classifier', model),
])preprocessor.fit(X_train)
transformed_X_train = pd.DataFrame(preprocessor.transform(X_train)).convert_dtypes()new_cat_feature_idx = [transformed_X_train.columns.get_loc(col) for col in transformed_X_train.select_dtypes(include=['int64', 'bool']).columns]pipeline.fit(X_train, y_train, classifier__cat_features=new_cat_feature_idx)