problem with Found input variables with inconsistent numbers of samples 1204 134

0 votes
import pandas as pd
import numpy as np
import sklearn
from sklearn import linear_model
from sklearn.utils import shuffle
data = pd.read_csv("insurance.csv")
data = data[["age","children"]]

predict = "age"
x = np.array(data.drop([predict],1))
y = np.array(data[predict])
x_train , x_test , Y_train , y_test = sklearn.model_selection.train_test_split(x,y, test_size=0.1)
linear = linear_model.LinearRegression()
linear.fit(x_train , y_test)
acc = linear.score(x_test , y_test)
print(acc)
print('co: \n' ,linear.coef_)
print('intercept: \n' ,linear.intercept_)
predictions = linear.predict(x_test)
for x in range(len(predictions)):
    print(predictions[x] , x_test[x] , y_test[x])
Traceback (most recent call last):
  File "C:/Users/Alessandro/PycharmProjects/proggetti/machineLearning_2.py", line 17, in <module>
    linear.fit(x_train , y_test)
  File "C:\Users\Alessandro\anaconda3\envs\tensor\lib\site-packages\sklearn\linear_model\_base.py", line 506, in fit
    y_numeric=True, multi_output=True)
  File "C:\Users\Alessandro\anaconda3\envs\tensor\lib\site-packages\sklearn\base.py", line 432, in _validate_data
    X, y = check_X_y(X, y, **check_params)
  File "C:\Users\Alessandro\anaconda3\envs\tensor\lib\site-packages\sklearn\utils\validation.py", line 73, in inner_f
    return f(**kwargs)
  File "C:\Users\Alessandro\anaconda3\envs\tensor\lib\site-packages\sklearn\utils\validation.py", line 813, in check_X_y
    check_consistent_length(X, y)
  File "C:\Users\Alessandro\anaconda3\envs\tensor\lib\site-packages\sklearn\utils\validation.py", line 257, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [1204, 134]

Jul 20, 2020 in Machine Learning by Alessandro
• 120 points
5,192 views

1 answer to this question.

0 votes

Hi@Alessandro,

Here you used x as your feature parameter and y as your predictor. But your feature parameter should not be 1D. So check the shape of x and if it is 1D, then convert it from 1D to 2D.

$ x.shape
$ x.reshape(-1,1)

Hope this will help you.

answered Jul 20, 2020 by MD
• 95,460 points
in train_test_split(x,y,test_Size=0.2,random_state=42)then it can work
Hi@sairam,

I don't think so. Either you specify random_state or not, it will work. This has a different meaning. If you specify then it will take random value from your dataset.

Related Questions In Machine Learning

0 votes
2 answers
0 votes
0 answers

ValueError: Found input variables with inconsistent numbers of samples: [11, 3988]

After reshaping also I am getting the ...READ MORE

Aug 27, 2020 in Machine Learning by sairam
• 160 points

edited Aug 27, 2020 by MD 5,329 views
0 votes
1 answer

ValueError: Found input variables with inconsistent numbers of samples: [616, 308]

Hi@Alshwarya, I think there is a problem with your ...READ MORE

answered Sep 23, 2020 in Machine Learning by MD
• 95,460 points
4,316 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,470 views
0 votes
1 answer
0 votes
1 answer

ImportError: cannot import name 'LinearRegression' from 'sklearn'

Hi@akhtar, You got this error because LinearRegression is ...READ MORE

answered Apr 24, 2020 in Machine Learning by MD
• 95,460 points
24,970 views
+1 vote
2 answers

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

Hi@akhtar, Here you used x as your feature ...READ MORE

answered Apr 14, 2020 in Machine Learning by MD
• 95,460 points

edited Aug 11, 2021 by Soumya 57,982 views
0 votes
1 answer

Found input variables with inconsistent numbers of samples:

Hi@sagar, You have converted your Dataframe into an ...READ MORE

answered Jul 13, 2020 in Machine Learning by MD
• 95,460 points
5,328 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP