valueerror: found input variables with inconsistent numbers of samples: [40, 10]

0 votes

Here I was creating a model based on multiple linear regression, but now I stuck with an error that is " value error: found input variables with inconsistent numbers of samples: [40, 10]

here I am showing you my code.

I am confused, where should I make changes?

in case of assigning the dependent and independent variable to x and y. When I checked y.shape it gives one-dimensional array instead of this it should show two-dimensional array. You can see the whole code as I have given below and screenshots.

import pandas as pd
import numpy as np
import matplotlib.pyplot as mt
dataset = pd.read_csv("50_Startups.csv")
x = dataset.iloc[ :, :-1].values
y = dataset.iloc[ :, 4].values
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer( transformers = [('encoder', OneHotEncoder(),[3])], remainder = 'passthrough')
x = np.array(ct.fit_transform(x))
from sklearn.model_selection import train_test_split
x_train, y_train, x_test, y_test = train_test_split( x, y, test_size = 0.2, random_state = 0 )
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit( x_train, y_train )
Jul 20 in Machine Learning by Monish
• 120 points

edited Jul 21 by MD 641 views

2 answers to this question.

0 votes

Hi@Monish,

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 21 by MD
• 56,140 points
but my x is not 1D ...in my code y is showing it is 1D.....

Hi,

You misplaced in your split section. You have written this below line.

x_train, y_train, x_test, y_test = train_test_split( x, y, test_size = 0.2, random_state = 0 )

But it should be written as given below.

x_train,x_test,y_tarin, y_test = train_test_split( x, y, test_size = 0.2, random_state = 0 )
okk..i made changes as u said...i change the sequence while train test split...but still it showing same problem....
Check once again. I tried with the same code with the same dataset. It is working fine.
0 votes
keep random_state =42 in train_test_split module
answered Aug 27 by sairam
• 160 points
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
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

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
0 votes
1 answer

problem with Found input variables with inconsistent numbers of samples: [1204, 134]

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

answered Jul 20 in Machine Learning by MD
• 56,140 points
403 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,305 views
+1 vote
1 answer
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