我正在尝试建立一个线性回归算法,该算法根据团队的价值来预测团队将获得的积分。我已经加载了.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()
接受序列(列表,数组等),而不是浮点数。该错误告诉您确切的问题以及解决方法。