patrick
patrick

Reputation: 11

ValueError: Found input variables with inconsistent numbers of samples: [12925, 517]

from sklearn.linear_model import LinearRegression
import numpy as np

X9=dataset.iloc[:,[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]].values
y9=dataset.iloc[:,29].values
#X9=pd.DataFrame(data=X9)
#y9=pd.DataFrame(data=y9)

X9 = X9.astype('float32')
y9 = LabelEncoder().fit_transform(y9.astype('str'))

#X9 = np.array(X9).reshape(12925,1)
#y9 = np.reshape(517,1)

X9 = X9.reshape((12925, 1))
y9 = y9.reshape((517, 1))

linreg = LinearRegression().fit(X9,y9)

linreg.intercept_

linreg.coef_

I am a beginner in python. I get the below error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-51-0f2045f4e5e6> in <module>()
     16 y9 = y9.reshape((517, 1))
     17 
---> 18 linreg = LinearRegression().fit(X9,y9)
     19 
     20 linreg.intercept_

3 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
    332         raise ValueError(
    333             "Found input variables with inconsistent numbers of samples: %r"
--> 334             % [int(l) for l in lengths]
    335         )
    336 

ValueError: Found input variables with inconsistent numbers of samples: [12925, 517]

my X9.shape is: (12925, 1) y9.shape is: (517, 1)

could you please guide me to solve this error. im trying to do The MR parameters were optimized using a least squares algorithm.

Upvotes: 0

Views: 14556

Answers (1)

Fabian
Fabian

Reputation: 802

You are having 12925 lines in your X9 and only 517 lines in your y9. They should be the same number because for every sample in X9 you would need a sample in your y9 to calculate the linear regression.

I don't have your data so I can't really reproduce and provide a proper solution.

First guess would be to recheck the shape of your dataset.

Another guess would be that you have to adjust your reshape:

X9 = X9.reshape((12925, 1))
y9 = y9.reshape((12925, 1))

Upvotes: 1

Related Questions