[已解决]报错:ValueError: Expected 2D array, got scalar array instead

时间:2022-07-25
本文章向大家介绍[已解决]报错:ValueError: Expected 2D array, got scalar array instead,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

报错代码:

new_x = 84610
pre_y = model.predict(new_x)
print(pre_y)

报错结果:

ValueError: Expected 2D array, got scalar array instead:
array=84610.
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.

解决思路:

值错误:应为二维数组,而得到的是一维数组: 使用array重新调整数据的形状。如果数据有单个功能或数组,则重新调整形状(-1,1)。如果数据包含单个示例,则重新调整形状(1,-1)。

解决方案:

加上

new_x = np.array(new_x).reshape(1, -1)

修改后的代码:

new_x = 84610
new_x = np.array(new_x).reshape(1, -1)
pre_y = model.predict(new_x)
print(pre_y)