import numpy as np
import matplotlib.pyplot as plt

#逻辑回归 牛顿法

#加载数据
def load_data(file):
    data,label = [],[]
    for line in file.readlines():
        lineArr = line.strip().split()
        data.append([1.0, float(lineArr[0] ), float( lineArr[1])] )
    for i in range(80):
        if i < 40 : 
            label.append(1)
        else :
            label.append(0)
    xmat = np.mat(data)                 #x集
    ymat = np.mat(label).reshape(80,1)  #标签集
    return xmat,ymat

def w_calc (xmat,ymat,maxIter = 7):
    W = np.zeros((3,1))
    m = xmat.shape[0]                       #80
    for i in range(maxIter):
        h = 1.0/(1+np.exp(-(xmat*W)) )     #(80,1)
        grad = (1.0/m)*xmat.T*(h-ymat)     #(3,1) = (3,80) * (80,1)
        H = (1.0/m)*xmat.T*np.diag((h*(1-h).T).A[0] ).T*xmat  #(3,3) =(3,80)*(80,1)*(1,80)*(80,3)
        W -= np.linalg.inv(H)*grad       #(3,1) = 
    return W

file = open("ex4x.dat")
xmat,ymat = load_data(file)
W = w_calc(xmat,ymat)
print('w:',W)

w0=W[0,0]
w1=W[1,0]
w2=W[2,0]

plotx1 = np.arange(20,50,2)
plotx2 = -w0/w2-w1/w2*plotx1   #x2轴
plt.plot(plotx1,plotx2,c='r',label='decision boundary')

plt.scatter(xmat[:,1][ymat==0].A,xmat[:,2][ymat==0].A,marker='o',label='label=neg')
plt.scatter(xmat[:,1][ymat==1].A,xmat[:,2][ymat==1].A,marker='+',label='lebel=pos')
plt.grid()
plt.legend()
plt.show()

fae88395db54dd0f0f9a0725769bb0563f6c34da
<mask token>


def load_data(file):
    data, label = [], []
    for line in file.readlines():
        lineArr = line.strip().split()
        data.append([1.0, float(lineArr[0]), float(lineArr[1])])
    for i in range(80):
        if i < 40:
            label.append(1)
        else:
            label.append(0)
    xmat = np.mat(data)
    ymat = np.mat(label).reshape(80, 1)
    return xmat, ymat


def w_calc(xmat, ymat, maxIter=7):
    W = np.zeros((3, 1))
    m = xmat.shape[0]
    for i in range(maxIter):
        h = 1.0 / (1 + np.exp(-(xmat * W)))
        grad = 1.0 / m * xmat.T * (h - ymat)
        H = 1.0 / m * xmat.T * np.diag((h * (1 - h).T).A[0]).T * xmat
        W -= np.linalg.inv(H) * grad
    return W


<mask token>

<mask token>


def load_data(file):
    data, label = [], []
    for line in file.readlines():
        lineArr = line.strip().split()
        data.append([1.0, float(lineArr[0]), float(lineArr[1])])
    for i in range(80):
        if i < 40:
            label.append(1)
        else:
            label.append(0)
    xmat = np.mat(data)
    ymat = np.mat(label).reshape(80, 1)
    return xmat, ymat


def w_calc(xmat, ymat, maxIter=7):
    W = np.zeros((3, 1))
    m = xmat.shape[0]
    for i in range(maxIter):
        h = 1.0 / (1 + np.exp(-(xmat * W)))
        grad = 1.0 / m * xmat.T * (h - ymat)
        H = 1.0 / m * xmat.T * np.diag((h * (1 - h).T).A[0]).T * xmat
        W -= np.linalg.inv(H) * grad
    return W


<mask token>
print('w:', W)
<mask token>
plt.plot(plotx1, plotx2, c='r', label='decision boundary')
plt.scatter(xmat[:, 1][ymat == 0].A, xmat[:, 2][ymat == 0].A, marker='o',
    label='label=neg')
plt.scatter(xmat[:, 1][ymat == 1].A, xmat[:, 2][ymat == 1].A, marker='+',
    label='lebel=pos')
plt.grid()
plt.legend()
plt.show()

<mask token>


def load_data(file):
    data, label = [], []
    for line in file.readlines():
        lineArr = line.strip().split()
        data.append([1.0, float(lineArr[0]), float(lineArr[1])])
    for i in range(80):
        if i < 40:
            label.append(1)
        else:
            label.append(0)
    xmat = np.mat(data)
    ymat = np.mat(label).reshape(80, 1)
    return xmat, ymat


