例如:如下案例有兩個錯誤
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)