简单的说就是每一次计算的时候计算梯度误差alpha作为更新的值
for k in range(maxCycles): #heavy on matrix operationsh = sigmoid(dataMatrix*weights) #matrix multerror = (labelMat - h) #vector subtractionweights = weights + alpha * dataMatrix.transpose()* error #matrix mult
改进1
分开每一步数据进行计算
for i in range(m):h = sigmoid(sum(dataMatrix[i]*weights))error = classLabels[i] - hweights = weights + alpha * error * dataMatrix[i]
改进2
使用随机选择的数据进行更新,可以避免周期性的波动(每次遇到某个固定的数据波动)
改进3
对Alpha进行修改
for j in range(numIter):dataIndex = range(m)for i in range(m):alpha = 4/(1.0+j+i)+0.0001 #apha decreases with iteration, does notrandIndex = int(random.uniform(0,len(dataIndex)))#go to 0 because of the constanth = sigmoid(sum(dataMatrix[randIndex]*weights))error = classLabels[randIndex] - hweights = weights + alpha * error * dataMatrix[randIndex]del(dataIndex[randIndex])
