python实战:逻辑回归实现二分类

时间:2022-12-06 23:49:09
from numpy import*
import matplotlib.pyplot as plt

def sigmoid(inX):
    return 1.0/(1+exp(-inX))

def loadDataSet(path):
    dataMat=[]
    labelmat=[]
    fr=open(path)
    for line in fr.readlines():
        a=float(line.strip().split()[0])
        b=float(line.strip().split()[1])
        c=int(line.strip().split()[2])
        dataMat.append([a,b,1])
        labelmat.append(c)
    fr.close()
    return dataMat,labelmat


def trainDataSet(dataMat,labelMat):
    alpha=0.001
    k=500
    dataMat_Vector=mat(dataMat)
    labelMat_Vector=mat(labelMat)
    num_Lines,num_Features=shape(dataMat_Vector)
    dataMat_T=dataMat_Vector.transpose()
    labelMat_T=labelMat_Vector.transpose()
    weight=ones((num_Features,1))
    print("dataMat_Vector:%s,%s"%shape(dataMat_Vector))
    print("weight:%s,%s"%shape(weight))
    for i in range(k):
        error=labelMat_T - sigmoid(dataMat_Vector * weight)
        weight=weight+alpha*dataMat_T*(error)
    #print(weight)
    return weight

path='/Users/wangyouzhuo/Desktop/testSet.txt'

dataMat,labelmat=loadDataSet(path)
weight=trainDataSet(dataMat,labelmat)

x_green=[]
x_red=[]
y_green=[]
y_red=[]

fig = plt.figure()
ax = fig.add_subplot(111)

for i in range(len(dataMat)):
    if int(labelmat[i])==0:
        x_green.append(float(dataMat[i][0]))
        y_green.append(float(dataMat[i][1]))
    else:
        x_red.append(float(dataMat[i][0]))
        y_red.append(float(dataMat[i][1]))

a=arange(-5.0,5.0,0.1)
a=list(a)
b=-(weight[2][0]/weight[1][0])-((weight[0][0]/weight[1][0])*a)
b=array(b)
b=b.tolist()
ax.scatter(x_green,y_green,color='green')
ax.scatter(x_red,y_red,color='red')
ax.plot(a,b[0])
plt.xlabel('x')
plt.ylabel('y')
plt.show()