Predikce a vyhodnocení

Machine Learning with Tree-Based Models in R

Sandro Raabe

Data Scientist

Predikce na nových datech

Obecné volání:
predict(model, new_data, type)
Argumenty:
  1. Natrénovaný model
  2. Datová sada pro predikci
  3. Typ predikce: třídy nebo pravděpodobnosti
Machine Learning with Tree-Based Models in R

Predikce na nových datech

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
Machine Learning with Tree-Based Models in R

Matice záměn

matice záměn 1

  • Ukazuje, jak moc se model mýlí
Machine Learning with Tree-Based Models in R

Matice záměn

matice záměn 2

Machine Learning with Tree-Based Models in R

Matice záměn

matice záměn 3

Machine Learning with Tree-Based Models in R

Matice záměn

matice záměn 4

  • Diagonála: správné predikce
  • Mimo diagonálu: chybné predikce
Machine Learning with Tree-Based Models in R

Matice záměn

 

  • TP: predikce je ano, skutečnost je ano
  • TN: predikce je ne, skutečnost je ne
  • FP: predikce je ano, skutečnost je ne
  • FN: predikce je ne, skutečnost je ano

matice záměn 4

Machine Learning with Tree-Based Models in R

Vytvoření matice záměn

# 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
Machine Learning with Tree-Based Models in R

Přesnost

  $$\text{accuracy} = \frac{\text{n of correct predictions}}{\text{n of total predictions}}$$

  • Název funkce: accuracy()
  • Stejné argumenty jako conf_mat()
    • data, estimate a truth
    • Společná struktura v yardstick
accuracy(pred_combined,
         estimate = .pred_class,
         truth = true_class)
  .metric     .estimate
  <chr>           <dbl>
1 accuracy        0.708
Machine Learning with Tree-Based Models in R

Pojďme hodnotit!

Machine Learning with Tree-Based Models in R

Preparing Video For Download...