오차행렬로 모델 평가

  • 훈련데이터의 정확도와 테스트데이터의 정확도가 높아야 하고 차이가 적을수록 좋은 모델이다.(일반화 가능성이 높은 결과)

  • 실제와 예측이 얼마나 일치하는가라는 관점이 모델의 성능을 평가할때 가장 중요함

평가할때 오차행렬(confusion matrix) 사용함

  1. 0을 0으로 1을 1로 예측하는 정확도가 중요함

  2. 오차 행렬 평가지표

confusionmatrix1

오차 행렬은 위와 같이 4분면 행렬에서 실제 label class과 예측 label class가 어떠한 유형을 가지고 mapping 되는지 나타낸다. 4분면의 왼쪽과 오른쪽은 에측된 class를 기준으로 Negative와 Positive로 분류하고, 위와 아래는 실제 class를 기준으로 Negative와 Positive로 분류한다. 따라서 예측 class와 실제 class에 따라 TN, FP, FN, TP 형태로 오차 행렬의 4분면을 채울 수 있다.

  1. TN은 예측값을 Negative 값인 0으로 예측했고 실제값 또한 Negative 값인 0일 때

  2. FP은 예측값을 Positive 값인 1으로 예측했는데 실제값은 Negative 값인 0일 때

  3. FN은 예측값을 Negative 값인 0으로 예측했는데 실제값은 Positive 값인 1일 때

  4. TP은 예측값을 Positive 값인 1으로 예측했고 실제값 또한 Positive 값인 1일 때

정밀도(precision) = TP/(TP+FP)

재현율(recall) = TP/(TP+FN)

F-score = 2/ ((1/정밀도) + (1/재현율))

실습

