Python-Numpy逻辑回归

2024-05-15 19:47:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在python中使用 纽比。我的成本函数(CF)似乎工作正常。但是有一个 梯度计算有问题。它返回3x100数组,而 应该返回3x1。我认为(hypo-y)部分有问题。在

def sigmoid(a):
   return 1/(1+np.exp(-a))    

def CF(theta,X,y):
   m=len(y)
   hypo=sigmoid(np.matmul(X,theta))
   J=(-1./m)*((np.matmul(y.T,np.log(hypo)))+(np.matmul((1-y).T,np.log(1-hypo))))
   return(J)

def gr(theta,X,y):
    m=len(y)
    hypo=sigmoid(np.matmul(X,theta))

    grad=(1/m)*(np.matmul(X.T,(hypo-y)))

    return(grad)

X是100x3数组,y是100x1,而{}是3x1数组。似乎这两个函数都是单独工作的,但此优化函数会出错:

^{pr2}$

The error: "ValueError: shapes (3,100) and (3,100) not aligned: 100 (dim 1) != 3 (dim 0)"


Tags: 函数loglenreturndefnp数组cf
1条回答
网友
1楼 · 发布于 2024-05-15 19:47:29
^{bq}$

立正!在

hypo的形状是(100,),而{}的形状是{}。在元素方面的-操作中,hypo根据numpy的broadcasting rules广播到形状(1, 100)。这将产生一个(100, 100)数组,这将导致矩阵乘法得到(3, 100)数组。在

通过使hypoy相同的形状来修复此问题:

hypo = sigmoid(np.matmul(X, theta)).reshape(-1, 1)  # -1 means automatic size on first dimension

还有一个问题:scipy.optimize.minimize(我假设您正在使用)期望渐变是一个形状(k,)的数组,但是函数gr返回形状(k, 1)的向量。这很容易修复:

^{pr2}$

最后的功能变成

def gr(theta,X,y):
    m=len(y)
    hypo=sigmoid(np.matmul(X,theta)).reshape(-1, 1)
    grad=(1/m)*(np.matmul(X.T,(hypo-y)))
    return grad.reshape(-1)

用toy data works运行它(我没有检查结果的数学或合理性):

theta = np.reshape([1, 2, 3], 3, 1)    
X = np.random.randn(100, 3)
y = np.round(np.random.rand(100, 1))    

optim = minimize(CF, theta, method='BFGS', jac=gr, args=(X,y))
print(optim)
#      fun: 0.6830931976615066
# hess_inv: array([[ 4.51307367, -0.13048255,  0.9400538 ],
#       [-0.13048255,  3.53320257,  0.32364498],
#       [ 0.9400538 ,  0.32364498,  5.08740428]])
#      jac: array([ -9.20709950e-07,   3.34459058e-08,   2.21354905e-07])
#  message: 'Optimization terminated successfully.'
#     nfev: 15
#      nit: 13
#     njev: 15
#   status: 0
#  success: True
#        x: array([-0.07794477,  0.14840167,  0.24572182])

相关问题 更多 >