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)