当使用训练出来的模型进行预测的时候,如果输入的需要预测的数据只有一条,会报错:
ValueError: Expected 2D array, got 1D array instead:
array=[8.09360732 3.36573151].
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要求需要把预测的样本变成二维数组即矩阵的形式。如果是单样本多特征,用array.reshape(1, -1)
处理后进行预测;如果是多样本单特征,用array.reshape(-1, 1)
处理后进行预测,便不会报错。