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 19 in Machine Learning by Alessandro
• 120 points
403 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 by MD
• 56,140 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 in Machine Learning by sairam
• 160 points

edited Aug 27 by MD 230 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 1 day ago in Machine Learning by MD
• 56,140 points
16 views
0 votes
1 answer
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,352 views
0 votes
0 answers
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 in Machine Learning by MD
• 56,140 points
1,306 views
+1 vote
1 answer
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 in Machine Learning by MD
• 56,140 points
690 views