梯度上升算法每次更新回归系数时都要遍历整个数据集,在样本数较少时还可以,当样本数目太多时复杂度太高,所以产生了随机梯度上升算法,每次仅用一个样本点来更新回归系数。
def stocGradAscent0(dataMatrix,classLabels):
m,n=shape(dataMatrix)
alpha=0.01
weights=ones(n)
for i in range(m):
h=sigmoid(sum(dataMatrix[i]*weights))
error=classLabels[i]-h
weights=weights+alpha*error*dataMatrix[i]
return weights