def w_calc(xmat, ymat, maxIter=7):
    W = np.zeros((3, 1))
    m = xmat.shape[0]
    for i in range(maxIter):
        h = 1.0 / (1 + np.exp(-(xmat * W)))
        grad = 1.0 / m * xmat.T * (h - ymat)
        H = 1.0 / m * xmat.T * np.diag((h * (1 - h).T).A[0]).T * xmat
        W -= np.linalg.inv(H) * grad
    return W


file = open('ex4x.dat')
xmat, ymat = load_data(file)
W = w_calc(xmat, ymat)
print('w:', W)
w0 = W[0, 0]
w1 = W[1, 0]
w2 = W[2, 0]
plotx1 = np.arange(20, 50, 2)
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='o',
    label='label=neg')
plt.scatter(xmat[:, 1][ymat == 1].A, xmat[:, 2][ymat == 1].A, marker='+',
    label='lebel=pos')
plt.grid()
plt.legend()
plt.show()

import numpy as np
import matplotlib.pyplot as plt


def load_data(file):
    data, label = [], []
    for line in file.readlines():
        lineArr = line.strip().split()
        data.append([1.0, float(lineArr[0]), float(lineArr[1])])
    for i in range(80):
        if i < 40:
            label.append(1)
        else:
            label.append(0)
    xmat = np.mat(data)
    ymat = np.mat(label).reshape(80, 1)
    return xmat, ymat


def w_calc(xmat, ymat, maxIter=7):
    W = np.zeros((3, 1))
    m = xmat.shape[0]
    for i in range(maxIter):
        h = 1.0 / (1 + np.exp(-(xmat * W)))
        grad = 1.0 / m * xmat.T * (h - ymat)
        H = 1.0 / m * xmat.T * np.diag((h * (1 - h).T).A[0]).T * xmat
        W -= np.linalg.inv(H) * grad
    return W


file = open('ex4x.dat')
xmat, ymat = load_data(file)
W = w_calc(xmat, ymat)
print('w:', W)
w0 = W[0, 0]
w1 = W[1, 0]
w2 = W[2, 0]
plotx1 = np.arange(20, 50, 2)
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='o',
    label='label=neg')
plt.scatter(xmat[:, 1][ymat == 1].A, xmat[:, 2][ymat == 1].A, marker='+',
    label='lebel=pos')
plt.grid()
plt.legend()
plt.show()

import numpy as np
import matplotlib.pyplot as plt

#逻辑回归 牛顿法

#加载数据
def load_data(file):
    data,label = [],[]
    for line in file.readlines():
        lineArr = line.strip().split()
        data.append([1.0, float(lineArr[0] ), float( lineArr[1])] )
    for i in range(80):
        if i < 40 : 
            label.append(1)
        else :
            label.append(0)
    xmat = np.mat(data)                 #x集
    ymat = np.mat(label).reshape(80,1)  #标签集
    return xmat,ymat

def w_calc (xmat,ymat,maxIter = 7):
    W = np.zeros((3,1))
    m = xmat.shape[0]                       #80
    for i in range(maxIter):
        h = 1.0/(1+np.exp(-(xmat*W)) )     #(80,1)
        grad = (1.0/m)*xmat.T*(h-ymat)     #(3,1) = (3,80) * (80,1)
        H = (1.0/m)*xmat.T*np.diag((h*(1-h).T).A[0] ).T*xmat  #(3,3) =(3,80)*(80,1)*(1,80)*(80,3)
        W -= np.linalg.inv(H)*grad       #(3,1) = 
    return W

file = open("ex4x.dat")
xmat,ymat = load_data(file)
W = w_calc(xmat,ymat)
print('w:',W)

w0=W[0,0]
w1=W[1,0]
w2=W[2,0]

plotx1 = np.arange(20,50,2)
plotx2 = -w0/w2-w1/w2*plotx1   #x2轴
plt.plot(plotx1,plotx2,c='r',label='decision boundary')

plt.scatter(xmat[:,1][ymat==0].A,xmat[:,2][ymat==0].A,marker='o',label='label=neg')
plt.scatter(xmat[:,1][ymat==1].A,xmat[:,2][ymat==1].A,marker='+',label='lebel=pos')
plt.grid()
plt.legend()
plt.show()
