分類モデリング

Rで学ぶ自然言語処理入門

Kasey Jones

Research Data Scientist

手順の振り返り

  1. データの整形/準備

    • Boxer/Napoleonの文に絞る
    • 単語をクリーン化してトークン化
    • TF-IDF重みの文書-単語行列を作成
  2. 学習用/テスト用に分割

  3. 学習用データでモデル学習
  4. テストデータで精度を報告
Rで学ぶ自然言語処理入門

手順2: データ分割

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, ]
Rで学ぶ自然言語処理入門

ランダムフォレストモデル

Rで学ぶ自然言語処理入門

分類の例

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
Rで学ぶ自然言語処理入門

混同行列

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

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

Rで学ぶ自然言語処理入門

テストデータでの予測

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
  • boxerの精度: 14/18
  • napoleonの精度: 10/12
  • 総合精度: 24/30 = 80%
Rで学ぶ自然言語処理入門

分類の練習

Rで学ぶ自然言語処理入門

Preparing Video For Download...