如何在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)
相关推荐
noravinsc3 分钟前
InforSuite AS 可以发布django和vue项目是否可行
vue.js·python·django
闭月之泪舞4 分钟前
OpenCv高阶(4.0)——案例:海报的透视变换
人工智能·opencv·计算机视觉
九亿AI算法优化工作室&24 分钟前
乡村地区无人机医药配送路径规划与优化仿真
人工智能·算法·matlab·回归
jndingxin27 分钟前
OpenCV CUDA模块中矩阵操作-----矩阵最大最小值查找函数
人工智能·opencv
AI technophile31 分钟前
OpenCV计算机视觉实战(5)——图像基础操作全解析
python·opencv·计算机视觉
AI Echoes34 分钟前
LLM(大语言模型)部署加速方法——PagedAttention
人工智能·语言模型·自然语言处理
Time Famine35 分钟前
射击游戏demo11
python·游戏·pygame
yangshuo128142 分钟前
风车OVF镜像:解放AI开发限制的Ubuntu精简系统
linux·人工智能·ubuntu
Jamence1 小时前
多模态大语言模型arxiv论文略读(七十七)
人工智能·语言模型·自然语言处理
学地理的小胖砸1 小时前
【Python 面向对象】
开发语言·python