報錯代碼:
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)