python numpy: ValueError: формы (14,) и (404,14) не выровнены: 14 (dim 0)! = 404 (dim 0) - PullRequest
0 голосов
/ 17 февраля 2020

Я пытаюсь реализовать локально пересчитанный метод наименьших квадратов, и я получаю это

 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)

Я попытался транспонировать, но он все еще не работает x_train является (404, 14) матрицей, а у имеет форму (404,) и test_datum имеют форму (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

Я работаю с набором данных для бостонского корпуса и загружаю его таким образом

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']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...