Getting error while fitting a linear regression model

linear_regression

#1

Hello,

I am trying to run the following Linear Regression code and I am getting the error as listed in the subject line. I have tried to research on the given error but I have not been able to figure out the reason.

Error that I am getting is

shapes (1,1705) and (2,) not aligned: 1705 (dim 1) != 2 (dim 0)

code is as follows

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
from sklearn.model_selection import train_test_split
#import test and train file

from sklearn.linear_model import LinearRegression 

train = pd.read_csv('Train_Data.csv')
#test = pd.read_csv('test.csv')

lreg = LinearRegression() 

X = train.loc[:,['Outlet_Establishment_Year','Item_MRP']] # splitting into training and cv for cross validation

x_train, x_cv, y_train, y_cv = train_test_split(X,train.Item_Outlet_Sales,test_size=0.20,random_state=42)

lreg.fit(x_train,y_train) # training the model 

pred = lreg.predict(x_cv) #  predicting on cv

mse = np.mean((pred - y_cv)**2) # calculating mse
print("MSE=",mse)

# calculating coefficients 
coeff = DataFrame(x_train.columns) 
coeff['Coefficient Estimate'] = Series(lreg.coef_)
print(coeff[[0,"Coefficient Estimate"]])

y_cv_matrix=y_cv.as_matrix()

print(lreg.score(pred,y_cv_matrix)) # this line not working

I have tried to reshape and use np.newaxis as well but it doesn’t work

All the help is appreciated.

Thanks in advance

Mohit


#2

Hi mohit,

Can you print the shape of pred and y_cv_matrix ?