gpt4 book ai didi

python - 为 onehotencoder 变量创建管道不起作用

转载 作者:行者123 更新时间:2023-12-04 08:37:03 26 4
gpt4 key购买 nike

我有一个问题,我试图将转换应用于我的分类特征“国家”和我的其余数字列。我怎么能做到这一点,因为我在下面尝试:

preprocess = make_column_transformer(
(numeric_cols, make_pipeline(MinMaxScaler())),
(categorical_cols, OneHotEncoder()))

model = make_pipeline(preprocess,XGBClassifier())

model.fit(X_train, y_train)
请注意,numeric_cols 作为列表传递,categorical_cols 也是如此。
然而 i get this error: TypeError: All estimators should implement fit and transform, or can be 'drop' or 'passthrough' specifiers.以及我所有数字列的列表 (type <class 'list'>) doesn't.我做错了什么,我该如何处理列国家中看不见的类别?

最佳答案

您需要先放置转换函数,然后将列作为后续参数,如果您查看帮助页面,它会写道:

sklearn.compose.make_column_transformer(*transformers, **kwargs)


一些像下面的将工作:
from sklearn.preprocessing import StandardScaler, OneHotEncoder,MinMaxScaler
from sklearn.compose import make_column_transformer
from sklearn.pipeline import make_pipeline

from xgboost import XGBClassifier

import numpy as np
import pandas as pd

X = pd.DataFrame({'x1':np.random.uniform(0,1,5),
'x2':np.random.choice(['A','B'],5)})

y = pd.Series(np.random.choice(['0','1'],5))

numeric_cols = X.select_dtypes('number').columns.to_list()
categorical_cols = X.select_dtypes('object').columns.to_list()

preprocess = make_column_transformer(
(MinMaxScaler(),numeric_cols),
(OneHotEncoder(),categorical_cols)
)

model = make_pipeline(preprocess,XGBClassifier())
model.fit(X,y)

Pipeline(steps=[('columntransformer',
ColumnTransformer(transformers=[('minmaxscaler',
MinMaxScaler(), ['x1']),
('onehotencoder',
OneHotEncoder(), ['x2'])])),
('xgbclassifier', XGBClassifier())])

关于python - 为 onehotencoder 变量创建管道不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64755898/

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