0

I'm trying to make a linear regression algorithm that predicts a number of points a team will get depending on its value, I have loaded up the .csv file and I haven't encountered any problems doing so. However, when trying to make up my own value and attempting to make an estimate of the value I put in, it gives me this error:

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.

Here is my code:

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))
desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

0

The model was trained on vectors (even if those vectors are of length 1), so model.predict() accepts a sequence (list, array, etc), not a float. The error tells you exactly what's wrong and how to remedy it.

desertnaut
  • 57,590
  • 26
  • 140
  • 166