Vorhersagen und bewerten

Maschinelles Lernen mit baumbasierten Modellen in R

Sandro Raabe

Data Scientist

Vorhersagen für neue Daten

Allgemeiner Aufruf:
predict(model, new_data, type)
Argumente:
  1. Trainiertes Modell
  2. Datensatz für Vorhersagen
  3. Vorhersagetyp: Labels oder Wahrscheinlichkeiten
Maschinelles Lernen mit baumbasierten Modellen in R

Vorhersagen für neue Daten

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
Maschinelles Lernen mit baumbasierten Modellen in R

Konfusionsmatrix

Konfusionsmatrix_1

  • Zeigt, wie stark ein Modell „verwirrt" ist
Maschinelles Lernen mit baumbasierten Modellen in R

Konfusionsmatrix

Konfusionsmatrix_2

Maschinelles Lernen mit baumbasierten Modellen in R

Konfusionsmatrix

Konfusionsmatrix_3

Maschinelles Lernen mit baumbasierten Modellen in R

Konfusionsmatrix

Konfusionsmatrix_4

  • Diagonale: korrekte Vorhersagen
  • Nebendiagonale: falsche Vorhersagen
Maschinelles Lernen mit baumbasierten Modellen in R

Konfusionsmatrix

 

  • TP: Vorhersage yes, Wahrheit yes
  • TN: Vorhersage no, Wahrheit no
  • FP: Vorhersage yes, Wahrheit no
  • FN: Vorhersage no, Wahrheit yes

Konfusionsmatrix_4

Maschinelles Lernen mit baumbasierten Modellen in R

Konfusionsmatrix erstellen

# Vorhersagen und Wahrheitswerte kombinieren
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
# Konfusionsmatrix berechnen
conf_mat(data = pred_combined,

estimate = .pred_class,
truth = true_class)
             Truth
Prediction    no   yes
        no   116    31
       yes    12    40
Maschinelles Lernen mit baumbasierten Modellen in R

Accuracy

  $$\text{accuracy} = \frac{\text{Anz. korrekter Vorhersagen}}{\text{Anz. aller Vorhersagen}}$$

  • Funktionsname: accuracy()
  • Gleiche Argumente wie bei conf_mat()
    • data, estimate und truth
    • Übliche Struktur in yardstick
accuracy(pred_combined,
         estimate = .pred_class,
         truth = true_class)
  .metric     .estimate
  <chr>           <dbl>
1 accuracy        0.708
Maschinelles Lernen mit baumbasierten Modellen in R

Lass uns bewerten!

Maschinelles Lernen mit baumbasierten Modellen in R

Preparing Video For Download...