title: Minimise a function in Python
body: <p>Could you help on how to minimize a function in Python? Particularly, it's the logistic function. Below is my cost function, I got it correctly because I checked the answer.</p>
  <pre><code>def g(z):
  
    h = 1 / (1 + numpy.exp(-z))
    return h
  
  theta1_ravel = theta1.ravel().T
  theta2_ravel = theta2.ravel().T
  
  theta_conca = numpy.concatenate((theta1_ravel, theta2_ravel))
  lambada = 1
  
  def computecost(theta_conca):
  
    theta1 = numpy.reshape(theta_conca[0:10025], (25, 401))
    theta2 = numpy.reshape(theta_conca[10025:10285], (10, 26))
  
    a1 = X_1
    a2 = g(X_1 * theta1.T) # (5000, 25)
    a2 = numpy.column_stack((numpy.ones((m, 1)), a2))
    a3 = g(a2 * theta2.T) # (5000, 10)
  
    reg_term = (lambada / (2*m)) * (numpy.sum(numpy.power(theta1[:, 1:], 2)) + numpy.sum(numpy.power(theta2[:, 1:], 2)))
  
    J = -(1/m) * (numpy.sum(numpy.multiply(Y, numpy.log(a3)) + numpy.multiply((1 - Y), numpy.log(1 - a3)))) + reg_term
  
    return J
  
  J = computecost(theta_conca)
  J
  </code></pre>
  <p>And then I try to minimize it, get theta by using &quot;scipy.optimize.minimize&quot; but it's not working.</p>
  <pre><code>theta_random = numpy.random.rand(m, 1)
  theta_random
  scipy.optimize.minimize(computecost, theta_random)
  </code></pre>
  <p>This is the error notification:</p>
  <pre><code>ValueError                                Traceback (most recent call last)
  &lt;ipython-input-36-05051c7a9e1f&gt; in &lt;module&gt;
        1 theta_random = numpy.random.rand(m, 1)
        2 theta_random
  ----&gt; 3 scipy.optimize.minimize(computecost, theta_random)
  
  /usr/lib/python3/dist-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
      592         return _minimize_cg(fun, x0, args, jac, callback, **options)
      593     elif meth == 'bfgs':
  --&gt; 594         return _minimize_bfgs(fun, x0, args, jac, callback, **options)
      595     elif meth == 'newton-cg':
      596         return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
  
  /usr/lib/python3/dist-packages/scipy/optimize/optimize.py in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
      996     else:
      997         grad_calls, myfprime = wrap_function(fprime, args)
  --&gt; 998     gfk = myfprime(x0)
      999     k = 0
     1000     N = len(x0)
  
  /usr/lib/python3/dist-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
      325     def function_wrapper(*wrapper_args):
      326         ncalls[0] += 1
  --&gt; 327         return function(*(wrapper_args + args))
      328 
      329     return ncalls, function_wrapper
  
  /usr/lib/python3/dist-packages/scipy/optimize/optimize.py in approx_fprime(xk, f, epsilon, *args)
      755 
      756     &quot;&quot;&quot;
  --&gt; 757     return _approx_fprime_helper(xk, f, epsilon, args=args)
      758 
      759 
  
  /usr/lib/python3/dist-packages/scipy/optimize/optimize.py in _approx_fprime_helper(xk, f, epsilon, args, f0)
      689     &quot;&quot;&quot;
      690     if f0 is None:
  --&gt; 691         f0 = f(*((xk,) + args))
      692     grad = numpy.zeros((len(xk),), float)
      693     ei = numpy.zeros((len(xk),), float)
  
  /usr/lib/python3/dist-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
      325     def function_wrapper(*wrapper_args):
      326         ncalls[0] += 1
  --&gt; 327         return function(*(wrapper_args + args))
      328 
      329     return ncalls, function_wrapper
  
  &lt;ipython-input-33-3761d25f71af&gt; in computecost(theta_conca)
       14 def computecost(theta_conca):
       15 
  ---&gt; 16     theta1 = numpy.reshape(theta_conca[0:10025], (25, 401))
       17     theta2 = numpy.reshape(theta_conca[10025:10285], (10, 26))
       18 
  
  &lt;__array_function__ internals&gt; in reshape(*args, **kwargs)
  
  /usr/lib/python3/dist-packages/numpy/core/fromnumeric.py in reshape(a, newshape, order)
      299            [5, 6]])
      300     &quot;&quot;&quot;
  --&gt; 301     return _wrapfunc(a, 'reshape', newshape, order=order)
      302 
      303 
  
  /usr/lib/python3/dist-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
       59 
       60     try:
  ---&gt; 61         return bound(*args, **kwds)
       62     except TypeError:
       63         # A TypeError occurs if the object does have such a method in its
  
  ValueError: cannot reshape array of size 5000 into shape (25,401)
  </code></pre>
  <p>How can I implement it? I'm sorry that the code looks like a mess but I think I shouldn't cut off something. I'll give you the entire code and data if you need to try on your device.</p>
  <p>Thank you.</p>