ツイートのトピックモデル化

Rで学ぶソーシャルメディアデータ分析

Vivek Vijayaraghavan

Data Science Coach

レッスン概要

  • トピックモデルの基礎
  • 文書-単語行列(DTM)の作成
  • DTM からトピックモデルを構築
Rで学ぶソーシャルメディアデータ分析

トピックと文書

トピックの定義と例

Rで学ぶソーシャルメディアデータ分析

トピックと文書

文書の定義と例

Rで学ぶソーシャルメディアデータ分析

トピックモデル

  • トピックを自動発見するタスク
  • 大規模データから主要トピックを抽出
  • 膨大な情報を素早く要約
Rで学ぶソーシャルメディアデータ分析

LDA の仕組み

  • トピックモデル化のための LDA(Latent Dirichlet Allocation)

LDA の仕組み

Rで学ぶソーシャルメディアデータ分析

LDA の仕組み

LDA の仕組み

Rで学ぶソーシャルメディアデータ分析

LDA の仕組み

LDA の仕組み

Rで学ぶソーシャルメディアデータ分析

文書-単語行列(DTM)

  • 文書-単語行列を作成
  • DTM はコーパスの行列表現
  • 行が文書、列が語(用語)

文書-単語行列(DTM)

Rで学ぶソーシャルメディアデータ分析

文書-単語行列を作成

# Create a document term matrix
dtm <- DocumentTermMatrix(twt_corpus_refined)
Rで学ぶソーシャルメディアデータ分析

文書-単語行列を作成

# Inspect the DTM
inspect(dtm)
Rで学ぶソーシャルメディアデータ分析

文書-単語行列を作成

<<DocumentTermMatrix (documents: 1000, terms: 5079)>>
Non-/sparse entries: 12862/5066138
Sparsity           : 100%
Maximal term length: 29
Weighting          : term frequency (tf)
Sample             :
     Terms
Docs    california child diabetes fat food health people ranks rates weight
  131          0     0        0   0    0      0      0     0     0      0
  161          0     0        0   2    0      0      0     0     0      1
  295          0     0        0   0    1      0      1     0     0      0
  418          0     0        0   0    0      0      0     0     1      0
  604          0     0        1   0    0      1      0     0     0      0
Rで学ぶソーシャルメディアデータ分析

DTM の前処理

  • 行合計が 0 より大きい行で DTM をフィルタ
# Find the sum of word counts in each Document 
rowTotals <- apply(dtm , 1, sum)
# Select rows from DTM with row totals greater than zero
tweet_dtm_new <- dtm[rowTotals> 0, ]
Rで学ぶソーシャルメディアデータ分析

トピックモデルを構築

  • LDA() 関数でトピックモデルを作成
# Build the topic model
library(topicmodels)
lda_5 <- LDA(tweet_dtm_new, k = 5)
Rで学ぶソーシャルメディアデータ分析

トピックモデルを構築

  • ツイートコーパスから 5 トピックを抽出
# View top 10 terms in the topic model
top_10terms <- terms(lda_5,10)
top_10terms
Rで学ぶソーシャルメディアデータ分析

上位 10 語を確認

     Topic 1        Topic 2        Topic 3     Topic 4      Topic 5   
 [1,] "disease"      "people"       "black"     "child"      "weight"  
 [2,] "health"       "health"       "fat"       "rates"      "diet"    
 [3,] "cancer"       "diabetes"     "trump"     "ranks"      "food"    
 [4,] "meghanmccain" "overweight"   "childhood" "california" "diabetes"
 [5,] "realcandaceo" "fat"          "health"    "fat"        "health"  
 [6,] "food"         "meghanmccain" "professor" "eat"        "bmi"     
 [7,] "risk"         "realcandaceo" "gender"    "people"     "problem" 
 [8,] "heart"        "body"         "studies"   "epidemic"   "eating"  
 [9,] "weight"       "weight"       "healthy"   "health"     "disease" 
[10,] "diabetes"     "obese"        "problem"   "healthy"    "family"
  • 肥満対策プログラムは中核トピックを軸に設計可能
Rで学ぶソーシャルメディアデータ分析

Ayo berlatih!

Rで学ぶソーシャルメディアデータ分析

Preparing Video For Download...