Sklearn之坑及解決方法

例如:如下案例有兩個錯誤

from sklearn.datasets import load_boston
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2

X_boston, y_boston = load_boston(return_X_y=True)
K_best = SelectKBest(score_func=chi2, k=4).fit_transform(X_boston, y_boston)
print(K_best)

錯誤一:ValueError: Expected 2D array, got 1D array instead:

錯誤提示
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
解決方法
在新版的sklearn中,所有的數據都應該是二維矩陣,哪怕它只是單獨一行或一列(比如前面做預測時,僅僅只用了一個樣本數據),這裏的y_boston是一維矩陣需要使用.reshape(1,-1)進行轉換。

new_y_boston = y_boston.reshape(-1, 1)

錯誤二:ValueError(“Unknown label type: %s” % repr(ys))

使用sklearn時,有些地方會出現標籤不能是小數,需要轉換成整數。

new_y_boston = new_y_boston.astype(int)

修改過後:

from sklearn.datasets import load_boston
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2

X_boston, y_boston = load_boston(return_X_y=True)
new_y_boston = y_boston.reshape(-1, 1)
new_y_boston = new_y_boston.astype(int)
K_best = SelectKBest(score_func=chi2, k=4).fit_transform(X_boston, new_y_boston)
print(K_best)
發佈了128 篇原創文章 · 獲贊 32 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章