Search code examples
machine-learningscikit-learnneural-networklinear-regressiontraining-data

Found input variables with inconsistent numbers of samples: [3, 1509]


I'm trying to pass y_train and u_train data to sklearn's LinearRegression.fit() function as follows:

LinearRegression.fit(u_train, y_train)

Both u_train and y_train have sizes of (503,3) (see below)

enter image description here

I don't understand why I am getting a dimension mismatch error since u_train and y_train are the same size. I get the error "Found input variables with inconsistent numbers of samples: [3, 1509]." Any help is greatly appreciated.


Solution

  • It might be that you're invoking the class rather than an instance. Usage should be like:

    from sklearn.linear_model import LinearRegression
    
    #Make new instance
    linear_reg = LinearRegression()
    #Fit
    linear_reg.fit(u_train, y_train)
    
    #One-liner alternative
    linear_reg = LinearRegression().fit(u_train, y_train)