sklearn 管道 - 如何在不同的列上应用不同的转换 [英] sklearn pipeline - how to apply different transformations on different columns

查看:23
本文介绍了sklearn 管道 - 如何在不同的列上应用不同的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 sklearn 中的管道很陌生,我遇到了这个问题:我有一个混合了文本和数字的数据集,即某些列只有文本,其余列有整数(或浮点数).

I am pretty new to pipelines in sklearn and I am running into this problem: I have a dataset that has a mixture of text and numbers i.e. certain columns have text only and rest have integers (or floating point numbers).

我想知道是否可以构建一个管道,例如,我可以在文本功能上调用 LabelEncoder() 并在数字列上调用 MinMaxScaler().我在网上看到的例子大多指向在整个数据集上使用 LabelEncoder() 而不是在选择列上.这可能吗?如果是这样,任何指针将不胜感激.

I was wondering if it was possible to build a pipeline where I can for example call LabelEncoder() on the text features and MinMaxScaler() on the numbers columns. The examples I have seen on the web mostly point towards using LabelEncoder() on the entire dataset and not on select columns. Is this possible? If so any pointers would be greatly appreciated.

推荐答案

我通常的做法是使用 FeatureUnion,使用 FunctionTransformer 拉出相关的列.

The way I usually do it is with a FeatureUnion, using a FunctionTransformer to pull out the relevant columns.

重要说明:

  • 你必须用 def 定义你的函数,因为烦人的是你不能在 FunctionTransformer 中使用 lambdapartial 如果你想腌制您的模型

  • You have to define your functions with def since annoyingly you can't use lambda or partial in FunctionTransformer if you want to pickle your model

你需要用validate=False

像这样:

from sklearn.pipeline import make_union, make_pipeline
from sklearn.preprocessing import FunctionTransformer

def get_text_cols(df):
    return df[['name', 'fruit']]

def get_num_cols(df):
    return df[['height','age']]

vec = make_union(*[
    make_pipeline(FunctionTransformer(get_text_cols, validate=False), LabelEncoder()))),
    make_pipeline(FunctionTransformer(get_num_cols, validate=False), MinMaxScaler())))
])

这篇关于sklearn 管道 - 如何在不同的列上应用不同的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