[机器学习]Logistic回归梯度上升法与改进的随机梯度上升算法

http://sbp810050504.blog.51cto.com/2799422/1608064/这个网址解释了多维空间的sigmoid函数与梯度上升算法的原理,大家可以参考一下。

from numpy import *
def loadDataSet():#读数据
    dataMat = []
    labelMat = []
    fr = open('testSet.txt')
    for line in fr.readlines():
        lineArr = line.strip().split()
        dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
        labelMat.append(int(lineArr[2]))
    return dataMat, labelMat


def sigmoid(intX):#sigmoid函数
    return 1.0 / (1 + exp(-intX))


def gradAscent(dataMatIn, classLabels):#Logistic回归梯度上升优化算法
    dataMatrix = mat(dataMatIn)
    labelMat = mat(classLabels).transpose()
    m, n = shape(dataMatrix)
    alpha = 0.001
    maxCycles = 500
    weights = ones((n, 1))
    for k in range(maxCycles):
        h = sigmoid(dataMatrix * weights)
        error = labelMat - h
        weights = weights + alpha * dataMatrix.transpose() * error
    return weights

def stocGradAscent0(dataMatrix,classLabels):#随机梯度上升算法
    m,n =shape(dataMatrix)
    alpha = 0.01
    weights =ones(n)
    for i in range(m):
        h = sigmoid(dataMatrix[i]*weights)
        error = classLabels[i] - h
        weights = weights + alpha*error*dataMatrix[i]
    return weights

def stocGradAscent1(dataMatrix,classLabels,numIter=150):#改进的随机梯度上升算法
    m,n =shape(dataMatrix)
    weights = ones(n)
    for j in range(numIter):
        dataIndex = list(range(m))
        for i in range(m):
            alpha = 4/(1.0+j+i)+0.01
            randIndex = int(random.uniform(0,len(dataIndex)))
            h = sigmoid(sum(dataMatrix[randIndex]*weights))
            error = classLabels[randIndex]-h
            weights = weights +alpha*error*dataMatrix[randIndex]
            del(dataIndex[randIndex])
    return weights


def plotBestFit(weights):#数据可视化
    import matplotlib.pyplot as plt
    dataMat,labelMat=loadDataSet()
    dataArr = array(dataMat)
    n = shape(dataArr)[0]
    xcord1 = []; ycord1 = []
    xcord2 = []; ycord2 = []
    for i in range(n):
        if int(labelMat[i])== 1:
            xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i,2])
        else:
            xcord2.append(dataArr[i,1]); ycord2.append(dataArr[i,2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord1, ycord1, s=30, c='blue', marker='s')
    ax.scatter(xcord2, ycord2, s=30, c='red')
    x = arange(-3.0, 3.0, 0.1)
    y = (-weights[0]-weights[1]*x)/weights[2]
    ax.plot(x, y)
    plt.xlabel('X1'); plt.ylabel('X2')
    plt.show()


def main():
    dataArr,labelMat = loadDataSet()
    weights1 = gradAscent(dataArr,labelMat)
    print(weights1)
    plotBestFit(weights1.getA())


    dataArr, labelMat = loadDataSet()
    weights2 = stocGradAscent0(array(dataArr),labelMat)
    print(weights2)
    plotBestFit(weights2)

    
    dataArr, labelMat = loadDataSet()
    weight3 = stocGradAscent1(array(dataArr),labelMat)
    print(weight3)
    plotBestFit(weight3)


if __name__ == '__main__':
    main()

结果:

图片[机器学习]Logistic回归梯度上升法与改进的随机梯度上升算法_第1张图片

图片一

图片二

图片三[机器学习]Logistic回归梯度上升法与改进的随机梯度上升算法_第2张图片[机器学习]Logistic回归梯度上升法与改进的随机梯度上升算法_第3张图片[机器学习]Logistic回归梯度上升法与改进的随机梯度上升算法_第4张图片


你可能感兴趣的:(机器学习)