python - 我的 Andrew Ng 类(class)的逻辑回归梯度 Python 代码不起作用

标签 python machine-learning logistic-regression

我已经为非正则化逻辑回归成本函数和查找梯度编写了一些代码,但无论我尝试什么,我的代码都会返回相同的 TypeError。

我已经尝试过代码的矢量化和 for 循环实现,但没有任何效果。我还想指出,评分者总是给我的成本函数打满分,但不给求偏导数的代码评分。我的结果始终与预期成本相符,但渐变部分没有返回任何内容。

它说这是成本: J(𝜃)=1𝑚Σ𝑖=1𝑚[−𝑦(𝑖)log(ℎ𝜃(𝑥(𝑖)))−(1−𝑦(𝑖))log(1−ℎ𝜃(𝑥(𝑖)))]

这是偏导数: ∂𝐽(𝜃)∂𝜃𝑗=1𝑚Σ𝑖=1𝑚(ℎ𝜃(𝑥(𝑖))−𝑦(𝑖))𝑥(𝑖)𝑗

(通过类(class),我可以验证这是正确的)

def costFunction(theta, X, y):
    # Initialize some useful values
    m = y.size  # number of training examples

    # You need to return the following variables correctly 
    J = 0
    grad = np.zeros(theta.shape)

    # ====================== YOUR CODE HERE ============
    for i in range(m):
        hypothesis = sigmoid(np.dot(theta.T, X[i, :]))
        J += y[i] * math.log(hypothesis) + (1 - y[i]) * math.log(1 - hypothesis)
        for j in range(n):
            grad = (hypothesis - y[i]) * X[i, j]

    J = (-1 / m) * J
    grad = (1 / m) * grad
    # =============================================================
    return J, grad



# Initialize fitting parameters
initial_theta = np.zeros(n+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]')

我希望输出是:

Cost at initial theta (zeros): 0.693
Expected cost (approx): 0.693

Gradient at initial theta (zeros):
    [-0.1000, -12.0092, -11.2628]
Expected gradients (approx):
    [-0.1000, -12.0092, -11.2628]

但我得到以下信息:

Cost at initial theta (zeros): 0.693
Expected cost (approx): 0.693

Gradient at initial theta (zeros):
Expected gradients (approx):
    [-0.1000, -12.0092, -11.2628]

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-ab2a7b541269> in <module>()
     15 cost, grad = costFunction(test_theta, X, y)
     16 
---> 17 print('Cost at test theta: {:.3f}'.format(*cost))
     18 print('Expected cost (approx): 0.218\n')
     19 

TypeError: format() argument after * must be an iterable, not numpy.float64

最佳答案

查看函数 costFunction() 时,返回值 J(您分配给 cost)是一个标量。因此,不能用星号*解包,应该直接传递给字符串格式化方法:

print('Cost at test theta: {:.3f}'.format(cost)) # passing 'cost' without the star

关于python - 我的 Andrew Ng 类(class)的逻辑回归梯度 Python 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58793912/

相关文章:

python - PySpark:AttributeError: 'PipelineModel'对象没有属性 'clusterCenters'

python - 使用 scikit-model 训练 Logistic 回归中的 N 维数据集

python - ConfigParser 中的 boolean 值总是返回 True

python - Pandas 数据框按列下降

tensorflow - FC 层后接 LSTM - Tensorflow

scikit-learn - 如何知道NLP模型中与特定类别相关的单词?

machine-learning - Vowpal Wabbit 逻辑回归

javascript - TensorFlow JS - 加载使用 Python 生成的模型

python - 如何为蜘蛛设置管道

machine-learning - 有没有办法在 ml5 yolo() 中使用自定义模型?