逻辑回归 - 基础 - 代码实现

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chuan403082010/article/details/86370675

ytb_lr.txt数据

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
#import lib
import  numpy as np
import matplotlib.pyplot as plt

#def loaddata
def loaddata(filename):
    file = open(filename)
    x = []
    y = []
    for line in file.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
    file.close()
    return xmat,ymat

# w calc
def w_cal(xmat,ymat,alpha = 0.001,maxIter = 10001):
    W = np.mat(np.random.randn(3,1))
    w_save= []
    #w update
    for i in range(maxIter):
        H = 1/(1 + np.exp(-xmat * W))
        dw = xmat.T * (H - ymat)
        W  -= alpha * dw
        if i % 100 ==0:
             w_save.append([W.copy(),i])


    return W,w_save

# implement
xmat,ymat = loaddata('ytb_lr.txt')

W,w_save = w_cal(xmat,ymat)
# print(W)
# print('xmat:',xmat)
# print('ymat:',ymat)

# 基本展示
# w0 = W[0,0]
# w1 = W[1,0]
# w2 = W[2,0]
# plotx1 =  np.arange(1,7,0.001)
# plotx2 = -w0 /w2 - w1/w2 * plotx1
# plt.plot(plotx1,plotx2,c = 'r',label = 'decision boundary')
#
# plt.scatter(xmat[:,1][ymat ==0].A,xmat[:,2][ymat ==0].A,marker='^',s = 150,label = 0)
# plt.scatter(xmat[:,1][ymat ==1].A,xmat[:,2][ymat ==1].A,s = 150,label = 1)
# plt.grid()
# plt.legend()
# plt.show()

#动画
for wi in w_save:
    plt.clf()
    w0 = wi[0][0,0]
    w1 = wi[0][1,0]
    w2 = wi[0][2,0]
    plotx1 =  np.arange(2,6,0.01)
    plotx2 = -w0 /w2 - w1/w2 * plotx1
    plt.plot(plotx1,plotx2,c = 'r',label = 'decision boundary')

    plt.scatter(xmat[:,1][ymat ==0].A,xmat[:,2][ymat ==0].A,marker='^',s = 150,label = 0)
    plt.scatter(xmat[:,1][ymat ==1].A,xmat[:,2][ymat ==1].A,s = 150,label = 1)
    plt.grid()
    plt.legend()
    plt.title('iter:%s'%np.str(wi[1]))
    plt.pause(0.001)
plt.show()


#逻辑回归编程基本款

猜你喜欢

转载自blog.csdn.net/chuan403082010/article/details/86370675