Tutorial 0405: Logistic Regression
Course: Machine learning
2024–08–20
1 SGD cho Logistic Regression
2 Code mẫu
1 import numpy as np
2 import matplotlib . pyplot as plt
3 from sklearn . model_selection import train_test_split
4
5 # đọc dữ liệu
6 iris = np . genfromtxt ( ’ iris_full . csv ’ , dtype = None , delimiter = ’ , ’ , skip_header =1)
7 X = iris [: , :4]
8 y = iris [: , 4]
9
10 N = X . shape [0] # 100
11 indices = np . random . permutation ( N )
12 X = X [ indices ]
13 y = y [ indices ]
14
15 intercept = np . ones (( X . shape [0] , 1) )
16 X_b = np . concatenate (( intercept , X ) , axis =1)
17
18 print ( ’ X_b ’ , X_b . shape )
19 print ( ’y ’ , y . shape )
20 print ( ’N ’ , N )
21
22 >>
1
23 X_b (100 , 5)
24 y (100 ,)
25 N 100
1 # khởi tạo
2
3 shuffled_indices = np . random . permutation ( N )
4 X_b = X_b [ shuffled_indices ]
5 y = y [ shuffled_indices ]
6
7 X_train , X_test , y_train , y_test = train_test_split ( X_b ,y , test_size =0.2 ,
random_state =4 , stratify = y )
8 N = X_train . shape [0]
9 print ( ’ X_train ’ , X_b . shape )
10 print ( ’ y_train ’ , y . shape )
11 print ( ’N ’ , N )
12
13 >>
14 X_train (100 , 5)
15 y_train (100 ,)
16 N 80
1 # xây dựng model
2
3 def sigmoid_function ( z ) :
4 return 1 / (1 + np . exp ( - z ) )
5
6 def loss_function ( y_hat , y ) :
7 return ( - y * np . log ( y_hat ) - (1 - y ) * np . log (1 - y_hat ) )
8
9 def predict (x , theta ) :
10 z = np . dot (x , theta )
11 y_hat = sigmoid_function ( z )
12
13 return y_hat
2
1 lr = 0.01
2 num_iter = 100
3
4 theta = np . array ([0.1 , 0.3 , 0.1 , 0.2 , -0.1])
5
6 losses = []
7 preds = []
8 accuracies = []
9
10
11 for epoch in range ( num_iter ) :
12 for i in range (0 , N ) :
13 xi = X_train [ i : i +1]
14 yi = y_train [ i : i +1]
15
16 # compute output
17 y_hat = predict ( xi , theta )
18
19 # compute loss
20 loss = loss_function ( y_hat , yi )
21
22 # compute mean of gradient
23 gradient = np . dot ( xi .T , ( y_hat - yi ) )
24
25 # update
26 theta = theta - lr * gradient
27
28 #===============================
29 # loss
30 losses . append ( loss )
1 # trực quan dữ liệu hàm loss
2
3 import matplotlib . pyplot as plt
4
5 plt . plot ( losses )
6 plt . show ()
1 theta
2 >> array ([ -0.29066313 , -0.51131797 , -1.9584287 , 2.96508145 , 1.12718795])
3
1 # compute acc
2 preds = []
3 for i in range (0 , len ( X_test ) ) :
4 xi = X_test [ i : i +1]
5 yi = y_test [ i : i +1]
6
7 y_hat = predict ( xi , theta ) . round ()
8 preds . append ( y_hat [0])
1 from sklearn . metrics import accuracy_score , confusion_matrix ,
classification_report
2 import seaborn as sns
3
4 print ( ’ Accuracy (%) : ’ , accuracy_score ( y_test , preds ) )
5 print ( c l a s s i f i c a t i o n _ r e p o r t ( y_test , preds ) )
6
7 >>
8 Accuracy (%) : 1.0
9 precision recall f1 - score support
10
11 0.0 1.00 1.00 1.00 10
12 1.0 1.00 1.00 1.00 10
13
14 accuracy 1.00 20
15 macro avg 1.00 1.00 1.00 20
16 weighted avg 1.00 1.00 1.00 20
1 sns . heatmap ( confusion_matrix ( y_test , preds ) , annot = True )
2 plt . show ()
4
3 Mini-Batch GD cho Logistic Regression
— Hết —