【问题标题】:ValueError on sklearn's linear_model.predictsklearn 的 linear_model.predict 上的 ValueError
【发布时间】:2019-07-14 12:49:56
【问题描述】:
import numpy as np 
import pandas as pd
from sklearn import datasets, linear_model
df = pd.read_csv("homeprices.csv")
df
model = linear_model.LinearRegression()
model.fit(df[['area']], df.price)
model.predict(5000)

ValueError: 预期的二维数组,得到的是标量数组: 数组=5000。 如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果数据包含单个样本,则使用 array.reshape(1, -1)。

【问题讨论】:

标签: python scikit-learn data-science valueerror


【解决方案1】:

换行:

model.predict(5000)

到:

model.predict([[5000]])

【讨论】:

  • 请稍微解释一下为什么会这样,但是 OP 的代码没有。
猜你喜欢
  • 2020-07-14
  • 2015-11-09
  • 2020-05-22
  • 1970-01-01
  • 2018-01-27
  • 2020-02-19
  • 2021-04-14
  • 2020-02-10
  • 1970-01-01
相关资源
最近更新 更多