1
2
3
data<-read.csv("data/Fvote.csv", header=TRUE)
data<-data[ , 2:15]
data
gender.malegender.femaleregion.Sudoregion.Chungcheungregion.Honamregion.Youngnamregion.Otherseduincomeagescore_govscore_progressscore_intentionvote
1 0 0 0 0 1 0 1.0 0.66666670.66666670.25 0.25 0.75 1
1 0 0 0 0 0 1 0.5 0.66666670.66666670.25 0.75 0.50 0
1 0 0 0 1 0 0 0.0 0.33333331.00000000.00 0.50 0.45 1
0 1 1 0 0 0 0 0.5 0.00000000.66666671.00 0.75 0.40 1
1 0 1 0 0 0 0 0.0 0.33333331.00000000.75 0.50 0.35 1
1 0 1 0 0 0 0 0.0 0.33333331.00000000.00 0.75 0.70 1
1 0 1 0 0 0 0 0.0 0.33333331.00000000.75 0.75 0.25 1
1 0 0 0 0 0 1 0.5 1.00000001.00000000.50 0.75 0.65 1
1 0 0 1 0 0 0 0.0 0.33333331.00000000.25 0.25 0.25 0
1 0 1 0 0 0 0 0.0 0.33333330.66666670.75 0.25 0.50 1
1 0 1 0 0 0 0 0.0 0.33333330.66666670.25 0.75 0.30 0
0 1 0 0 0 1 0 0.0 0.00000000.66666670.50 0.25 0.40 1
1 0 0 0 0 0 1 0.0 0.33333331.00000000.50 0.25 0.50 1
1 0 0 1 0 0 0 0.5 1.00000001.00000000.50 0.50 0.35 1
1 0 0 0 0 1 0 1.0 1.00000000.66666670.50 0.75 0.65 1
1 0 1 0 0 0 0 0.5 0.66666670.66666670.50 0.50 0.55 1
1 0 0 0 0 0 1 0.5 1.00000000.66666670.75 0.50 0.75 1
0 1 1 0 0 0 0 0.5 0.33333330.66666671.00 0.75 0.40 1
0 1 0 0 1 0 0 0.0 0.33333330.33333330.50 0.25 0.50 0
0 1 0 0 0 0 1 1.0 1.00000000.66666670.50 0.50 0.50 1
1 0 1 0 0 0 0 0.0 0.00000000.33333330.50 0.50 0.25 0
0 1 1 0 0 0 0 1.0 0.33333330.33333330.50 0.75 0.30 1
0 1 0 0 0 1 0 0.5 0.33333330.33333330.50 0.50 0.10 1
1 0 1 0 0 0 0 0.0 0.66666670.66666670.50 0.75 0.15 1
1 0 0 0 0 1 0 0.5 0.66666670.66666670.50 0.25 0.65 1
1 0 1 0 0 0 0 0.5 0.33333330.33333330.50 0.25 0.55 1
0 1 1 0 0 0 0 0.5 0.66666670.33333330.50 0.75 0.45 1
0 1 1 0 0 0 0 0.5 1.00000000.66666670.75 0.75 0.50 1
1 0 1 0 0 0 0 0.0 0.66666670.66666670.00 0.25 0.50 1
0 1 1 0 0 0 0 0.5 1.00000001.00000000.50 0.50 0.30 1
..........................................
1 0 1 0 0 0 0 0.5 0.00000000.33333330.50 1.00 0.30 1
1 0 0 1 0 0 0 0.5 0.00000000.33333330.75 0.50 0.60 1
0 1 1 0 0 0 0 0.0 0.00000000.00000000.75 0.25 0.60 1
1 0 0 0 0 1 0 0.0 0.00000000.33333330.00 0.75 0.45 1
0 1 1 0 0 0 0 0.0 0.00000000.00000000.50 0.75 0.45 1
0 1 1 0 0 0 0 0.0 0.00000000.33333330.25 0.75 0.50 0
1 0 0 0 0 0 1 0.5 0.33333330.66666670.75 0.50 0.60 0
1 0 1 0 0 0 0 0.5 0.00000000.00000000.50 0.50 0.45 1
1 0 1 0 0 0 0 0.0 0.00000000.33333330.50 0.50 0.50 1
1 0 0 0 0 0 1 0.0 0.00000000.00000000.25 0.25 0.40 0
0 1 1 0 0 0 0 0.5 0.00000000.66666670.75 0.75 0.85 1
0 1 0 1 0 0 0 0.0 0.00000000.00000000.50 0.75 0.45 0
1 0 0 0 0 0 1 0.0 0.00000000.33333330.50 0.75 0.60 1
1 0 0 1 0 0 0 0.5 0.00000000.33333330.25 0.00 0.30 1
1 0 0 0 0 0 1 0.0 0.00000000.00000000.50 0.50 0.50 0
0 1 1 0 0 0 0 0.5 0.33333330.66666670.25 0.75 0.50 1
0 1 1 0 0 0 0 0.0 0.00000000.66666670.25 0.50 0.50 1
1 0 1 0 0 0 0 0.5 0.00000000.33333330.75 0.75 1.00 1
1 0 1 0 0 0 0 0.0 0.00000000.33333330.50 0.25 0.30 1
0 1 1 0 0 0 0 0.5 0.00000000.33333330.75 0.50 0.50 1
0 1 1 0 0 0 0 0.5 0.00000000.33333330.75 0.75 0.65 0
1 0 1 0 0 0 0 0.5 0.00000000.33333330.50 0.50 0.60 1
0 1 1 0 0 0 0 0.5 0.33333330.33333330.50 0.50 0.65 1
0 1 1 0 0 0 0 0.0 0.00000000.00000001.00 0.75 0.55 0
0 1 1 0 0 0 0 0.0 0.66666671.00000000.50 0.25 0.00 1
1 0 0 0 0 1 0 0.0 1.00000001.00000000.50 0.50 0.20 1
1 0 1 0 0 0 0 0.5 0.00000000.33333330.50 0.75 0.40 1
1 0 0 1 0 0 0 0.5 0.00000000.33333330.50 0.50 0.40 1
1 0 1 0 0 0 0 0.5 0.66666671.00000000.50 0.25 0.75 1
0 1 1 0 0 0 0 0.5 0.33333330.33333330.50 0.50 0.70 1
1
2
data$vote<-factor(data$vote, labels=c("no", "yes"))
head(data)
gender.malegender.femaleregion.Sudoregion.Chungcheungregion.Honamregion.Youngnamregion.Otherseduincomeagescore_govscore_progressscore_intentionvote
1 0 0 0 0 1 0 1.0 0.66666670.66666670.25 0.25 0.75 yes
1 0 0 0 0 0 1 0.5 0.66666670.66666670.25 0.75 0.50 no
1 0 0 0 1 0 0 0.0 0.33333331.00000000.00 0.50 0.45 yes
0 1 1 0 0 0 0 0.5 0.00000000.66666671.00 0.75 0.40 yes
1 0 1 0 0 0 0 0.0 0.33333331.00000000.75 0.50 0.35 yes
1 0 1 0 0 0 0 0.0 0.33333331.00000000.00 0.75 0.70 yes

train / test dataset 나누기

1
2
library(caret)

1
2
3
4
set.seed(42)
# vote를 종속변수로 두기 (투표를 했는지 안했는지 예측)
training.samples <- createDataPartition(data$vote, p = 0.7, list = FALSE)
training.samples
Resample1
1
2
3
4
5
6
7
11
12
15
16
17
18
19
20
21
23
25
26
27
28
29
30
31
35
36
37
38
39
40
...
170
171
173
174
175
176
177
178
179
181
183
184
187
188
189
190
191
192
193
195
196
197
198
200
201
202
205
207
208
210
1
2
3

