I'm trying to create a linear regression in python, but I keep getting the error “Expected 2D array, got 1D array instead:” How do I fix this?
This is the code
%matplotlib inline
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import pandas as pd
import sklearn
data = pd.read_csv('C:\\Users\\colew\\2017-18_AdvancedStats_Salary.csv')
data = data.drop(columns = "Player")
data = data.drop(columns = "Salary")
data = data.drop(columns = "Pos")
data = data.drop(columns = "Tm")
from sklearn import linear_model
X = data['VORP']
y = data['WS']
lm = linear_model.LinearRegression()
model = lm.fit(X,y)
This is the error
valueError: Expected 2D array, got 1D array instead:
array=[-0.4 -0.9 -0.1 -0.3 -0.4 -0.5 -0.2 -0.2 -0.8 -0.3 -0.2 -0.8 -1.2 -0.3
-0.5 -0.2 -0.5 -0.4 -0.6 0. -0.3 -0.5 -0.2 -0.1 -0.3 -0.3 -0.4 -0.1
-0.2 -0.1 -0.2 -0.2 -0.4 -0.2 -0.4 -0.3 -0.1 -0.2 -0.1 -0.1 -0.3 -0.1
-0.1 -0.2 -0.2 -0.2 -0.4 -0.1 -0.1 -0.1 -0.1 -0.1 0. -0.1 -0.2 -0.2
-0.1 -0.1 0. -0.1 -0.1 -0.1 -0.2 0. 0. -0.1 -0.1 0. 0. 0.
0. 0. 0. 0. 0.1 0.1 0. 0. 0.1 0.1 0. 0.1 0.1 0.
0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.2 0.1 0.1 0.1 0. 0.1 0.1
0.1 0.2 0.1 0.2 0. 0.1 0.1 0.2 0.2 0.3 0.2 0.2 0.2 0.3
0. 0.2 0.1 0.2 0.1 0.2 0.2 0.2 0.2 0.2 0.1 0.3 0.3 0.1
0.3 0.4 0.2 0.5 0.1 0.4 0.5 0.2 0.5 0.1 0.1 0.2 0.2 0.3
0.5 0.4 0.6 0.6 0.6 0.4 0.3 0.6 0.4 0.4 0.4 0.2 0.1 0.3
0.4 0.3 0.7 0.1 0.4 0.4 0.6 0.7 0.4 0.5 0.1 0.3 0.5 0.2
0.2 0.4 0.7 0.5 0.7 0.8 0.2 0.5 0.7 0.8 0.5 1. 1. 0.8
0.7 0.8 0.8 1. 0.9 0.8 1.1 0.7 0.8 0.9 0.9 0.5 1. 0.7
0.9 1. 0.1 0.6 0.6 1.1 1. 0.2 0.8 0.7 1.1 1. 0.7 1.
0.8 1.2 0.6 0.9 0.9 1. 1.2 1.2 0.6 1.2 0.7 0.8 0.8 0.1
1.3 0.8 1. 0.9 1.1 0.9 1.2 1.7 1.2 0.1 1.2 0.3 0.4 0.9
1.2 0.8 1.1 1.9 0.3 1.1 1.2 0.9 0.7 1.8 1.4 1.5 1.8 1.
1.8 0.3 1.2 1.7 1.2 1.9 0.3 1. 0.7 0.8 1.5 0.4 2.1 1.3
2.1 1.6 1.9 1.6 0.8 1.5 1.8 0.9 0.6 0.9 1.5 1.5 2.2 2.1
1.5 2.3 1.3 2. 2. 2.6 0.8 1.2 2.1 0.6 2.7 0.7 2.6 1.9
2. 1. 2.2 0.8 2. 1.6 1.7 1.3 2.5 0.9 0.5 3.1 2.7 2.4
2.1 1.9 0.7 1.1 1.3 1.9 2.3 2.4 1.6 3.1 2.5 1.5 2.3 1.
1.9 2.2 3.5 1.3 3.3 3.3 3. 1.9 3.3 2.3 1.3 2. 2.9 3.4
2.7 1.3 4.2 4.2 3.6 5.1 4.7 0.8 4.8 1.5 3.3 4.7 4.4 5.4
4.1 5.7 5.1 4. 7.2 4.4 8.6 8.1].
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.
I'm not sure how to reshape the code. I've tried the following, but I'm left with the same error:
(X, y).reshape(-1, 1)
I also don't know why the output only shows the values of X and not also y.
Comments
Post a Comment