title: scipy.optimize.fmin_cg function need two callable function f and fprime, how could I extract two function from a function that return both two values?
body: <pre><code> def linear_regression(theta, X, y, lamb):
      # X(12,1+1)  theta(2,1) y(12,1)
      m = X.shape[0]
      ones = np.ones([m, 1])
      X = np.hstack([ones, X])
      h = X.dot(theta)
      # cost function
      J = 1 / 2 / m * np.sum(np.power(h - y, 2)) + lamb / 2 / m * np.sum(np.power(theta[1:], 2))
      # gradient   X(12,2)  X.T(2,12) (h-y)(12,1)   sum_error(2,1)\
      sum_error = 1 / m * X.T.dot(h - y)
      temp = theta
      temp[0] = 0
      gradient = sum_error + lamb / m * temp
      return J, gradient
  
  
  
  
  def f(theta, X, y, lamb):
      J, gradient = linear_regression(theta, X, y, lamb)
      return J
  
  
  def fprime(theta, X, y, lamb):
      J, gradient = linear_regression(theta, X, y, lamb)
      return gradient
  
  
  
  J, gradient = linear_regression(theta,X,y,1)
  # theta need to be a vector not matrix
  result = opt.fmin_cg(f, theta, fprime=fprime, args=(X,y,1))
  print(result[0])
  </code></pre>
  <p>Explain:</p>
  <ol>
  <li>opt.fmin_cg(f, theta, fprime=fprime, args=(X,y,1))  need a callable function f and fprime</li>
  <li>f, fprime is the return value of linear_regression(theta, X, y, lamb)</li>
  <li>It is easy to compute the cost function and gradient in the same function</li>
  </ol>
  <p>Question:</p>
  <ol>
  <li>Is there an easy way to extract two callable function from linear_regression(theta, X, y, lamb)</li>
  <li>calling J, gradient = linear_regression(theta,X,y,1) and pass to opt.fmin_cg(J, theta, fprime=gradient , args=(X,y,1)) is not work</li>
  </ol>