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 !

Mr. T
  • 11,960
  • 10
  • 32
  • 54
ricksant
  • 117
  • 9
  • ValueError Traceback (most recent call last) in () 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. – ricksant May 31 '18 at 19:54
  • Your question will probably attract more answers, when you provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Mr. T May 31 '18 at 20:31

0 Answers0