如何在Sklearn Pipeline中运行CatBoost

介绍

CatBoost的一大特点是可以很好的处理类别特征(Categorical Features)。当我们将其结合到Sklearn的Pipeline中时,会发生如下报错:

shell 复制代码
_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。

python 复制代码
# 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)
相关推荐
封印师请假去地球钓鱼6 分钟前
生产力工具 AI 图片生成工具介绍
人工智能·生成式图片
⁡ ⁡10 分钟前
Python打字练习
python
厚德云32 分钟前
Runway开放Gen-3:比Sora更好的视频模型?
人工智能·ai·云计算·视频
新手村领路人43 分钟前
macos m2 百度paddleocr文字识别 python
开发语言·python·macos
JAMES费1 小时前
python机器人编程——用pytorch实现六轴机械臂的正向和逆向数值解算,及python算法解析
pytorch·python·机器人
渡众机器人1 小时前
自动驾驶水泥搅拌车在梁场的应用(下)
人工智能·科技·物联网·机器学习·机器人·自动驾驶·搅拌车
PythonFun1 小时前
如何用Python向PPT中批量插入图片
服务器·python·powerpoint
yuanlulu1 小时前
在昇腾服务器上使用llama-factory对baichuan2-13b模型进行lora微调
人工智能·深度学习·lora·nlp·大语言模型·llama
CoderIsArt1 小时前
Python:一个挑选黑色棋盘的程序
python·计算机视觉