오차행렬로 모델 평가 
  평가할때 오차행렬(confusion matrix) 사용함
 
  
    0을 0으로 1을 1로 예측하는 정확도가 중요함
   
  
    오차 행렬 평가지표
   
 
오차 행렬은 위와 같이 4분면 행렬에서 실제 label class과 예측 label class가 어떠한 유형을 가지고 mapping 되는지 나타낸다. 4분면의 왼쪽과 오른쪽은 에측된 class를 기준으로 Negative와 Positive로 분류하고, 위와 아래는 실제 class를 기준으로 Negative와 Positive로 분류한다. 따라서 예측 class와 실제 class에 따라 TN, FP, FN, TP 형태로 오차 행렬의 4분면을 채울 수 있다.
  
    TN은 예측값을 Negative 값인 0으로 예측했고 실제값 또한 Negative 값인 0일 때
   
  
    FP은 예측값을 Positive 값인 1으로 예측했는데 실제값은 Negative 값인 0일 때
   
  
    FN은 예측값을 Negative 값인 0으로 예측했는데 실제값은 Positive 값인 1일 때
   
  
    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.male gender.female region.Sudo region.Chungcheung region.Honam region.Youngnam region.Others edu income age score_gov score_progress score_intention vote 
	1         0         0         0         0         1         0         1.0       0.6666667 0.6666667 0.25      0.25      0.75      1         1         0         0         0         0         0         1         0.5       0.6666667 0.6666667 0.25      0.75      0.50      0         1         0         0         0         1         0         0         0.0       0.3333333 1.0000000 0.00      0.50      0.45      1         0         1         1         0         0         0         0         0.5       0.0000000 0.6666667 1.00      0.75      0.40      1         1         0         1         0         0         0         0         0.0       0.3333333 1.0000000 0.75      0.50      0.35      1         1         0         1         0         0         0         0         0.0       0.3333333 1.0000000 0.00      0.75      0.70      1         1         0         1         0         0         0         0         0.0       0.3333333 1.0000000 0.75      0.75      0.25      1         1         0         0         0         0         0         1         0.5       1.0000000 1.0000000 0.50      0.75      0.65      1         1         0         0         1         0         0         0         0.0       0.3333333 1.0000000 0.25      0.25      0.25      0         1         0         1         0         0         0         0         0.0       0.3333333 0.6666667 0.75      0.25      0.50      1         1         0         1         0         0         0         0         0.0       0.3333333 0.6666667 0.25      0.75      0.30      0         0         1         0         0         0         1         0         0.0       0.0000000 0.6666667 0.50      0.25      0.40      1         1         0         0         0         0         0         1         0.0       0.3333333 1.0000000 0.50      0.25      0.50      1         1         0         0         1         0         0         0         0.5       1.0000000 1.0000000 0.50      0.50      0.35      1         1         0         0         0         0         1         0         1.0       1.0000000 0.6666667 0.50      0.75      0.65      1         1         0         1         0         0         0         0         0.5       0.6666667 0.6666667 0.50      0.50      0.55      1         1         0         0         0         0         0         1         0.5       1.0000000 0.6666667 0.75      0.50      0.75      1         0         1         1         0         0         0         0         0.5       0.3333333 0.6666667 1.00      0.75      0.40      1         0         1         0         0         1         0         0         0.0       0.3333333 0.3333333 0.50      0.25      0.50      0         0         1         0         0         0         0         1         1.0       1.0000000 0.6666667 0.50      0.50      0.50      1         1         0         1         0         0         0         0         0.0       0.0000000 0.3333333 0.50      0.50      0.25      0         0         1         1         0         0         0         0         1.0       0.3333333 0.3333333 0.50      0.75      0.30      1         0         1         0         0         0         1         0         0.5       0.3333333 0.3333333 0.50      0.50      0.10      1         1         0         1         0         0         0         0         0.0       0.6666667 0.6666667 0.50      0.75      0.15      1         1         0         0         0         0         1         0         0.5       0.6666667 0.6666667 0.50      0.25      0.65      1         1         0         1         0         0         0         0         0.5       0.3333333 0.3333333 0.50      0.25      0.55      1         0         1         1         0         0         0         0         0.5       0.6666667 0.3333333 0.50      0.75      0.45      1         0         1         1         0         0         0         0         0.5       1.0000000 0.6666667 0.75      0.75      0.50      1         1         0         1         0         0         0         0         0.0       0.6666667 0.6666667 0.00      0.25      0.50      1         0         1         1         0         0         0         0         0.5       1.0000000 1.0000000 0.50      0.50      0.30      1         ... ... ... ... ... ... ... ... ... ... ... ... ... ... 1         0         1         0         0         0         0         0.5       0.0000000 0.3333333 0.50      1.00      0.30      1         1         0         0         1         0         0         0         0.5       0.0000000 0.3333333 0.75      0.50      0.60      1         0         1         1         0         0         0         0         0.0       0.0000000 0.0000000 0.75      0.25      0.60      1         1         0         0         0         0         1         0         0.0       0.0000000 0.3333333 0.00      0.75      0.45      1         0         1         1         0         0         0         0         0.0       0.0000000 0.0000000 0.50      0.75      0.45      1         0         1         1         0         0         0         0         0.0       0.0000000 0.3333333 0.25      0.75      0.50      0         1         0         0         0         0         0         1         0.5       0.3333333 0.6666667 0.75      0.50      0.60      0         1         0         1         0         0         0         0         0.5       0.0000000 0.0000000 0.50      0.50      0.45      1         1         0         1         0         0         0         0         0.0       0.0000000 0.3333333 0.50      0.50      0.50      1         1         0         0         0         0         0         1         0.0       0.0000000 0.0000000 0.25      0.25      0.40      0         0         1         1         0         0         0         0         0.5       0.0000000 0.6666667 0.75      0.75      0.85      1         0         1         0         1         0         0         0         0.0       0.0000000 0.0000000 0.50      0.75      0.45      0         1         0         0         0         0         0         1         0.0       0.0000000 0.3333333 0.50      0.75      0.60      1         1         0         0         1         0         0         0         0.5       0.0000000 0.3333333 0.25      0.00      0.30      1         1         0         0         0         0         0         1         0.0       0.0000000 0.0000000 0.50      0.50      0.50      0         0         1         1         0         0         0         0         0.5       0.3333333 0.6666667 0.25      0.75      0.50      1         0         1         1         0         0         0         0         0.0       0.0000000 0.6666667 0.25      0.50      0.50      1         1         0         1         0         0         0         0         0.5       0.0000000 0.3333333 0.75      0.75      1.00      1         1         0         1         0         0         0         0         0.0       0.0000000 0.3333333 0.50      0.25      0.30      1         0         1         1         0         0         0         0         0.5       0.0000000 0.3333333 0.75      0.50      0.50      1         0         1         1         0         0         0         0         0.5       0.0000000 0.3333333 0.75      0.75      0.65      0         1         0         1         0         0         0         0         0.5       0.0000000 0.3333333 0.50      0.50      0.60      1         0         1         1         0         0         0         0         0.5       0.3333333 0.3333333 0.50      0.50      0.65      1         0         1         1         0         0         0         0         0.0       0.0000000 0.0000000 1.00      0.75      0.55      0         0         1         1         0         0         0         0         0.0       0.6666667 1.0000000 0.50      0.25      0.00      1         1         0         0         0         0         1         0         0.0       1.0000000 1.0000000 0.50      0.50      0.20      1         1         0         1         0         0         0         0         0.5       0.0000000 0.3333333 0.50      0.75      0.40      1         1         0         0         1         0         0         0         0.5       0.0000000 0.3333333 0.50      0.50      0.40      1         1         0         1         0         0         0         0         0.5       0.6666667 1.0000000 0.50      0.25      0.75      1         0         1         1         0         0         0         0         0.5       0.3333333 0.3333333 0.50      0.50      0.70      1          
1
2
 data $ vote <- factor ( data $ vote ,   labels = c ( "no" ,   "yes" )) 
 head ( data ) 
 
gender.male gender.female region.Sudo region.Chungcheung region.Honam region.Youngnam region.Others edu income age score_gov score_progress score_intention vote 
	1         0         0         0         0         1         0         1.0       0.6666667 0.6666667 0.25      0.25      0.75      yes       1         0         0         0         0         0         1         0.5       0.6666667 0.6666667 0.25      0.75      0.50      no        1         0         0         0         1         0         0         0.0       0.3333333 1.0000000 0.00      0.50      0.45      yes       0         1         1         0         0         0         0         0.5       0.0000000 0.6666667 1.00      0.75      0.40      yes       1         0         1         0         0         0         0         0.0       0.3333333 1.0000000 0.75      0.50      0.35      yes       1         0         1         0         0         0         0         0.0       0.3333333 1.0000000 0.00      0.75      0.70      yes        
train / test dataset 나누기 
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 
 
 
      
Leave a comment