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]

3
Answers

Replies

I faced a similar problem while fitting a regression model . The problem in my case was, Number of rows in X was not equal to number of rows in y. You likely get problems because you remove rows containing nulls in X_train and y_train independent of each other. y_train probably has few, or no nulls and X_train probably has some. So when you remove a row in X_train and the same row is not removed in y_train it will cause your data to be unsynced and have different lenghts. Instead you should remove nulls before you separate X and y.


 

 

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)

 
 

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.
Protected by Astra Security