Scipy优化fmin ValueError:使用序列设置数组元素(2)

时间:2018-05-31 17:52:56

标签: python arrays numpy scipy

我对这个错误做了一些研究。我发现了一些东西 Scipy optimize fmin ValueError: setting an array element with a sequence

我有同样的问题。我正在尝试return J[0,0],gradreturn J[0],grad,但没有其他选择正在发挥作用。任何线索?
PS:我是Python和scipy的新手。

提前致谢

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) 

我收到以下错误消息:

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

顺便说一句,我从帖子Scipy optimize fmin ValueError: setting an array element with a sequence收到同样的消息 解决方案J[0][0]J[0]来自那里!

0 个答案:

没有答案