列变压器系列不明确错误

2024-04-25 22:52:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个管道,但出现了错误

有什么建议吗?我的两个输入列都是分类的,输出也是分类的

代码

from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.impute import SimpleImputer

target = dataframe.final
features = dataframe[['A', 'B']].copy()
X_train1, X_test1, y_train1, y_test1 = train_test_split(features, target,test_size=0.2, 
random_state=42)
numerical_features = features.dtypes == 'float'
categorical_features = ~numerical_features

preprocess = make_column_transformer(
(numerical_features, make_pipeline(SimpleImputer(), StandardScaler())),
(categorical_features, OneHotEncoder()))
model = make_pipeline(
                      preprocess,
                      LogisticRegression())
model.fit(X_train1, y_train1)

错误:

        ValueError                                Traceback (most recent call last)
    <ipython-input-53-e1960c314217> in <module>
         14     preprocess,
         15     LogisticRegression())
    ---> 16 model.fit(X_train1, y_train1)
         17 print("logistic regression score: %f" % model.score(X_test1, y_test1))

    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Tags: fromtestimportmakemodelpipeline错误分类