尝试使用 model.predict() 预测值的线性回归错误



我正在尝试制作一种线性回归算法,该算法根据其值预测团队将获得的分数,我已经加载了.csv文件,并且没有遇到任何问题。但是,当尝试弥补我自己的价值并尝试估计我输入的价值时,它会给我这个错误:

Traceback (most recent call last):
File "", line 28, in <module>
print(model.predict(enquiry))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/linear_model/_base.py", line 236, in predict
return self._decision_function(X)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/linear_model/_base.py", line 218, in _decision_function
X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/utils/validation.py", line 73, in inner_f
return f(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/utils/validation.py", line 617, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got scalar array instead:
array=566.0.
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.

这是我的代码:

import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
df = pd.read_csv("premstats.csv")
print(df.describe())
print(df.columns)
y = df.Points
X = df.Value
X = X.values.reshape(-1, 1)
y = y.values.reshape(-1, 1)
# Can we do linear regression on this?
model = LinearRegression()
model.fit(X,y)
predictions = model.predict(X)
plt.scatter(X, y, alpha=0.4)
# Plot line here:
plt.plot(X,predictions, "-")
plt.title("Premier League")
plt.xlabel("Team Values from seaons 2013/14 to 2018/19")
plt.ylabel("Points collected")
plt.show()
while True:
enquiry = float(input("Enter the value of a team, and I'll predict the number of points they'll collect!"))
print(model.predict(enquiry))

该模型是在向量上训练的(即使这些向量的长度为 1(,因此model.predict()接受序列(列表、数组等(,而不是浮点数。该错误会告诉您到底出了什么问题以及如何补救。

最新更新