Чтобы получить оптимальное значение Theta в логистической регрессии, я использовал функцию optimize.minimize (), а моя функция costFunction (X, y, theta) возвращает стоимость и градиент с учетом значений X, y и theta.Я проверил мою функцию costFunction () с начальным значением тета, и она работает нормально.Но при обращении к этой функции в optimize.minimize () она сообщает об ошибке значения.
Вот мои коды для costFunction и где я вызываю функцию optimize.minimize ()
def costFunction(X,y,theta):
J = 0.0
m = Y.size
J = -1/m * np.sum(((1-y)*np.log(1-sigmoid(np.dot(X,theta))))+((y)*np.log(sigmoid(np.dot(X,theta)))))
grad = 1/m*np.dot(X.T,(sigmoid(np.dot(X,theta))-y))
return J, grad ```
#To check the function :
print(X[:,:3].shape)
J,grad = costFunction(X[:,:3],Y,theta=[0,0,0])
print(J)
print( grad)
#and this returns the following output:
(1000, 3)
0.6931471805599454
[ 0. 17.25682 5.92721]
#and here's where I call optimize.minimize() function:
options = {'maxiter' : 400}
initial_theta = np.zeros(3)
x = X[:,:3]
#res = optimize.minimize(computeCost,initial_theta,(X[:,:3],Y),jac = True,method = 'TNC',options = options)
res = optimize.minimize(costFunction,
initial_theta,
(x, Y),
jac=True,
method='TNC',
options=options)
cost = res.fun
theta = res.x
print("cost ".cost)
print("theta ".theta)
#and it returns the following error :
ValueError Traceback (most recent call last)
<ipython-input-69-55576d96c00a> in <module>
8 jac=True,
9 method='TNC',
---> 10 options=options)
11
12 cost = res.fun
~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
604 elif meth == 'tnc':
605 return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback,
--> 606 **options)
607 elif meth == 'cobyla':
608 return _minimize_cobyla(fun, x0, args, constraints, **options)
~/anaconda3/lib/python3.7/site-packages/scipy/optimize/tnc.py in _minimize_tnc(fun, x0, args, jac, bounds, eps, scale, offset, mesg_num, maxCGit, maxiter, eta, stepmx, accuracy, minfev, ftol, xtol, gtol, rescale, disp, callback, **unknown_options)
407 offset, messages, maxCGit, maxfun,
408 eta, stepmx, accuracy, fmin, ftol,
--> 409 xtol, pgtol, rescale, callback)
410
411 funv, jacv = func_and_grad(x)
~/anaconda3/lib/python3.7/site-packages/scipy/optimize/tnc.py in func_and_grad(x)
369 else:
370 def func_and_grad(x):
--> 371 f = fun(x, *args)
372 g = jac(x, *args)
373 return f, g
~/anaconda3/lib/python3.7/site-packages/scipy/optimize/optimize.py in __call__(self, x, *args)
61 def __call__(self, x, *args):
62 self.x = numpy.asarray(x).copy()
---> 63 fg = self.fun(x, *args)
64 self.jac = fg[1]
65 return fg[0]
<ipython-input-65-97115ec06e6e> in costFunction(X, y, theta)
2 J = 0.0
3 m = Y.size
----> 4 J = -1/m * np.sum(((1-y)*np.log(1-sigmoid(np.dot(X,theta))))+((y)*np.log(sigmoid(np.dot(X,theta)))))
5 grad = 1/m*np.dot(X.T,(sigmoid(np.dot(X,theta))-y))
6 return J, grad
ValueError: shapes (3,) and (1000,) not aligned: 3 (dim 0) != 1000 (dim 0)```