1
votes

i get the following error while i try to minimize the following function using CG:

def costFunctionReg(theta, X, y, labda):
    import numpy as np
    import sigmoid as sg

    grad=np.zeros((28,1),dtype=float)

    #setting predictions
    m=len(y)
    #calculating predictions using the sigmoid function
    predictions=sg.sigmoid(np.dot(X,theta))
    predictions=predictions.reshape(np.size(predictions),1)
    y=y.reshape(np.size(y),1)
    predictionsminusy=predictions-y
    #predictionsminusyr=predictionsminusy.reshape(np.size(predictionsminusy),1)
    #calculating the cost of using theta as per logistic regression equation
    logistic=(-y*np.array(np.log(predictions)))-((1-y)*np.array(np.log(1-predictions)))
    endr=np.size(theta)
    J=1./m * np.sum(logistic)+(labda/(2*m))*np.sum(theta[0:endr]**2)
    #computing partial derivatives w.r.t to parameters
    endg=np.size(grad)
    endx,end=np.shape(X)
    grad[0,:]= 1.0/m * np.sum(np.dot((X[:,0]) ,(predictionsminusy) ));

    grad[1:endg,:]=((((1./m )* (np.dot(((predictionsminusy.conj().T)), (X[:,1:end]))))).conj().T)


    return [J,grad]

I get the error:

ValueError: matrices are not aligned

/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py(1151)_minimize_cg() 1150 while (gnorm > gtol) and (k < maxiter): -> 1151 deltak = numpy.dot(gfk, gfk) 1152

1
indention counts, especially in python. please reformat your code (start each line with 4 spaces for code display) - indivisible
@andi import statements inside functions are perfectly legal in Python. Do not make semantic changes when reformatting someone else's code. - nobody
@Craig van Wilson: could you give specifications for theta, X, y, labda so I could try reproduce the error? - user3413108
@AndrewMedico : this was absolutly not intentional and I appologize for it. - user3413108

1 Answers

1
votes

Try return J, grad.flatten() rather than return J, grad