Logistic回归和numpy:ValueError:操作数不能与形状一起广播

时间:2019-02-24 19:19:16

标签: python numpy logistic-regression numpy-broadcasting

这里是机器学习的初学者。
在python 3.7中,尝试运行numpy.optimize的fmin_tnc时,我总是收到此错误。
我知道这种类型的问题已经被问过几次了,但是尽管已经检查了我的矩阵尺寸和代码几次,但是我找不到我的错误。

功能如下:

def compute_cost(theta, X, y, lambda_):
    m = len(y)
    mask = np.eye(len(theta))
    mask[0,0] = 0

    hypo = sigmoid(X @ theta)
    func = y.T @ np.log(hypo) + (1-y.T) @ np.log(1-hypo)
    cost = -1/m * func
    reg_cost = cost + lambda_/(2*m) * (mask@theta).T @ (mask@theta)

    grad = 1/m * X.T@(hypo-y) + lambda_/m * (mask@theta)

    return reg_cost.item(), grad

这是我的尺寸:

X: (118, 3)
y: (118, 1)
theta: (3, 1)

函数调用

initial_theta = np.zeros((3,1))
lambda_ = 1

thetopt, nfeval, rc = opt.fmin_tnc(
    func=compute_cost, 
    x0=initial_theta, 
    args=(X, y, 1)
)

错误。

File "<ipython-input-21-f422f885412a>", line 16, in compute_cost
    grad = 1/m * X.T@(hypo-y) + lambda_/m * (mask@theta)

ValueError: operands could not be broadcast together with shapes (3,118) (3,)

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

在scipy.optimize.tnc中,fmin_tnc函数调用_minimize_tnc,这似乎很繁琐。在此功能中,它几乎要做的第一件事(第348行)是将x​​0展平:

x0 = asfarray(x0).flatten()

因此,您需要做的是在函数中重塑它。只需将这一行添加到您的compute_cost函数的开头即可:

theta = theta.reshape((3, 1))