Python实现的梯度下降算法不会收敛

时间:2015-05-13 17:07:53

标签: python algorithm machine-learning

我在python中实现了一个梯度下降算法,它在运行时就没有收敛。当我调试它时,我必须使alpha非常小,让它看起来像#39;收敛。 alpha就像是1e-12那么小。

这是我的代码

    def batchGradDescent(dataMat, labelMat):
        dataMatrix = mat(dataMat)
        labelMatrix = mat(labelMat).transpose()
        m, n = shape(dataMatrix)
        cycle = 1000000
        alpha = 7e-11
        saved_weights = ones((n,1))
        weights = saved_weights
        for k in range(cycle):
            hypothesis = dataMatrix * weights
            saved_weights = weights
            error = labelMatrix - hypothesis
            weights = saved_weights + alpha * dataMatrix.transpose() * error
            print weights-saved_weights

        return weights

我的数据集就像这样(一行)

800 0   0.3048  71.3    0.00266337  126.201

前五个元素是特征,最后一个是标签。

有人可以提供帮助吗?我在这里真的很沮丧。我认为我的算法在理论上是正确的。是关于数据集的规范化吗?

谢谢。

0 个答案:

没有答案