类型错误:fit_transform() 采用 2 个位置参数,但给出了 3 个 [英] TypeError: fit_transform() takes 2 positional arguments but 3 were given

查看:91
本文介绍了类型错误:fit_transform() 采用 2 个位置参数,但给出了 3 个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Pandas DataFrame df.我想使用不同的编码器对 df 的连续和分类特征进行编码.我发现使用 make_column_transformer 很舒服,但下面显示的代码使用 LabelEncoder() 失败,但使用 OneHotEncoder(handle_unknown='ignore')).错误信息是:

I have pandas DataFrame df. I want to encode continuous and categorical features of df using different encoders. I find it very comfortable to use make_column_transformer, but the code shown below fails with LabelEncoder(), but works fine with OneHotEncoder(handle_unknown='ignore')). The error message is:

TypeError: fit_transform() 需要 2 个位置参数,但 3 个是给定

TypeError: fit_transform() takes 2 positional arguments but 3 were given

我不清楚如何解决这个问题.

It's not clear to me how to fix this issue.

代码:

from sklearn.compose import make_column_transformer
from sklearn.preprocessing import RobustScaler, OneHotEncoder, LabelEncoder

continuous_features = ['COL1','COL2']       
categorical_features = ['COL3','COL4']

column_trans = make_column_transformer(
    (categorical_features,LabelEncoder()),
    (continuous_features, RobustScaler()))

X_enc = column_trans.fit_transform(df)

推荐答案

根据 https://scikit-learn.org/stable/modules/generated/sklearn.compose.make_column_transformer.html.

make_column_transformer(
...     (StandardScaler(), ['numerical_column']),
...     (OneHotEncoder(), ['categorical_column']))

所以对于你的情况:

from sklearn.compose import make_column_transformer
from sklearn.preprocessing import RobustScaler, OneHotEncoder, LabelEncoder

continuous_features = ['COL1','COL2']       
categorical_features = ['COL3','COL4']

column_trans = make_column_transformer(
    (OneHotEncoder(), categorical_features),
    (RobustScaler(), continuous_features))

X_enc = column_trans.fit_transform(df)

如果要使用LabelEncoder(),只能传一列,不能传两列!

If you want to use LabelEncoder(), you can only pass one column, not two!

希望这会有所帮助.

这篇关于类型错误:fit_transform() 采用 2 个位置参数,但给出了 3 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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