Classificatiemodellering

Introductie tot Natural Language Processing in R

Kasey Jones

Research Data Scientist

Overzicht van de stappen

  1. Data opschonen/voorbereiden

    • Filter op zinnen van Boxer/Napoleon
    • Schone tokens van woorden gemaakt
    • Document-termmatrix met TF‑IDF weging gemaakt
  2. Trainings- en testsets maken

  3. Model trainen op de trainingsset
  4. Nauwkeurigheid rapporteren op de testset
Introductie tot Natural Language Processing in R

Stap 2: data splitsen

set.seed(1111)
sample_size <- floor(0.80 * nrow(animal_matrix))
train_ind <- sample(nrow(animal_matrix), size = sample_size)
train <- animal_matrix[train_ind, ]
test <- animal_matrix[-train_ind, ]
Introductie tot Natural Language Processing in R

Random forest-modellen

Introductie tot Natural Language Processing in R

Classificatievoorbeeld

library(randomForest)
rfc <- randomForest(x = as.data.frame(as.matrix(train)), 
                    y = animal_sentences$Name[train_ind], nTree = 50)
rfc
Call:
 randomForest(...
        OOB estimate of  error rate: 23.33%
Confusion matrix:
         boxer napoleon class.error
boxer       37       20   0.3508772
napoleon     8       55   0.1269841
Introductie tot Natural Language Processing in R

De confusion matrix

Call:
 randomForest(...
        OOB estimate of  error rate: 23.33%
Confusion matrix:
         boxer napoleon class.error
boxer       37       20   0.3508772
napoleon     8       55   0.1269841

Nauwkeurigheid: (37 + 55) / (37 + 20 + 8 + 55) = 76%

Introductie tot Natural Language Processing in R

Voorspellingen op testset

y_pred <- predict(rfc, newdata = as.data.frame(as.matrix(test)))
table(animal_sentences[-train_ind, ]$Name, y_pred)
          y_pred
           boxer napoleon
  boxer       14        4
  napoleon     2       10
  • Nauwkeurigheid voor boxer: 14/18
  • Nauwkeurigheid voor napoleon: 10/12
  • Totale nauwkeurigheid: 24/30 = 80%
Introductie tot Natural Language Processing in R

Oefening classificatie

Introductie tot Natural Language Processing in R

Preparing Video For Download...