簡體   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