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 21, 2020 in Machine Learning by Monish
• 120 points

edited Jul 21, 2020 by MD 14,464 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.

Ready to dive into the world of machine learning? Supercharge your career and join our comprehensive Machine Learning Course today!

answered Jul 21, 2020 by MD
• 95,440 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.
yes you figure it out correctly thank you for help!
0 votes
keep random_state =42 in train_test_split module
answered Aug 27, 2020 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, 2020 in Machine Learning by sairam
• 160 points

edited Aug 27, 2020 by MD 5,022 views
0 votes
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, 2020 in Machine Learning by MD
• 95,440 points
5,011 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, 2020 in Machine Learning by MD
• 95,440 points
4,577 views
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,023 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,440 points
22,987 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,440 points

edited Aug 11, 2021 by Soumya 57,084 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,440 points
3,884 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