《机器学习实战笔记》-逻辑回归-梯度上升法

主要难点:公式推导和边界函数的设定


import matplotlib.pyplot as plt
import  numpy as np
'''
求函数f(x)=-x^2+4x的最大值
'''
def gradient_Ascent_test():
    def f_prime(x_old):
       return  -2*x_old+4  # 'f(x)的导数=-2x+4'
    # 初始化:old小于new new从曲线(0,0)开始,学习率为0.01误差值为0.000001
    x_old=-1
    x_new=0
    alpha=0.01
    pression=0.00000001
    while(x_new-x_old>pression):
        x_old=x_new
        x_new=x_old+alpha*f_prime(x_old)
    print(x_new)
'''加载数据'''
def loadDataSet():
    dataMat=[]
    lableMat=[]
    fr=open('testSet.txt')
    for line in fr.readlines():
        lineArr=line.strip().split()
        dataMat.append([1.0,float(lineArr[0]),float(lineArr[1])])
        lableMat.append((int(lineArr[2])))
    fr.close();
    return  dataMat,lableMat
'''描绘数据'''
def plotDataSet():
    dataMat,labMat=loadDataSet()
    dataArr=np.array(dataMat)
    n=np.shape(dataMat)[0]
    xcord1=[];ycord1=[]
    xcord2=[];ycord2=[]
    for i in range(n):
        if int(labMat[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)  #将画布分为1行1列的第一块(从左到右从上到下的第一块)'
    ax.scatter(xcord1, ycord1, s = 20, c = 'red', marker = 's')#绘制正样本
    ax.scatter(xcord2, ycord2, s = 20, c = 'green')
    plt.title('data')
    plt.xlabel('x'); plt.ylabel('y')                                    #绘制label
    plt.show()

"sigmoid函数 "
def sigmoid(inX):
    return 1.0/(1+np.exp(-inX))
"梯度上升法"
"算法思想:读取数据-获取数据行列数-设置学习率和循环次数-初始权重为1-循环500次对w的值进行调整" \
"最后输出:返回获得权重的数组"
def gradAscent(dataMatIn,classLabels):
    dataMatrix=np.mat(dataMatIn)
    lableMat=np.mat(classLabels).transpose()
    m,n=np.shape(dataMatrix)
    alpha=0.001
    maxCycles=500
    weights=np.ones((n,1))
    for k in range(maxCycles):
        weights=weights+alpha*dataMatrix.transpose()*(lableMat-sigmoid(dataMatrix*weights))

    return  weights.getA()
'绘制决策边界'
'加载数据-将数据转为数组-获取数据行数-建立存放不同数组的点(正例x1y1;反例x2y2)-将点根据标签不同加入x1、x2' \
'新建图像-绘制网格-设置分界线的x范围和对应的w值-绘制点、标题、x轴名称y轴名称-打印地图'
def plotBestFit(weights):
    dataMat,lableMat=loadDataSet()
    dataArr=np.array(dataMat)
    xcord1=[];ycord1=[]
    xcord2=[];ycord2=[]
    n=np.shape(dataMat)[0]
    for i in range(n):
        if(lableMat[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=20, c='red', marker='s', alpha=.5)  # 绘制正样本
    ax.scatter(xcord2, ycord2, s=20, c='green', alpha=.5)
    x=np.arange(-5.0,5.0,0.1)
    y= (-weights[0]-weights[1]*x)/weights[2]
    ax.plot(x,y)
    plt.title("logcie")
    plt.xlabel("x1")
    plt.ylabel("x2")
    plt.show()
if __name__ == '__main__':
    dataMat,lableMat=loadDataSet()
    weight=gradAscent(dataMat,lableMat)
    plotBestFit(weight)






实验结果:

《机器学习实战笔记》-逻辑回归-梯度上升法_第1张图片

 

你可能感兴趣的:(深度学习实战-自学笔记,机器学习,逻辑回归,人工智能)