简体   繁体   中英

How fix the error that I got in linear regression in scikit-learn

I am new in the concept of linear regression in Python. I am using the linear regression in scikit-learn to find the predicted value of y, here it is called y_new. The below code is what I have scripted so far:

import numpy as np 
#creating data for the run
x=spendings = np.linspace(0,5,4000)
y=sales = np.linspace(0,0.5,4000)
#defining the training function
def train(x,y):
    from sklearn.linear_model import LinearRegression
    model = LinearRegression().fit(x,y)
    return model 
model = train(x,y)
x_new = 23.0
y_new = model.predict([[x_new]])
print(y_new)

I can't get the value of the y_new due to this error message:

Expected 2D array, got 1D array instead:
array=[0.00000000e+00 1.25031258e-03 2.50062516e-03 ... 4.99749937e+00
 4.99874969e+00 5.00000000e+00].
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. 

According to the documentation of LinearRegression fit method expects X and y input to be (n_samples, n_features) shape.

if you check your x and y shapes,it is like this

x=spendings = np.linspace(0,5,4000)
y=sales = np.linspace(0,0.5,4000)
print(x.shape)
print(y.shape)

(4000,)
(4000,)

what error says,you need to reshape your x and y to shape (n_samples, n_features) using arr.reshape(-1,1) . so what you need is reshape your x and y before fit to the LinearRegression.

x = x.reshape(-1,1)
y = y.reshape(-1,1)
print(x.shape)
print(y.shape)
(4000, 1)
(4000, 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM