逻辑回归,梯度计算错误

时间:2021-05-23 12:49:38

标签: python logistic-regression

我目前正在学习 Andrew ng 机器学习课程。在第二个练习逻辑回归中。我遇到了很大的麻烦,到目前为止无法解决。错误发生在成本函数中。 (https://github.com/dibgerge/ml-coursera-python-assignments/tree/master/Exercise2 这是我使用的 python 版本)

def costFunction(theta, X, y):
  
    m = y.size 
    
    J = 0
    
    grad = np.zeros(theta.shape)
    
    cost = -y*np.log(sigmoid(np.dot(X,theta))) - ((1-y)*np.log(1-sigmoid(np.dot(X,theta))))
    
    J = (1/m)*sum(cost)
    
    grad = 1/m * np.dot(X.transpose(),(sigmoid(np.dot(X,theta)) - y))

    return J[0], grad

当我运行这个单元格时,看起来我已经完美地计算了梯度(我也复制了许多不同的计算梯度的方法),这个错误弹出。谁能帮我解决这个问题

initial_theta = np.zeros((n+1,1))
cost, grad = costFunction(initial_theta, X, y)

print('Cost at initial theta (zeros): {:.3f}'.format(cost))
print('Expected cost (approx): 0.693\n')
print('Gradient at initial theta (zeros):')
print('\t[{:.4f}, {:.4f}, {:.4f}]'.format(*grad))
print('Expected gradients (approx):\n\t[-0.1000, -12.0092, -11.2628]\n')

# Compute and display cost and gradient with non-zero theta
test_theta = np.array([-24, 0.2, 0.2])
cost, grad = costFunction(test_theta, X, y)

print('Cost at test theta: {:.3f}'.format(cost))
print('Expected cost (approx): 0.218\n')

print('Gradient at test theta:')
print('\t[{:.3f}, {:.3f}, {:.3f}]'.format(*grad))
print('Expected gradients (approx):\n\t[0.043, 2.566, 2.647]')
<块引用>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-4242e4a47821> in <module>
      7 print('Expected cost (approx): 0.693\n')
      8 print('Gradient at initial theta (zeros):')
----> 9 print('\t[{:.4f}, {:.4f}, {:.4f}]'.format(*grad))
     10 print('Expected gradients (approx):\n\t[-0.1000, -12.0092, -11.2628]\n')
     11 

TypeError: unsupported format string passed to numpy.ndarray.__format__

计算后打印出来的梯度是这样的

> [[  0.5          0.5          0.5         -0.5         -0.5
>     0.5         -0.5         -0.5         -0.5         -0.5
>     0.5          0.5         -0.5         -0.5          0.5    -0.5         -0.5          0.5         -0.5         -0.5
>     0.5         -0.5          0.5          0.5         -0.5    -0.5         -0.5          0.5          0.5          0.5    -0.5         -0.5          0.5         -0.5          0.5
>     0.5          0.5         -0.5          0.5          0.5    -0.5          0.5         -0.5          0.5          0.5
>     0.5         -0.5         -0.5         -0.5         -0.5    -0.5         -0.5         -0.5          0.5          0.5
>     0.5         -0.5          0.5         -0.5         -0.5    -0.5          0.5          0.5          0.5          0.5
>     0.5         -0.5          0.5         -0.5         -0.5
>     0.5         -0.5         -0.5         -0.5         -0.5    -0.5         -0.5         -0.5          0.5          0.5    -0.5         -0.5         -0.5         -0.5         -0.5    -0.5          0.5         -0.5         -0.5          0.5    -0.5         -0.5          0.5         -0.5         -0.5    -0.5         -0.5         -0.5         -0.5         -0.5       ]  [ 32.82213703  32.82213703  32.82213703 -32.82213703 -32.82213703
>    32.82213703 -32.82213703 -32.82213703 -32.82213703 -32.82213703
>    32.82213703  32.82213703 -32.82213703 -32.82213703  32.82213703   -32.82213703 -32.82213703  32.82213703 -32.82213703 -32.82213703
>    32.82213703 -32.82213703  32.82213703  32.82213703 -32.82213703   -32.82213703 -32.82213703  32.82213703  32.82213703  32.82213703   -32.82213703 -32.82213703  32.82213703 -32.82213703  32.82213703
>    32.82213703  32.82213703 -32.82213703  32.82213703  32.82213703   -32.82213703  32.82213703 -32.82213703  32.82213703  32.82213703
>    32.82213703 -32.82213703 -32.82213703 -32.82213703 -32.82213703   -32.82213703 -32.82213703 -32.82213703  32.82213703  32.82213703
>    32.82213703 -32.82213703  32.82213703 -32.82213703 -32.82213703   -32.82213703  32.82213703  32.82213703  32.82213703  32.82213703
>    32.82213703 -32.82213703  32.82213703 -32.82213703 -32.82213703
>    32.82213703 -32.82213703 -32.82213703 -32.82213703 -32.82213703   -32.82213703 -32.82213703 -32.82213703  32.82213703  32.82213703   -32.82213703 -32.82213703 -32.82213703 -32.82213703 -32.82213703   -32.82213703  32.82213703 -32.82213703 -32.82213703  32.82213703   -32.82213703 -32.82213703  32.82213703 -32.82213703 -32.82213703   -32.82213703 -32.82213703 -32.82213703 -32.82213703 -32.82213703]  [ 33.11099904  33.11099904  33.11099904 -33.11099904 -33.11099904
>    33.11099904 -33.11099904 -33.11099904 -33.11099904 -33.11099904
>    33.11099904  33.11099904 -33.11099904 -33.11099904  33.11099904   -33.11099904 -33.11099904  33.11099904 -33.11099904 -33.11099904
>    33.11099904 -33.11099904  33.11099904  33.11099904 -33.11099904   -33.11099904 -33.11099904  33.11099904  33.11099904  33.11099904   -33.11099904 -33.11099904  33.11099904 -33.11099904  33.11099904
>    33.11099904  33.11099904 -33.11099904  33.11099904  33.11099904   -33.11099904  33.11099904 -33.11099904  33.11099904  33.11099904
>    33.11099904 -33.11099904 -33.11099904 -33.11099904 -33.11099904   -33.11099904 -33.11099904 -33.11099904  33.11099904  33.11099904
>    33.11099904 -33.11099904  33.11099904 -33.11099904 -33.11099904   -33.11099904  33.11099904  33.11099904  33.11099904  33.11099904
>    33.11099904 -33.11099904  33.11099904 -33.11099904 -33.11099904
>    33.11099904 -33.11099904 -33.11099904 -33.11099904 -33.11099904   -33.11099904 -33.11099904 -33.11099904  33.11099904  33.11099904   -33.11099904 -33.11099904 -33.11099904 -33.11099904 -33.11099904   -33.11099904  33.11099904 -33.11099904 -33.11099904  33.11099904   -33.11099904 -33.11099904  33.11099904 -33.11099904 -33.11099904   -33.11099904 -33.11099904 -33.11099904 -33.11099904 -33.11099904]]

0 个答案:

没有答案