sklearn 错误集合
2019-04-29 本文已影响0人
sttech
ValueError: Expected 2D array, got 1D array instead:
![](https://img.haomeiwen.com/i2606566/d52d6bd55ff53aff.png)
- 问题: 对模型进行预测的时候出现
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)进行转换。