To get the optimal value of Theta in logistic regression I was using optimize.minimize() function, and my function costFunction(X,y,theta) returns the cost and gradient given the value of X,y and theta. I've checked my function costFunction() with an initial value of theta and it's working fine. But on referencing this function in optimize.minimize() it reports value error.
Here are my codes for costFunction and where i call the function 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)```