無法import Imputer
2020/03/19 04:48 下午
機器學習共學討論版
塔米
觀看數:12
回答數:5
收藏數:0
ml100-4
ml100-4-d21
老師您好,
剛執行Day021作業,發現無法執行這一個程式碼
from sklearn.preprocessing import Imputer
錯誤指出說沒有辦法匯入Imputer,經查詢後應改為如下程式碼:
from sklearn.impute import SimpleImputer
接著把以下程式碼有用到`Imputer`的改成SimpleImputer,程式碼如下:
from sklearn.preprocessing import MinMaxScaler
from sklearn.impute import SimpleImputer
# 特徵欄位清單
train = app_train
features = list(train.columns)
# 複製 test 資料
test = app_test.copy()
# 填補器 : 設定缺失值補中位數
imputer = SimpleImputer(strategy = 'median')
# 縮放器 : 設定特徵縮放到 0~1 區間
scaler = MinMaxScaler(feature_range = (0, 1))
# 填補器載入個欄中位數
imputer.fit(train)
# 將中位數回填 train, test 資料中的空缺值
train = imputer.transform(train)
test = imputer.transform(app_test)
# 縮放器載入 train 的上下限, 對 train, test 進行縮放轉換
scaler.fit(train)
train = scaler.transform(train)
test = scaler.transform(test)
print('Training data shape: ', train.shape)
print('Testing data shape: ', test.shape)
執行過程卻出現問題,指說這一段程式碼`--> 20 imputer.fit(train)`開始有誤,錯誤指示如下。
TypeError: unorderable types: str() < float()
經過下面解答者提供的解法,我改過之後卻出現這個問題
ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
經爬文還是找不到相關資訊><,也看documents但仍無解,請老師指教! 謝謝