ValueError: need more than 3 values to unpack when using optimize.minimize
<p>I'm pretty new to python and I got stuck on this:
I'd like to use <code>scipy.optimize.minimize</code> to maximize a function and I'm having some problem with the extra arguments of the function I defined.</p>

<p>I looked for a solution in tons of answered questions but I can't find anything that solves my problem.
I saw in <a href="http://stackoverflow.com/questions/19843752/structure-of-inputs-to-scipy-minimize-function">Structure of inputs to scipy minimize function</a> how to pass extra arguments that one wants to be constant in the minimization of the function and my code seems fine to me from this point of view.</p>

<p>This is my code:</p>

<pre><code>import numpy as np
from scipy.stats import pearsonr
import scipy.optimize as optimize

def min_pears_function(a,exp):
    (b,c,d,e)=a
    return (1-(pearsonr(b + exp[0] * c + exp[1] * d + exp[2],e)[0]))


a = (log_x,log_y,log_t,log_z) # where log_x, log_y, log_t and log_z are numpy arrays with same length

guess_PF=[0.6,2.0,0.2]

res = optimize.minimize(min_pears_function, guess_PF, args=(a,), options={'xtol': 1e-8, 'disp': True})
</code></pre>

<p>When running the code I get the following error:</p>

<blockquote>
  <p>ValueError: need more than 3 values to unpack</p>
</blockquote>

<p>But I can't see what needed argument I'm missing. The function seems to work fine, so I guess the problem is in <code>optimize.minimize</code> call?</p>

<p>Your error occurs here:</p>

<pre><code>def min_pears_function(a,exp):
    # XXX: This is your error line
    (b,c,d,e)=a
    return (1-(pearsonr(b + exp[0] * c + exp[1] * d + exp[2],e)[0]))
</code></pre>

<p>This is because:</p>

<ul>
<li>the initial value you pass to <code>optimize.minimize</code> is <code>guessPF</code> which has just three values (<code>[0.6,2.0,0.2]</code>). </li>
<li>this initial value is passed to <code>min_pears_function</code> as the variable <code>a</code>.  </li>
</ul>

<p>Did you mean for it to be passed as <code>exp</code>?  Is it <code>exp</code> you wish to solve for?  In that case, redefine the signature as:</p>

<pre><code>def min_pears_function(exp, a):
    ...
</code></pre>

python