1

I have done some research about this error. I found something in Scipy optimize fmin ValueError: setting an array element with a sequence

I have the same problem. I am trying return J[0,0],grad or return J[0],grad but none of the alternatives are working. Any clue?
PS: I am kind of newbie in Python and scipy.

thanks in advance

def funcaoCustoRegressaoLogisticaReg(theta, X, y, lamb=0.01, returnGrad=True):
    m = len(y)
    n=len(theta)
    J = 0
    grad = np.zeros(theta.shape)
    h_theta=sigmoid(np.dot(X,theta))

    # calcula somatório ao quadrado de theta - primeiro termo
    reg = (lamb/(2*m))*(np.dot(theta.T,theta)-np.power(theta[0],2)) 
    p1 = np.dot(-y.T,(np.log(h_theta)))
    p2 = np.dot( -(1-y).T, (np.log(1-h_theta ) ))
    J = (1.0/m)*(p1+p2) +reg
    mk = np.ones(theta.shape);
    mk[0] = 0; 

    # CALCULO DO GRADIENTE
    reg_grad=(lamb/m)*(theta*mk) # primeiro elemento não é regularizado
    reg_grad=reg_grad.reshape((n,1))
    grad = (1/m)*(np.dot((h_theta - y).T,X)).T +reg_grad

    if (returnGrad==True):
        return J,grad
    return J

from scipy.optimize import fmin
from scipy.optimize import fmin_bfgs
parametros=(X_map, y, 1) 
theta = fmin(funcaoCustoRegressaoLogisticaReg, x0=initial_theta, 
args=parametros) 

I get the following error message:

ValueError Traceback (most recent call last) <ipython-input-167-536f4f074fed> in <module>() 6 7 parametros=(X_map, y, 1) ----> 8 theta = fmin(funcaoCustoRegressaoLogisticaReg, x0=initial_theta, args=parametros) ... 531 for k in range(N + 1): --> 532 fsim[k] = func(sim[k]) 533 534 ind = numpy.argsort(fsim) 
ValueError: setting an array element with a sequence

BTW, I get the same message from the post Scipy optimize fmin ValueError: setting an array element with a sequence and the solutions J[0][0] and J[0] were taken from there !

2
  • ValueError Traceback (most recent call last) <ipython-input-167-536f4f074fed> in <module>() 6 7 parametros=(X_map, y, 1) ----> 8 theta = fmin(funcaoCustoRegressaoLogisticaReg, x0=initial_theta, args=parametros) ... 531 for k in range(N + 1): --> 532 fsim[k] = func(sim[k]) 533 534 ind = numpy.argsort(fsim) ValueError: setting an array element with a sequence. Commented May 31, 2018 at 19:54
  • Your question will probably attract more answers, when you provide a Minimal, Complete, and Verifiable example Commented May 31, 2018 at 20:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.