1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+ # @Time : 2018/9/3 15:30
4+ # @Author : louwill
5+ # @File : lr_class.py
6+ 7+
8+
9+ import numpy as np
10+ import matplotlib .pyplot as plt
11+ from sklearn .datasets .samples_generator import make_classification
12+
13+ class logistic_regression ():
14+ def __init__ (self ):
15+ pass
16+
17+ def sigmoid (self , x ):
18+ z = 1 / (1 + np .exp (- x ))
19+ return z
20+
21+ def initialize_params (self , dims ):
22+ W = np .zeros ((dims , 1 ))
23+ b = 0
24+ return W , b
25+
26+ def logistic (self , X , y , W , b ):
27+ num_train = X .shape [0 ]
28+ num_feature = X .shape [1 ]
29+
30+ a = self .sigmoid (np .dot (X , W ) + b )
31+ cost = - 1 / num_train * np .sum (y * np .log (a ) + (1 - y ) * np .log (1 - a ))
32+
33+ dW = np .dot (X .T , (a - y )) / num_train
34+ db = np .sum (a - y ) / num_train
35+ cost = np .squeeze (cost )
36+
37+ return a , cost , dW , db
38+
39+ def logistic_train (self , X , y , learning_rate , epochs ):
40+ W , b = self .initialize_params (X .shape [1 ])
41+ cost_list = []
42+
43+ for i in range (epochs ):
44+ a , cost , dW , db = self .logistic (X , y , W , b )
45+ W = W - learning_rate * dW
46+ b = b - learning_rate * db
47+
48+ if i % 100 == 0 :
49+ cost_list .append (cost )
50+ if i % 100 == 0 :
51+ print ('epoch %d cost %f' % (i , cost ))
52+
53+ params = {
54+ 'W' : W ,
55+ 'b' : b
56+ }
57+ grads = {
58+ 'dW' : dW ,
59+ 'db' : db
60+ }
61+
62+ return cost_list , params , grads
63+
64+ def predict (self , X , params ):
65+ y_prediction = self .sigmoid (np .dot (X , params ['W' ]) + params ['b' ])
66+
67+ for i in range (len (y_prediction )):
68+ if y_prediction [i ] > 0.5 :
69+ y_prediction [i ] = 1
70+ else :
71+ y_prediction [i ] = 0
72+
73+ return y_prediction
74+
75+ def accuracy (self , y_test , y_pred ):
76+ correct_count = 0
77+ for i in range (len (y_test )):
78+ for j in range (len (y_pred )):
79+ if y_test [i ] == y_pred [j ] and i == j :
80+ correct_count += 1
81+
82+ accuracy_score = correct_count / len (y_test )
83+ return accuracy_score
84+
85+ def create_data (self ):
86+ X , labels = make_classification (n_samples = 100 , n_features = 2 , n_redundant = 0 , n_informative = 2 ,
87+ random_state = 1 , n_clusters_per_class = 2 )
88+ labels = labels .reshape ((- 1 , 1 ))
89+ offset = int (X .shape [0 ] * 0.9 )
90+ X_train , y_train = X [:offset ], labels [:offset ]
91+ X_test , y_test = X [offset :], labels [offset :]
92+ return X_train , y_train , X_test , y_test
93+
94+ def plot_logistic (self , X_train , y_train , params ):
95+ n = X_train .shape [0 ]
96+ xcord1 = []
97+ ycord1 = []
98+ xcord2 = []
99+ ycord2 = []
100+ for i in range (n ):
101+ if y_train [i ] == 1 :
102+ xcord1 .append (X_train [i ][0 ])
103+ ycord1 .append (X_train [i ][1 ])
104+ else :
105+ xcord2 .append (X_train [i ][0 ])
106+ ycord2 .append (X_train [i ][1 ])
107+ fig = plt .figure ()
108+ ax = fig .add_subplot (111 )
109+ ax .scatter (xcord1 , ycord1 , s = 32 , c = 'red' )
110+ ax .scatter (xcord2 , ycord2 , s = 32 , c = 'green' )
111+ x = np .arange (- 1.5 , 3 , 0.1 )
112+ y = (- params ['b' ] - params ['W' ][0 ] * x ) / params ['W' ][1 ]
113+ ax .plot (x , y )
114+ plt .xlabel ('X1' )
115+ plt .ylabel ('X2' )
116+ plt .show ()
117+
118+
119+ if __name__ == "__main__" :
120+ model = logistic_regression ()
121+ X_train , y_train , X_test , y_test = model .create_data ()
122+ print (X_train .shape , y_train .shape , X_test .shape , y_test .shape )
123+ cost_list , params , grads = model .logistic_train (X_train , y_train , 0.01 , 1000 )
124+ print (params )
125+ y_train_pred = model .predict (X_train , params )
126+ accuracy_score_train = model .accuracy (y_train , y_train_pred )
127+ print ('train accuracy is:' , accuracy_score_train )
128+ y_test_pred = model .predict (X_test , params )
129+ accuracy_score_test = model .accuracy (y_test , y_test_pred )
130+ print ('test accuracy is:' , accuracy_score_test )
131+ model .plot_logistic (X_train , y_train , params )
0 commit comments