본문 바로가기
AI-driven Methodology/Artificial Intelligence

[AI] 로지스틱 회귀 (Logistic Regression)

by goatlab 2022. 7. 10.
728x90
반응형
SMALL

로지스틱 회귀 (Logistic Regression)

 

 

선형 회귀에서 확장한 것으로 독립 변수의 선형 결합을 이용하여 사건의 발생 가능성을 예측하는데 사용되는 통계 기법이다.

 

numerical_derivative, sigmoid 함수 정의

 

import numpy as np
from datetime import datetime

np.random.seed(0)

def numerical_derivative(f, x):
    delta_x = 1e-4 # 0.0001
    grad = np.zeros_like(x)
    
    it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
    
    while not it.finished:
        idx = it.multi_index        
        tmp_val = x[idx]
        x[idx] = float(tmp_val) + delta_x
        fx1 = f(x) # f(x+delta_x)
        
        x[idx] = float(tmp_val) - delta_x 
        fx2 = f(x) # f(x-delta_x)
        grad[idx] = (fx1 - fx2) / (2*delta_x)
        
        x[idx] = tmp_val 
        it.iternext()   
        
    return grad
# 최종출력은 y = sigmoid(Wx+b) 이며, 손실함수는 cross-entropy로 나타냄
def sigmoid(z):
    return 1 / (1+np.exp(-z))

 

Data Definition

 

x_data = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]).reshape(10,1)   
t_data = np.array([0, 0, 0, 0,  0,  0,  1,  1,  1,  1]).reshape(10,1)

print("x_data.shape = ", x_data.shape, ", t_data.shape = ", t_data.shape)
x_data.shape =  (10, 1) , t_data.shape =  (10, 1)

 

Initialize Weights and Bias

 

W = np.random.rand(1,1)  
b = np.random.rand(1)  

print("W = ", W, ", W.shape = ", W.shape, ", b = ", b, ", b.shape = ", b.shape)
W =  [[0.5488135]] , W.shape =  (1, 1) , b =  [0.71518937] , b.shape =  (1,)

 

Define Loss Function and output (y)

 

def loss_func(x, t):
    delta = 1e-7    # log 무한대 발산 방지
    
    z = np.dot(x, W) + b
    y = sigmoid(z)
    
    # cross-entropy 
    return  -np.sum( t*np.log(y + delta) + (1-t)*np.log((1 - y)+delta ) ) 

# 손실함수 값 계산 함수
# 입력변수 x, t : numpy type
def error_val(x, t):
    delta = 1e-7    # log 무한대 발산 방지
    
    z = np.dot(x, W) + b
    y = sigmoid(z)
    
    # cross-entropy 
    return  -np.sum( t*np.log(y + delta) + (1-t)*np.log((1 - y)+delta ) )

 

Learning

 

learning_rate = 1e-2  # 발산하는 경우, 1e-3 ~ 1e-6 등으로 바꾸어서 실행

f = lambda x : loss_func(x_data,t_data)  

print("Initial error value = ", error_val(x_data, t_data) )

start_time = datetime.now()

for step in  range(100001):  
    W -= learning_rate * numerical_derivative(f, W)
    
    b -= learning_rate * numerical_derivative(f, b)
    
    if (step % 5000 == 0):
        print("step = ", step, "error value = ", error_val(x_data, t_data) )
        
end_time = datetime.now()
        
print("")
print("Elapsed Time => ", end_time - start_time)
Initial error value =  27.572348887805205
step =  0 error value =  11.083107627560512
step =  5000 error value =  0.8460538852420929
step =  10000 error value =  0.6213212802190435
step =  15000 error value =  0.5096928691516133
step =  20000 error value =  0.4379216494151207
step =  25000 error value =  0.38625342252958283
step =  30000 error value =  0.34660969259370533
step =  35000 error value =  0.31491869701805886
step =  40000 error value =  0.2888470707057566
step =  45000 error value =  0.2669359732255671
step =  50000 error value =  0.2482141370712009
step =  55000 error value =  0.23200351293423327
step =  60000 error value =  0.217812927336217
step =  65000 error value =  0.20527589515576555
step =  70000 error value =  0.19411226769561546
step =  75000 error value =  0.18410353081080733
step =  80000 error value =  0.1750763101296366
step =  85000 error value =  0.16689101622278724
step =  90000 error value =  0.15943382228981376
step =  95000 error value =  0.15261086776663912
step =  100000 error value =  0.14634398759497833

Elapsed Time =>  0:00:14.077175

 

Evaluate and Predict

 

# 학습을 마친 후, 임의의 데이터에 대해 미래 값 예측 함수
# 입력변수 test_data : numpy type
def predict(test_data):
    z = np.dot(test_data, W) + b
    y = sigmoid(z)
    
    if y >= 0.5:
        result = 1  # True
    else:
        result = 0  # False
    
    return y, result
test_data = np.array([13.0])

(real_val, logical_val) = predict(test_data)

print(real_val, logical_val)
[0.52082848] 1
728x90
반응형
LIST