美文网首页
逻辑回归及梯度下降(代码)

逻辑回归及梯度下降(代码)

作者: lilicat | 来源:发表于2019-01-23 15:50 被阅读0次

    重点

    1 W 计算
    2 动画显示(jupyter notebook)
    3 数据读取
    4 例子数据


    W迭代计算

    def w_cal(xmat,ymat,alpha=0.001,maxIter=101):
        #W init
        W = np.mat(np.random.randn(3,1))
        W_save = []
        for i in range(maxIter):
            H = 1/(1+np.exp(-xmat*W))
            dw = xmat.T*(H-ymat)
            W -= alpha*dw
            W_save.append([W.copy(),i])
        return W,W_save
    

    动画显示

    %matplotlib inline
    from IPython import display
    
    for w in W_save:
        plt.clf()
        w0 = w[0][0,0]
        w1 = w[0][1,0]
        w2 = w[0][2,0]
        plotx1 = np.arange(1,7,0.01)
        plotx2 = -w0/w2-w1/w2*plotx1
        plt.plot(plotx1,plotx2,c='r',label='decision boundary')
        plt.scatter(x[:,1][y==0].A,x[:,2][y==0].A,marker= '^',label="label=0")
        plt.scatter(x[:,1][y==1].A,x[:,2][y==1].A,label="label=0")
        plt.grid()
        plt.legend()
        plt.title('iter:%s'%np.str(w[1]+1))
        #plt.pause(0.1)
        plt.show()
        display.clear_output(wait=True)
    

    数据读取

    def loadData(filename):
        x = []
        y = []
        with open(filename) as f:
            for line in f.readlines():
                line = line.strip().split()
                x.append([1,float(line[0]),float(line[1])])
                y.append(float(line[-1]))
        xmat = np.mat(x)
        ymat = np.mat(y).T
        return xmat,ymat
    

    例子数据
    2 1 0
    2 2 0
    5 4 1
    4 5 1
    2 3 0
    3 2 0
    6 5 1
    4 1 0
    6 3 1
    7 4 1

    相关文章

      网友评论

          本文标题:逻辑回归及梯度下降(代码)

          本文链接:https://www.haomeiwen.com/subject/vytsjqtx.html