Reputation: 1735
As you can infer from above, when I pass series of data as argument to Linear regression model , it throws an error.
ValueError: Expected 2D array, got 1D array instead:
array=[2600 3000 3200 3600 4000].
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.
Upvotes: 1
Views: 270
Reputation: 46888
The function requires you to input an array of shape (n1,n2), if you check the vignette:
X: {array-like, sparse matrix} of shape (n_samples, n_features) Training data
y: array-like of shape (n_samples,) or (n_samples, n_targets) Target values. Will be cast to X’s dtype if necessary
Even when you have a pandas Series, the shape of this object is (n,):
import numpy as np
from sklearn.linear_model import LinearRegression
X = pd.Series(np.random.randn(100))
y = pd.Series(np.random.randn(100))
LinearRegression().fit(X, y)
ValueError: Expected 2D array, got 1D array instead:
X.shape
#(100,)
You can convert them to a dataframe or convert to numpy array with the correct shape:
reg = LinearRegression().fit(pd.DataFrame(X), y)
or:
reg = LinearRegression().fit(np.array(X).reshape(-1,1),y)
Upvotes: 1