train  <- data[training.samples, ]
test <- data[-training.samples, ]
1
2
3
4
5
6
7
finalControl <- trainControl(method = "none", classProbs = TRUE)
FinalModel <- train(vote ~ ., data = train, 
                    method = "knn", 
                    trControl = finalControl, 
                    tuneGrid = data.frame(k=8),
                    metric = "Accuracy")
FinalModel
1
2
3
4
5
6
7
8
k-Nearest Neighbors 

148 samples
 13 predictor
  2 classes: 'no', 'yes' 

No pre-processing
Resampling: None 

모델 평가

훈련 모델의 예측 Class 측정(훈련데이터의 정확도)

1
train_pred<-predict(FinalModel, train)
1
2
3
# 오차행렬 적용
confusionMatrix(data = train_pred, reference = train$vote)
# 정확도 ->  Accuracy : 0.75             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Confusion Matrix and Statistics

          Reference
Prediction no yes
       no  19  13
       yes 24  92
                                          
               Accuracy : 0.75            
                 95% CI : (0.6722, 0.8175)
    No Information Rate : 0.7095          
    P-Value [Acc > NIR] : 0.1597          
                                          
                  Kappa : 0.344           
                                          
 Mcnemar's Test P-Value : 0.1002          
                                          
            Sensitivity : 0.4419          
            Specificity : 0.8762          
         Pos Pred Value : 0.5937          
         Neg Pred Value : 0.7931          
             Prevalence : 0.2905          
         Detection Rate : 0.1284          
   Detection Prevalence : 0.2162          
      Balanced Accuracy : 0.6590          
                                          
       'Positive' Class : no              
1
2
3
confusionMatrix(data = train_pred, reference = train$vote, mode = "prec_recall")
# mode = "prec_recall" 설정하면 precision,recall 조회됨

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Confusion Matrix and Statistics

          Reference
Prediction no yes
       no  19  13
       yes 24  92
                                          
               Accuracy : 0.75            
                 95% CI : (0.6722, 0.8175)
    No Information Rate : 0.7095          
    P-Value [Acc > NIR] : 0.1597          
                                          
                  Kappa : 0.344           
                                          
 Mcnemar's Test P-Value : 0.1002          
                                          
              Precision : 0.5938          
                 Recall : 0.4419          
                     F1 : 0.5067          
             Prevalence : 0.2905          
         Detection Rate : 0.1284          
   Detection Prevalence : 0.2162          
      Balanced Accuracy : 0.6590          
                                          
       'Positive' Class : no              
1
postResample(pred = train_pred, obs = train$vote)
1
2
3
4
5
6
7
8
9

Accuracy

0.75

Kappa

0.344034499281265

테스트 모델의 예측 Class 측정

1
test_pred<-predict(FinalModel, test)
1
2
confusionMatrix(data = test_pred, reference = test$vote)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Confusion Matrix and Statistics

          Reference
Prediction no yes
       no   3   8
       yes 15  37
                                         
               Accuracy : 0.6349         
                 95% CI : (0.504, 0.7527)
    No Information Rate : 0.7143         
    P-Value [Acc > NIR] : 0.9347         
                                         
                  Kappa : -0.0126        
                                         
 Mcnemar's Test P-Value : 0.2109         
                                         
            Sensitivity : 0.16667        
            Specificity : 0.82222        
         Pos Pred Value : 0.27273        
         Neg Pred Value : 0.71154        
             Prevalence : 0.28571        
         Detection Rate : 0.04762        
   Detection Prevalence : 0.17460        
      Balanced Accuracy : 0.49444        
                                         
       'Positive' Class : no             
1
2
confusionMatrix(data = test_pred, reference = test$vote, mode = "prec_recall")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Confusion Matrix and Statistics

          Reference
Prediction no yes
       no   3   8
       yes 15  37
                                         
               Accuracy : 0.6349         
                 95% CI : (0.504, 0.7527)
    No Information Rate : 0.7143         
    P-Value [Acc > NIR] : 0.9347         
                                         
                  Kappa : -0.0126        
                                         
 Mcnemar's Test P-Value : 0.2109         
                                         
              Precision : 0.27273        
                 Recall : 0.16667        
                     F1 : 0.20690        
             Prevalence : 0.28571        
         Detection Rate : 0.04762        
   Detection Prevalence : 0.17460        
      Balanced Accuracy : 0.49444        
                                         
       'Positive' Class : no             
1
postResample(pred = test_pred, obs = test$vote)
1
2
3
4
5
6
7
8
9

Accuracy

0.634920634920635

Kappa

-0.0125786163522012

Meta Info

Categories:

Published At:

Modified At:

Leave a comment