Scipy optimize function: matrixes not aligned
<p>EDIT: The data set is the MNIST data set from the Homework of Week 4 of Andrew Ng's Machine Learning Course</p>

<p>I've checked the <a href="https://stackoverflow.com/questions/8752169/matrices-are-not-aligned-error-python-scipy-fmin-bfgs">question on scipy optimize</a> but I still couldn't figure out what is wrong with my code. I am trying to optimize theta for the oneVsAll question on the Andrew Ng coursera course.</p>

<p>Here is the relevant code</p>

<pre><code>def sigmoid(x):
a = []
for item in x:
    a.append(1/(1+math.exp(-item)))
return a

def hypothesis(x, theta):
    return np.array(sigmoid(np.dot(x, theta)))

def costFunction(theta, x, y, lamba_):
    m = X.shape[0]

    part1 = np.dot(y.T, np.log(hypothesis(x, theta)).reshape(m,1))
    part2 = np.dot((np.ones((m,1)) - y).T, np.log( 1 - hypothesis(x, theta)).reshape(m,1))

    summ = (part1 + part2)

    return -summ[0]/m

def gradientVect(theta, x, y, lambda_):
    n = X.shape[1]
    m = X.shape[0]
    gradient = []

    theta = theta.reshape(n,1)

    beta = hypothesis(x, theta) - y

    reg = theta[1:] * lambda_/m

    grad = np.dot(X.T, beta) * 1./m

    grad[1:] = grad[1:] * reg

    return grad.flatten()


from scipy import optimize

def optimizeTheta(x, y, nLabels, lambda_):

    for i in np.arange(0, nLabels):
        theta = np.zeros((n,1))
        res = optimize.minimize(costFunction, theta, args=(x, (y == i)*1, lambda_), method=None,
                       jac=gradientVect, options={'maxiter':50})
        print(res)
    return result
</code></pre>

<p>but running</p>

<pre><code>optimizeTheta(X, y, 10, 0) # X shape = 401, 500
</code></pre>

<p>Gives me the following error:</p>

<pre><code>---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-247-e0e6e4c1eddd&gt; in &lt;module&gt;()
      3 n = X.shape[1]
      4 
----&gt; 5 optimizeTheta(X, y, 10, 0)

&lt;ipython-input-246-0a15e9f4769a&gt; in optimizeTheta(x, y, nLabels, lambda_)
     54         theta = np.zeros((n,1))
     55         res = optimize.minimize(costFunction, x0 = theta, args=(x, (y == i)*1, lambda_), method=None,
---&gt; 56                        jac=gradientVect, options={'maxiter':50})
     57         print(res)
     58     return result

//anaconda/lib/python3.5/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    439         return _minimize_cg(fun, x0, args, jac, callback, **options)
    440     elif meth == 'bfgs':
--&gt; 441         return _minimize_bfgs(fun, x0, args, jac, callback, **options)
    442     elif meth == 'newton-cg':
    443         return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,

//anaconda/lib/python3.5/site-packages/scipy/optimize/optimize.py in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
    859     gnorm = vecnorm(gfk, ord=norm)
    860     while (gnorm &gt; gtol) and (k &lt; maxiter):
--&gt; 861         pk = -numpy.dot(Hk, gfk)
    862         try:
    863             alpha_k, fc, gc, old_fval, old_old_fval, gfkp1 = \

ValueError: shapes (401,401) and (2005000,) not aligned: 401 (dim 1) != 2005000 (dim 0)
</code></pre>

<p>And I can't figure out why the shapes are not aligned.</p>

<p>Thanks!</p>