분류 모델링

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...