Ahkam Mohammed
Ahkam Mohammed

Reputation: 61

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

I am solving a machine learning problem using python's sklearn library

I am using pandas dataframe and i wanted to train a linear regression model using my local data and predict new values. here are my code samples.

customers= pd.read_csv('Ecommerce Customers')
X= customers[['Avg. Session Length', 'Time on App','Time on Website', 'Length of Membership']]
y=['Yearly Amount Spent']

when i try to run this below code

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)

it gives me an error

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

in my dataset it has 500 rows and 8 columns sklearn verion is

import sklearn
format(sklearn.__version__)
'0.20.1'

pls help me . Thanks In advance

Upvotes: 1

Views: 1841

Answers (1)

desertnaut
desertnaut

Reputation: 60321

Looking closely at your code, you don't take y to be a column of your dataframe customers, as you probably intend to do; as you have it

y=['Yearly Amount Spent']

y is just a 1-element list:

y
# ['Yearly Amount Spent']

hence scikit-learn justifiably complains that the length of your labels y is just 1.

Change it to

y=customers['Yearly Amount Spent']

Upvotes: 6

Related Questions