Lecture 11: 頻度重みづけTF-IDF

getFreqMtxDir関数の読み込み

source("getFreqMtxDir.R")

getFreqMtxDir()の実行

テキストファイルが格納されているフォルダを指定

res <- getFreqMtxDir("msgs", encoding = "sjis")

頻度数の重み付け

Term Frequency-Inverse Document Frequency

複数のテキストに共通して出現する単語の低く評価

TF-IDF 1

\[ w=tf*log(\frac{N}{df}) \]

tf: term frequency

df: document frequency

tf値(頻度数)を計算

tf <- getFreqMtxDir("testData")

N値(ドキュメント数)を計算

N <- ncol(tf)

df値(1行あたり0以上の要素数)を計算

df <- apply(tf, 1, function(x) length(x[x > 0]))

tf-idf値を計算

w <- tf * log(N/df)
w
##   test1 test2 test3
## a 0.000 0.000 0.000
## b 1.622 1.622 0.000
## c 0.000 0.000 0.000
## d 0.000 0.000 1.099
## e 0.000 0.000 0.000
## f 0.000 4.460 3.649
## g 0.000 2.838 2.838
## h 0.000 0.000 4.394

TF-IDF 2

共通出現頻度も考慮したtf-idf式

\[ w=tf*(log(\frac{N}{df})+1) \]

tf-idf値を計算

w <- tf * (log(N/df) + 1)
w
##    test1  test2  test3
## a  3.000  2.000  2.000
## b  5.622  5.622  0.000
## c 13.000  2.000  3.000
## d  0.000  0.000  2.099
## e  7.000  1.000  1.000
## f  0.000 15.460 12.649
## g  0.000  9.838  9.838
## h  0.000  0.000  8.394

msg実行例

res <- getFreqMtxDir("msgs", tfidf = 1)
round(res, 2)[1:5, ]
##       ICU kyotoU tokyoU tufs
## 000  1.39   0.00      0 0.00
## 10   0.00   1.39      0 0.00
## 150  0.00   0.00      0 1.39
## 18   1.39   0.00      0 0.00
## 1890 1.39   0.00      0 0.00

相関係数

ピアソン積率相関係数

\[ Corr(x,y)= \frac{\sum (x_{i}-\overline{x}) (y_{i}-\overline{y})}{\sqrt{\sum (x_{i}-\overline{x})^2\sum (y_{i}-\overline{y})^2}} \]

\[ -1 \leq Corr(x,y) \leq 1 \]

  Corr(x,y) = 1: 正の相関(類似)
  Corr(x,y) = -1: 負の相関
  Corr(x,y) = 0: 無相関(関係なし)

相関係数行列(テキスト間)

tf <- getFreqMtxDir("testData")
cor(tf)
##         test1   test2   test3
## test1  1.0000 -0.2876 -0.3798
## test2 -0.2876  1.0000  0.7967
## test3 -0.3798  0.7967  1.0000

相関係数行列(単語間)

行と列を転置する

t(tf)
##       a b  c d e  f g h
## test1 3 4 13 0 7  0 0 0
## test2 2 4  2 0 1 11 7 0
## test3 2 0  3 1 1  9 7 4
round(cor(t(tf)), 2)
##       a     b     c     d     e     f     g     h
## a  1.00  0.50  1.00 -0.50  1.00 -0.99 -1.00 -0.50
## b  0.50  1.00  0.43 -1.00  0.50 -0.34 -0.50 -1.00
## c  1.00  0.43  1.00 -0.43  1.00 -1.00 -1.00 -0.43
## d -0.50 -1.00 -0.43  1.00 -0.50  0.34  0.50  1.00
## e  1.00  0.50  1.00 -0.50  1.00 -0.99 -1.00 -0.50
## f -0.99 -0.34 -1.00  0.34 -0.99  1.00  0.99  0.34
## g -1.00 -0.50 -1.00  0.50 -1.00  0.99  1.00  0.50
## h -0.50 -1.00 -0.43  1.00 -0.50  0.34  0.50  1.00
# res1

散布図(test1, test2)

plot(tf[, 1], tf[, 2], type = "n", xlab = "test1", ylab = "test2")
text(tf[, 1], tf[, 2], rownames(tf))
cor(tf[, 1], tf[, 2])
## [1] -0.2876
mtext(paste("corr = ", round(cor(tf[, 1], tf[, 2]), 2)), side = 3)

plot of chunk unnamed-chunk-11

散布図(test1, test3)

