Getting a value error while implementing logically weighted regression

Hi
for this challenge I have applied logically weighted regression for predictions but I’m getting a value error" ValueError: operands could not be broadcast together with shapes (1,6) (1,2)" while I’m executing my code. I have tried my best to find out error in the program but unable to fix it. So I’m sharing entire code with you, help me out to fix the error. Thank You
Following is my code:

import numpy as np
import pandas as pd

#loading data
dfx=pd.read_csv(’./Train/Train.csv’)
dfx_test=pd.read_csv(’./Test/Test.csv’)

X=dfx.values[:,0:5] #xtrain
Y=dfx.values[:,5].reshape(X.shape[0],1) #ytrain

X_test=dfx_test.values

X=(X-X.mean())/X.std() #normalise

#logically weighted regression
def getW(query_point,X,tau):
M=X.shape[0]
W=np.mat(np.eye(M))

for i in range(M):
    xi=X[i]
    x=query_point
    W[i,i]=np.exp(np.dot((xi-x),(xi-x).T)/(-2*tau*tau)) 
    
return W  

def predict(X,Y,query_x,tau):
X=np.mat(X)
Y=np.mat(Y)

M=X.shape[0]
ones=np.ones((M,1))
X_=np.hstack((X,ones))  

qx=np.mat([query_x,1])   

W=getW(qx,X_,tau)


theta=np.linalg.pinv(X_.T*(W*X_)) * (X_.T*(W*Y))  

#print(theta.shape)

pred=np.dot(qx,theta) 

return theta,pred

examples=X_test.shape[0]
Submission=[]
for i in range(examples):
theta,pred=predict(X,Y,X_test[i],0.1)
submission.append(pred)

I have applied locally weighted regression***

hey @pasta ,

this line is making the query point as of shape (1,2) but you need to it be as ( 1,6 ) , and hence it raises a shape error when you try X[i] - query_point in getW().
so instead of np.mat , run
qx=np.hstack((query_x,1))
It will work perfectly.

I hope this might have helped you.
Thank You and Happy Learning :slightly_smiling_face:.

ok I have got it, program executed properly … thank you

hey @pasta ,
Its really good that your doubt is resolved.
I would request you to kindly mark this doubt as resolved and do provide your valuable feedback to help us improve this platform.

Thank You and Happy Learning :slightly_smiling_face:.