有三个ndarray: X (100,3) y (100,1) theta(3,) theta是初始参数([0,0,0])
我的损失函数为
def Cost_Function(theta, X, y):
return np.mean(-y * np.log(sigmoid(X @ theta)) - (1 - y) * np.log(1 - sigmoid(X @ theta)))
梯度函数
def gradient(theta, X, y):
return (1 / len(X)) * X.T @ (sigmoid(X @ theta) - y)
我试图用scipy.optimize 的minimize函数来拟合求theta
res = opt.minimize(fun=Cost_Function, x0=theta, args=(X, y), method='Newton-CG', jac=gradient)
报错1:The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
改为x0=theta.all()后,报错2:matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 3)