如何在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)
相关推荐
云边有个稻草人3 分钟前
基于CANN ops-nn的AIGC神经网络算子优化与落地实践
人工智能·神经网络·aigc
chian-ocean4 分钟前
视觉新范式:基于 `ops-transformer` 的 Vision Transformer 高效部署
人工智能·深度学习·transformer
程序猿追7 分钟前
探索 CANN Graph 引擎的计算图编译优化策略:深度技术解读
人工智能·目标跟踪
哈__7 分钟前
CANN加速语音识别ASR推理:声学模型与语言模型融合优化
人工智能·语言模型·语音识别
慢半拍iii17 分钟前
CANN算子开发实战:手把手教你基于ops-nn仓库编写Broadcast广播算子
人工智能·计算机网络·ai
历程里程碑24 分钟前
普通数组----合并区间
java·数据结构·python·算法·leetcode·职场和发展·tornado
weixin_3954489125 分钟前
mult_yolov5_post_copy.c_cursor_0205
c语言·python·yolo
User_芊芊君子30 分钟前
CANN数学计算基石ops-math深度解析:高性能科学计算与AI模型加速的核心引擎
人工智能·深度学习·神经网络·ai
小白|33 分钟前
CANN与联邦学习融合:构建隐私安全的分布式AI推理与训练系统
人工智能·机器学习·自动驾驶
艾莉丝努力练剑41 分钟前
hixl vs NCCL:昇腾生态通信库的独特优势分析
运维·c++·人工智能·cann