예측 계산과 설명

Python으로 배우는 Generalized Linear Models

Ita Cirovic Donev

Data Science Consultant

예측 계산하기

모델 적합 후

  1. 원래 $x$에 대한 적합값

모델 적합 과정 다이어그램.

Python으로 배우는 Generalized Linear Models

예측 계산하기

모델 적합 후

  1. 원래 $x$에 대한 적합값

  2. 새로운 $x$에 대한 예측값

모델 적합 및 예측 계산 다이어그램.

Python으로 배우는 Generalized Linear Models

예측 계산하기

  • 말굽게 모델 y ~ weight $$ \mu = \frac{\exp(-3.6947+1.8151 \times weight)}{1+\exp(-3.6947+1.8151 \times weight)} $$

  • 새 측정: weight = 2.85

$$ \mu = \frac{\exp(-3.6947+1.8151 \times \color{blue}{2.85})}{1+\exp(-3.6947+1.8151 \times \color{blue}{2.85})} = 0.814 $$

Python으로 배우는 Generalized Linear Models

Python에서 예측

  • 데이터셋 new_data에 대해 모델 예측 계산
    # Compute model predictions
    model_GLM.predict(exog = new_data)
    
Python으로 배우는 Generalized Linear Models

확률에서 클래스까지

정의된 확률 임계값에 따른 모델 출력 분리.

Python으로 배우는 Generalized Linear Models

클래스 예측 계산하기

# Extract fitted probabilities from model
crab['fitted'] = model.fittedvalues.values
# Define cut-off value
cut_off = 0.4
# Compute class predictions
crab['pred_class'] = np.where(crab['fitted'] > cut_off, 1, 0)
Python으로 배우는 Generalized Linear Models

클래스 예측 계산하기

# Count occurences for each class
crab['pred_class'].value_counts()
1    151
0     22
Cut-off $\hat y=1$ $\hat y=0$
$\mu = 0.4$ 151 22
$\mu = 0.5$ 126 47
Python으로 배우는 Generalized Linear Models

혼동 행렬

빈 혼동 행렬.

Python으로 배우는 Generalized Linear Models

혼동 행렬 - 진음성

혼동 행렬에서 진음성(TN) 표시.

Python으로 배우는 Generalized Linear Models

혼동 행렬 - 진양성

혼동 행렬에서 진음성과 진양성(TP) 표시.

Python으로 배우는 Generalized Linear Models

혼동 행렬 - 위양성

혼동 행렬에서 진음성, 진양성, 위양성(FP) 표시.

Python으로 배우는 Generalized Linear Models

혼동 행렬 - 위음성

혼동 행렬에서 진음성, 진양성, 위양성, 위음성(FN) 표시.

Python으로 배우는 Generalized Linear Models

Python에서 혼동 행렬

print(pd.crosstab(y_actual, y_predicted, 
            rownames=['Actual'], colnames=['Predicted'], 
            margins = True))
Predicted   0    1  All
Actual                 
0          15   47   62
1           7  104  111
All        22  151  173
Python으로 배우는 Generalized Linear Models

연습해 봅시다!

Python으로 배우는 Generalized Linear Models

Preparing Video For Download...