Shape error while calculating R Square

linear_regression
pandas
mse
python

#1

Hello,
While running the code below…infact on running the last line of the code i am getting the following error

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

I have tried to convert Series to arrays and tried to reshape the arrays as well… at the same time i have tried converting arrays to series as well. But I keep on getting this error

All I am trying to do is find is R- Square on predicted values and target variable test values

train_df=pd.read_csv(“train.csv”)
test_df=pd.read_csv(“test.csv”)

print(train_df.columns)
print(train_df.info())
print(train_df.describe())

lreg=LinearRegression()

X = train_df[[“Outlet_Establishment_Year”,“Item_MRP”]]

X_train,x_cv,y_train,y_cv=train_test_split(X,train_df.Item_Outlet_Sales)
lreg.fit(X_train,y_train) # training
pred=lreg.predict(x_cv) # prediciting

Calculating MSE

mse =np.mean((y_cv-pred)**2)
print(mse)

calculating coefficients

coeff=pd.DataFrame(X_train.columns,columns=[“Features”])
coeff[“Coefficient Estimate”]=pd.Series(lreg.coef_)
print(coeff)
pred=pred.reshape(1,-1)

lreg.score(pred,y_cv)

Thanks


#2

hi @mohitlearns,

please print the first few rows of pred and y_cv .