예측과 평가

R로 배우는 트리 기반 Machine Learning

Sandro Raabe

Data Scientist

새 데이터 예측

일반 호출:
predict(model, new_data, type)
인수:
  1. 학습된 모델
  2. 예측할 데이터셋
  3. 예측 유형: 레이블 또는 확률
R로 배우는 트리 기반 Machine Learning

새 데이터 예측

predict(model, new_data = test_data, 
               type = "class")
  .pred_class
  <fct>      
1 no         
2 no         
3 yes        
4 no
predict(model, new_data = test_data, 
               type = "prob")
     .pred_no  .pred_yes
     <dbl>     <dbl>
1    0.866     0.134
2    0.956     0.044
3    0.672     0.328
4    0.877     0.123
R로 배우는 트리 기반 Machine Learning

혼동 행렬

혼동 행렬_1

  • 모델의 혼동 정도를 보여줍니다
R로 배우는 트리 기반 Machine Learning

혼동 행렬

혼동 행렬_2

R로 배우는 트리 기반 Machine Learning

혼동 행렬

혼동 행렬_3

R로 배우는 트리 기반 Machine Learning

혼동 행렬

혼동 행렬_4

  • 대각선: 정확한 예측
  • 비대각선: 잘못된 예측
R로 배우는 트리 기반 Machine Learning

혼동 행렬

 

  • TP: 예측 , 실제
  • TN: 예측 아니오, 실제 아니오
  • FP: 예측 , 실제 아니오
  • FN: 예측 아니오, 실제

혼동 행렬_4

R로 배우는 트리 기반 Machine Learning

혼동 행렬 생성

# Combine predictions and truth values
pred_combined <- predictions %>% 
   mutate(true_class = test_data$outcome)

pred_combined
  .pred_class  true_class
  <fct>        <fct>     
1 no           no        
2 no           yes       
3 no           no        
4 yes          yes
# Calculate the confusion matrix
conf_mat(data = pred_combined,

estimate = .pred_class,
truth = true_class)
             Truth
Prediction    no   yes
        no   116    31
       yes    12    40
R로 배우는 트리 기반 Machine Learning

정확도

  $$\text{accuracy} = \frac{\text{정확한 예측 수}}{\text{전체 예측 수}}$$

  • 함수명: accuracy()
  • conf_mat()과 동일한 인수
    • data, estimate, truth
    • yardstick의 공통 구조
accuracy(pred_combined,
         estimate = .pred_class,
         truth = true_class)
  .metric     .estimate
  <chr>           <dbl>
1 accuracy        0.708
R로 배우는 트리 기반 Machine Learning

평가해 봅시다!

R로 배우는 트리 기반 Machine Learning

Preparing Video For Download...