Home > Software engineering >  why sklearn LinearRegression model does not accept one dimensional data?
why sklearn LinearRegression model does not accept one dimensional data?

Time:02-17

I'm trying to learn the basics of Linear Regression.

I tried to build the simplest model with the simplest data for the starter .

for data I had:

## Data (Apple stock prices)
   apple = np.array([155, 160, 165])
   days = np.array([1, 2, 3])

the X would be the days and y would be the apple stock price.

I try to build the model with a one-liner :

model = LinearRegression().fit(X=days,y=apple)

Then I get the error that says, the model expects 2d data as input. but "why" ? both the X and y, in this case, the number of days and the stock prices for the apple, are one dimensional. why it should be converted into a 2d array?

CodePudding user response:

The input is an array of size (nxm) with m being the number of x variables. in your case m=1, so you need an array of (3x1). your current input is (3,). try:

days = np.array([1, 2, 3]).reshape(-1,1)

CodePudding user response:

The model was created to support both 1D and 2D data, if the input shape was 1D, 2D won't be supported, but when the input shape is 2D, both 2D and 1D is supported by just reshaping the 1D array to a 2D array. This is why the model was built to accept 2D arrays for flexibility. So, just reshape your data and your model will function right. Try this:

apple = np.array([155, 160, 165]).reshape(-1,1)
days = np.array([1, 2, 3]).reshape(-1,1)

model = LinearRegression().fit(X=days,y=apple)
  • Related