gpt4 book ai didi

python - 如何为不同的分类列创建带有编码的管道?

转载 作者:行者123 更新时间:2023-12-05 04:52:00 25 4
gpt4 key购买 nike

我在尝试实现管道时遇到问题,我想在不同的分类列上使用 OrdinalEncoder 和 OneHotEncoder。

此时我的代码如下:

X = stroke_df.drop(columns=['id', 'smoking_status', 'stroke'])
y = stroke_df['stroke'].copy()

num_columns = X.select_dtypes(np.number).columns.tolist()
cat_columns = X.select_dtypes('object').columns.tolist()
all_columns = num_columns + cat_columns # this order will need to be preserved
print('Numerical columns:', ', '.join(num_columns))
print('Categorical columns:', ', '.join(cat_columns))

num_pipeline = Pipeline([
('imputer', SimpleImputer(missing_values=np.nan, strategy='median')),
('scaler', StandardScaler())
])

cat_pipeline = ColumnTransformer([
('label_encoder', LabelEncoder(), ['ever_married', 'work_type']),
('one_hot_encoder', OneHotEncoder(), ['gender', 'residence_type'])
])

pipeline = ColumnTransformer([
('num', num_pipeline, num_columns),
('cat', cat_pipeline, cat_columns)
])

然而,在尝试调用管道上的 fit_transform 并对输入特征矩阵进行预处理后,我得到了 TypeError:

X_prep = pipeline.fit_transform(X)
TypeError: fit_transform() takes 2 positional arguments but 3 were given

最佳答案

您的错误来自于在您的管道中使用 LabelEncoder。 documentation指出它应该只用于编码 y 变量。如果您的变量确实是有序的,请改用序号编码器,否则使用单热编码。下面的代码也使用了一个简单的管道。

import pandas as pd
import numpy as np

from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder, OrdinalEncoder

# Set-up
df = pd.DataFrame({'gender': np.random.choice(['M', 'F'], size=5),
'ever_married': np.random.choice(['Y', 'N'], size=5),
'residence_type': list('ABCDE'),
'work_type': list('abcde'),
'num_col': np.array([1, 2, np.nan, 3, 4])})

ord_cols = ['ever_married', 'work_type']
ohe_cols = ['gender', 'residence_type']
num_cols = ['num_col']

# Preprocessing pipeline
num_pipeline = Pipeline([
('imputer', SimpleImputer(missing_values=np.nan, strategy='median')),
('scaler', StandardScaler())
])

pipeline = ColumnTransformer(
[
('num_imputer', num_pipeline, num_cols),
('ord_encoder', OrdinalEncoder(), ord_cols),
('ohe_encoder', OneHotEncoder(), ohe_cols)
]
)

# Preprocessing
X_prep = pipeline.fit_transform(df)

输出:

df

gender ever_married residence_type work_type num_col
0 M Y A a 1.0
1 F Y B b 2.0
2 F Y C c NaN
3 M Y D d 3.0
4 M N E e 4.0

X_prep

array([[-1.5, 1. , 0. , 0. , 1. , 1. , 0. , 0. , 0. , 0. ],
[-0.5, 1. , 1. , 1. , 0. , 0. , 1. , 0. , 0. , 0. ],
[ 0. , 1. , 2. , 1. , 0. , 0. , 0. , 1. , 0. , 0. ],
[ 0.5, 1. , 3. , 0. , 1. , 0. , 0. , 0. , 1. , 0. ],
[ 1.5, 0. , 4. , 0. , 1. , 0. , 0. , 0. , 0. , 1. ]])

关于python - 如何为不同的分类列创建带有编码的管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66623208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com