2017-05-24 5 views
1

私は今日、tfおよび/またはtfidfがRで壊れているようだと認識しました。以下の私の例を見てください。マニュアルのデータ、すなわちcrudeを使用しています。私は、結果として得られる頻出用語リストが等しくないことを期待しています。しかし、彼らは平等です。これは決して起こるべきではないでしょうか?R:weightTfとweightTfIdfは同じ頻出語リストを生成しますか?

data("crude") 

dtm <- DocumentTermMatrix(crude, control = list(weighting = function(x) weightTf, stopwords = FALSE)) 
frequentTerms1 <- data.frame(findFreqTerms(dtm)[1:1000]) 
#View(frequentTerms1) 


dtm <- DocumentTermMatrix(crude, control = list(weighting = function(x) weightTfIdf(x, normalize = FALSE), stopwords = FALSE)) 
frequentTerms2 <- data.frame(findFreqTerms(dtm)[1:1000]) 
#View(frequentTerms2) 

frequentTerms1 == frequentTerms2 

サンプルコードに間違いがありますか?私は基礎となるtmパッケージのマニュアルからそれをコピーし、比較としてtfのケースを追加しました。

ありがとうございました。

敬具 トルステン


編集#1: さて、答えてくれてありがとうlukeA。それは大いに役立ちます。したがって、頻繁な用語を取得する「正しい」方法は次のとおりです。

data("crude") 

dtm <- DocumentTermMatrix(crude, control = list(weighting = function(x) weightTf, stopwords = FALSE)) 
frequentTerms1 <- as.data.frame(sort(colSums(as.matrix(dtm)), decreasing = TRUE)) 
#View(frequentTerms1) 


dtm <- DocumentTermMatrix(crude, control = list(weighting = function(x) weightTfIdf(x, normalize = FALSE), stopwords = FALSE)) 
frequentTerms2 <- as.data.frame(sort(colSums(as.matrix(dtm)), decreasing = TRUE)) 
#View(frequentTerms2) 

frequentTerms1 == frequentTerms2 

ここでは、両方のリストが異なっています。

答えて

3

デフォルトでは、findFreqTermsは、転置されたdocument-term-matrix(= term-document-matrix)の行合計が0以上で無限より小さいか等しいかどうかをチェックします。これは、周波数重み付けとtfidf重み付けを使用するすべての用語に当てはまります。あなたは、たとえば、別の最低周波数を指定した場合、今

txts <- c("Hello super World", "Hello World World") 
corp <- VCorpus(VectorSource(txts)) 
tf <- DocumentTermMatrix(corp, control=list(weighting=weightTf)) 
tfidf <- DocumentTermMatrix(corp, control=list(weighting=weightTfIdf)) 

all(findFreqTerms(tf)==findFreqTerms(tfidf)) 
# [1] TRUE 

:ここでは一例だと

findFreqTerms(tf, lowfreq = 1) 
# [1] "hello" "super" "world" 
findFreqTerms(tfidf, lowfreq = 0.33) 
# [1] "super" 

as.matrix(tf) 
#  Terms 
# Docs hello super world 
# 1  1  1  1 
# 2  1  0  2 

as.matrix(tfidf) 
#  Terms 
# Docs hello  super world 
# 1  0 0.3333333  0 
# 2  0 0.0000000  0 
関連する問題