繁体   English   中英

当我在管道上尝试 LabelEncoder 时,为什么管道会抛出 FitFailedWarining?

[英]Why is pipeline throwing FitFailedWarining when I try LabelEncoder on my pipeline?

我是机器学习的新手,并试图制作一个让我忙碌的项目,所以我不太了解sklearn的工作原理。 主要目标是训练 model 来预测分类变量。 当我尝试labelEncodingy变量进行 labelEncoding 时,出现以下错误:

ValueError: not enough values to unpack (expected 3, got 2)

  FitFailedWarning)

这是我正在使用的代码

#Rough training

cols_to_use = [col for col in formatData.columns if col not in 'type1']
x = formatData[cols_to_use]
y = formatData.type1
#print(x.columns)
#print(y)


numerical_transformer = SimpleImputer(strategy='constant')
categorical_tansformer = Pipeline(steps=[
                                        ('imputer', SimpleImputer(strategy='most_frequent')),
                                        ('label', LabelEncoder())
                                        ])


preprocessor = ColumnTransformer(transformers=[('num',numerical_transformer),('cat',categorical_tansformer)])

my_pipeline = Pipeline(steps=[('preprocessor',preprocessor),
                              ('model',RandomForestRegressor(n_estimators=50,random_state=0))])

from sklearn.model_selection import cross_validate
from sklearn.model_selection import cross_val_predict

cv_results = cross_validate(my_pipeline,x,y,cv=5,scoring=('r2','neg_mean_absolute_error'))

predictions = cross_val_predict(my_pipeline,x,y,cv=5)
print(cv_results['test_neg_mean_absolute_error'])
print(predictions)

任何帮助表示赞赏,如果您需要更多信息,请发表评论。

管道旨在转换X ,而不是y (对此有一些讨论,尤其是在重新采样器中应该同时更改Xy的行;请参阅imblearn以获取至少在该方向上的修复。)

特别是, fit_transform(X, y)的默认定义为fit(X, y).transform(X) 因此,管道中的LabelEncoder将尝试转换X ,并且会失败,因为它不知道如何处理 2D 输入。 您应该只在管道之外对 label 编码y

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM