ValueError: Expected 2D array, got 1D array instead:
错误图示- 问题: 对模型进行预测的时候出现
xPred = [102, 6]
yPred = regr.predict(xPred)
会出现以下错误
ValueError: Expected 2D array, got 1D array instead:
array=[102 6].
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.
解决方法
xPred = [100,6]
xPred = np.array(xPred).reshape(1,-1)
yPred = regr.predict(xPred)
原因是新版sklearn中所有的数据都应该是二维矩阵,需要使用.reshape(1,-1)进行转换。
网友评论