你能帮我使用 optimize.fmin 进行逻辑回归吗?下面是我的代码,其中 X_1 是 (100, 3) 矩阵 y 是 (100, 1) 矩阵
def cost(theta):
m = len(X_1)
cost1 = (y.T) * numpy.log(1/(1+numpy.exp(-X_1*theta)))
cost2 = ((1-y).T) * numpy.log(1-1/(1+numpy.exp(-X_1*theta)))
J = -(1/m) * (cost1 + cost2)
return J
fmin(func = cost, x0 = numpy.zeros((3, 1)))
这里是错误(我只选择了箭头所指的错误,我不能把整个东西放在这里)。
ValueError Traceback (most recent call last)
<ipython-input-247-4a2362053322> in <module>
---> 10 fmin(func = cost, x0 = numpy.zeros((3, 1)))
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in fmin(func, x0, args, xtol, ftol, maxiter, maxfun, full_output, disp, retall, callback, initial_simplex)
--> 443 res = _minimize_neldermead(func, x0, args, callback=callback, **opts)
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in _minimize_neldermead(func, x0, args, callback, maxiter, maxfev, disp, return_all, initial_simplex, xatol, fatol, adaptive, **unknown_options)
--> 586 fsim[k] = func(sim[k])
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
--> 327 return function(*(wrapper_args + args))
<ipython-input-247-4a2362053322> in cost(theta)
----> 4 cost1 = (y.T) * numpy.log(1/(1+numpy.exp(-(X_1*theta))))
/usr/lib/python3/dist-packages/numpy/matrixlib/defmatrix.py in __mul__(self, other)
--> 220 return N.dot(self, asmatrix(other))
<__array_function__ internals> in dot(*args, **kwargs)
ValueError: shapes (100,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
谢谢。