[英]Why is pipeline throwing FitFailedWarining when I try LabelEncoder on my pipeline?
我是機器學習的新手,並試圖制作一個讓我忙碌的項目,所以我不太了解sklearn
的工作原理。 主要目標是訓練 model 來預測分類變量。 當我嘗試labelEncoding
的y
變量進行 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
。 (對此有一些討論,尤其是在重新采樣器中應該同時更改X
和y
的行;請參閱imblearn
以獲取至少在該方向上的修復。)
特別是, fit_transform(X, y)
的默認定義為fit(X, y).transform(X)
。 因此,管道中的LabelEncoder
將嘗試轉換X
,並且會失敗,因為它不知道如何處理 2D 輸入。 您應該只在管道之外對 label 編碼y
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.