Home > other >  Logistics regression summary
Logistics regression summary

Time:10-06

Different colors represent different categories, find a Logistic regression using the gradient rise method under the classifier in the data set on one of the best regression function,



Def create_data () :
DataM=[]
ClassM=[]
With the open (' testSet. TXT) as f:
For the l in f.r eadlines () :
Lenarr=(l.s trip (). The split ())
DataM. Append ([1.0, float (lenarr [0]), the float (lenarr [1])])
ClassM. Append (int (lenarr [2]))
Return dataM, classM

Def sigmoid (x) :
Return 1.0/(1 + np. Exp (x)) # vector operations involved, the inside of the np pack function


Def grad (dataM, classM) : # return values for the regression coefficient
DataM=np. Mat (dataM) # into matrix form, convenient calculation
ClassM=np. Mat (classM). Transpose (#) into a matrix transpose again after
M, n=np. Shape (dataM)
Alpha=0.001
Maxcycles=500
Weights=np. 'ones ((n, 1))
For I in range (maxcycles) :
H=sigmoid (dataM * weights)
Error=classM -h
Weights=weights + alpha * dataM transpose () * error
Return weights




Def grad (dataM, classM) : # return values for the regression coefficient
DataM=np. Mat (dataM) # into matrix form, convenient calculation
ClassM=np. Mat (classM). Transpose (#) into a matrix transpose again after
M, n=np. Shape (dataM)
Alpha=0.001
Maxcycles=500
Weights=np. 'ones ((n, 1))
For I in range (maxcycles) :
H=sigmoid (dataM * weights)
Error=classM -h
Weights=weights + alpha * dataM transpose () * error
Return weights


Weights + alpha * dataM transpose () * error




Def plotFig () :
The import matplotlib. Pyplot as PLT datas,
Labels=create_data ()
Ws=grad (datas, labels)
Print (ws)
Ws=np. Array (ws)
DataArr=np. Array (datas)
Data_len=np. Shape (datas) [0] # data number
X1=[]; X2==y1 [] []; Y2=[]
For I in range (data_len) :
If the int (labels [I])==1: # belongs to the first category when
X1. Append (dataArr [I] [1]).
Y1. Append (dataArr [I] [2])
The else:
X2. Append (dataArr [I] [1]).
Y2. Append (dataArr [I] [2])
FIG.=PLT figure (#) combine two kinds of data in drawing scatterplot ax=FIG. Add_subplot (111) ax. Scatter (x1, y1, s=30, c='red', marker='s') ax. Scatter (x2, y2, s=30, c='green')
X=np. Arange (3.0, 3.0, 0.1)
Y=(ws [0] - ws [1] * x)/ws. [2] ax plot (x, y) # draw PLT boundary function. The show ()

The last run

  • Related