如何在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)
相关推荐
毕设源码-郭学长12 分钟前
【开题答辩全过程】以 Python基于大数据的四川旅游景点数据分析与可视化为例,包含答辩的问题和答案
大数据·python·数据分析
海底的星星fly14 分钟前
【Prompt学习技能树地图】单一思维链优化-自我一致性提示工程原理、实践与代码实现
人工智能·语言模型·prompt
无妄无望20 分钟前
解码器系列(1)BERT
人工智能·深度学习·bert
葡萄与www30 分钟前
模块化神经网络
人工智能·深度学习·神经网络·机器学习
Lin_Aries_042133 分钟前
容器化 Flask 应用程序
linux·后端·python·docker·容器·flask
MediaTea43 分钟前
Jupyter Notebook:基于 Web 的交互式编程环境
前端·ide·人工智能·python·jupyter
colus_SEU44 分钟前
【循环神经网络3】门控循环单元GRU详解
人工智能·rnn·深度学习·机器学习·gru
阿_旭44 分钟前
基于深度学习的CT扫描图像肝脏肿瘤智能检测与分析系统【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·肝脏肿瘤分割
Juicedata1 小时前
九识智能:基于 JuiceFS 的自动驾驶多云亿级文件存储
人工智能·机器学习·自动驾驶
平和男人杨争争1 小时前
情绪识别论文阅读——EMO
论文阅读·人工智能·机器学习