I'm trying to implement Locally reweighed least square and I'm getting this
b = np.array([np.sum(weights.T.dot(y_train.T)), np.sum(weights.T.dot(y_train.T).dot(x_train))])
ValueError: shapes (14,) and (404,14) not aligned: 14 (dim 0) != 404 (dim 0)
I have tried transposing but it's still not working x_train is a (404, 14) matrix and y has shape (404,) and test_datum has shape (14, 1)
def LRLS(test_datum, x_train, y_train, tau):
'''
Given a test datum, it returns its prediction based on locally weighted regression
Input: test_datum is a dx1 test vector
x_train is the N_train x d design matrix
y_train is the N_train x 1 targets vector
tau is the local reweighting parameter
output is y_hat the prediction on test_datum
'''[formulas][1]
m = len(x_train)
yest = np.zeros(m)
# Initializing all weights from the bell shape kernel function
w = np.array([np.exp(- (x_train - x_train[i]) ** 2 / (2 * tau**2)) for i in range(m)])
# Looping through all x-points
for i in range(m):
weights = w[:, i]
b = np.array([np.sum(weights.T.dot(y_train.T)), np.sum(weights.T.dot(y_train.T).dot(x_train))])
A = np.array([[np.sum(weights), np.sum(weights * x_train)], [np.sum(weights * x_train), np.sum(weights * x_train * x_train)]])
theta = np.linalg.solve(A, b)
yest[i] = theta[0] + theta[1] * test_datum[i]
return yest
I'm working with boston housing dataset and loading it this way
from sklearn.datasets import load_boston
boston = load_boston()
x = boston['data']
N = x.shape[0]
x = np.concatenate((np.ones((506,1)),x),axis=1) # add constant one feature - no bias needed
d = x.shape[1]
y = boston['target']
np.dot(A,B)
the last dim ofA
must match the 2nd to the last ofB
. The(14,) and (404,14)
arrays are in the wrong order. You many need to review thenp.dot
docs.