ValueError: Found input variables with inconsistent numbers of samples: [1, 1000]

Machine Learning

I am trying to create one Machine Learning model using LinearRegression model, but I am getting the below error.

import pandas as pd
data = pd.read_csv('db.csv')
x = data['TV']
y = data['Sales']
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x,y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/user/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/linear_model/base.py", line 512, in fit
y_numeric=True, multi_output=True)
File "/user/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py", line 531, in check_X_y
check_consistent_length(X, y)
File "/user/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py", line 181, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [1, 1000]

2
Answers

Replies

In the above, x is considered as the feature parameter and y as the predictor. You need to make sure that the feature parameter is not 1D. Hence, you will need to check the shape of X. If it is 1D, then you will need to convert it from !D to 2D.


$ x.shape


$ x.reshape(-1,1)

 

In your code, you have specified x as a feature parameter and y as a predictor. The feature parameter should not be 1D. So you have to change it to 2D.



You can check the shape of x using,



$ x.shape



You can then reshape x,



$ x.reshape(-1,1)

 
 

If you want to unleash your potential in this competitive field, please visit the Machine Learning course page for more information, where you can find the Machine Learning tutorials and Machine Learning frequently asked interview questions and answers as well.

 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.