plot(tf[, 1], tf[, 3], type = "n", xlab = "test1", ylab = "test3")
text(tf[, 1], tf[, 3], rownames(tf))
cor(tf[, 1], tf[, 3])
## [1] -0.3798
mtext(paste("corr = ", round(cor(tf[, 1], tf[, 3]), 2)), side = 3)

plot of chunk unnamed-chunk-12

msgsフォルダー内テキストの相関係数

res <- getFreqMtxDir("msgs", encoding = "sjis")
round(cor(res), 2)
##         ICU kyotoU tokyoU tufs
## ICU    1.00    0.8   0.84 0.76
## kyotoU 0.80    1.0   0.80 0.80
## tokyoU 0.84    0.8   1.00 0.80
## tufs   0.76    0.8   0.80 1.00

msgsフォルダー内テキストの相関係数(TF-IDF1)

res <- getFreqMtxDir("msgs", encoding = "sjis", tfidf = 1)
round(cor(res), 2)
##          ICU kyotoU tokyoU  tufs
## ICU     1.00  -0.22  -0.19 -0.16
## kyotoU -0.22   1.00  -0.15 -0.18
## tokyoU -0.19  -0.15   1.00 -0.11
## tufs   -0.16  -0.18  -0.11  1.00

msgsフォルダー内テキストの相関係数(TF-IDF2)

res <- getFreqMtxDir("msgs", encoding = "sjis", tfidf = 2)
round(cor(res), 2)
##         ICU kyotoU tokyoU tufs
## ICU    1.00   0.53   0.60 0.44
## kyotoU 0.53   1.00   0.62 0.49
## tokyoU 0.60   0.62   1.00 0.54
## tufs   0.44   0.49   0.54 1.00

共通出現単語を抽出

res <- getFreqMtxDir("msgs", encoding = "sjis")
df <- apply(res, 1, function(x) length(x[x > 0]))
df[1:5]
##  000   10  150   18 1890 
##    1    1    1    1    1
df[df == 4]
##          a        and        are         as         at         by 
##          4          4          4          4          4          4 
##  education        for      human         in         is         it 
##          4          4          4          4          4          4 
##        its         of   research       that        the       this 
##          4          4          4          4          4          4 
##    through         to university       will       with      world 
##          4          4          4          4          4          4
names(df[df == 4])
##  [1] "a"          "and"        "are"        "as"         "at"        
##  [6] "by"         "education"  "for"        "human"      "in"        
## [11] "is"         "it"         "its"        "of"         "research"  
## [16] "that"       "the"        "this"       "through"    "to"        
## [21] "university" "will"       "with"       "world"

変数dfから1-2テキスト共通単語を抽出

df[df <= 2]

変数refから1-2テキスト共通単語を抽出

res[rownames(res) %in% names(df[df <= 2]), ]

練習(自分で考えてみましょう)

変数dfから3テキスト以上の共通単語を抽出

出力結果(一部抜粋)

##          a   academia   academic activities         an        and 
##          4          3          3          3          3          4 
##      april        are         as         at 
##          3          4          4          4

練習(自分で考えてみましょう)

3テキスト以上の共通単語だけのテキスト頻度行列を抽出

出力結果(一部抜粋)

##            ICU kyotoU tokyoU tufs
## a           10     21     10    8
## academia     0      1      1    1
## academic     1      6      4    0
## activities   0      3      2    1
## an           5      7      0    2
## and         23     38     22   34
## april        2      1      0    1
## are          4      5      3    2
## as           4      7      6    2
## at           4      1      2    1

変数dfから2-3テキスト共通単語を抽出

出力結果(一部抜粋)

df[df >= 2 & df <= 3][1:10]
##           60      ability     academia     academic accumulation 
##            2            2            3            3            2 
##       across   activities     addition        after          all 
##            2            3            2            2            2

練習(自分で考えてみましょう)

2-3テキスト共通単語だけのテキスト頻度行列を抽出

出力結果(一部抜粋)

##              ICU kyotoU tokyoU tufs
## 60             1      0      0    1
## ability        0      1      0    1
## academia       0      1      1    1
## academic       1      6      4    0
## accumulation   0      0      1    1

練習(自分で考えてみましょう)

2-3テキスト共通単語だけのテキスト頻度行列をもとに、テキストの相関係数を計算

出力結果

##          ICU kyotoU tokyoU tufs
## ICU     1.00   0.12  -0.18 0.02
## kyotoU  0.12   1.00   0.09 0.00
## tokyoU -0.18   0.09   1.00 0.02
## tufs    0.02   0.00   0.02 1.00