例如:如下案例有两个错误
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)