在用sklearn进行模型预测时发现了几次同样的报错:
ValueError: Expected 2D array, got 1D array instead:
array=XXXXX
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中,所有数据都应该是二维矩阵,哪怕它只是单独一行或一列,需要使用.reshape(1,-1)进行转换,示例如下。
import sklearn.svm as svm
import numpy as np
X = [[0, 0], [1, 1]]
y = [0.5, 1.5]
clf = svm.SVR()
clf.fit(X, y)
result = clf.predict(np.array([0.5, 0.5]).reshape(1, -1))
print result
其实还有更简洁的写法,多加一个[]即可:
result = clf.predict([[0.5, 0.5]])
来自:https://www.cnblogs.com/myshuzhimei/p/11776614.html