R中的removeSparseTerms如何工作?

Lon*_*guy 18 r lda tm

我在R中使用removeSparseTerms方法,它需要输入一个阈值.我还读到,值越高,返回矩阵中保留的项数就越多.

这种方法如何工作以及它背后的逻辑是什么?我理解稀疏性的概念,但这个阈值是否表明一个术语应该出现多少文件,或者其他比例等等?

Ken*_*oit 47

在感sparse参数removeSparseTerms(),稀疏度指的阈值相对文档频率为一个术语,高于该术语将被移除.相对文档频率在这里表示比例.作为命令的帮助页面(尽管不是很清楚),稀疏度随着接近1.0 而变小.(请注意,稀疏性不能取值0或1.0,只能取值介于两者之间.)

例如,如果设置sparse = 0.99为参数removeSparseTerms(),那么这将只会删除方面超过0.99稀疏.确切的解释sparse = 0.99是,对于期限$ j $,您将保留$ df_j> N*(1 - 0.99)$的所有条款,其中$ N $是文档的数量 - 在这种情况下,可能所有条款都将是保留(见下面的例子).

接近另一个极端,如果sparse = .01,则只保留(几乎)每个文档中出现的术语.(当然这取决于术语的数量和文档的数量,并且在自然语言中,像"the"这样的常用词可能出现在每个文档中,因此永远不会"稀疏".)

稀疏度阈值为0.99的示例,其中一个术语最多出现在(第一个示例中)少于0.01个文档中,而(第二个示例)出现在0.01个以上的文档中:

> # second term occurs in just 1 of 101 documents
> myTdm1 <- as.DocumentTermMatrix(slam::as.simple_triplet_matrix(matrix(c(rep(1, 101), rep(1,1), rep(0, 100)), ncol=2)), 
+                                weighting = weightTf)
> removeSparseTerms(myTdm1, .99)
<<DocumentTermMatrix (documents: 101, terms: 1)>>
Non-/sparse entries: 101/0
Sparsity           : 0%
Maximal term length: 2
Weighting          : term frequency (tf)
> 
> # second term occurs in 2 of 101 documents
> myTdm2 <- as.DocumentTermMatrix(slam::as.simple_triplet_matrix(matrix(c(rep(1, 101), rep(1,2), rep(0, 99)), ncol=2)), 
+                                weighting = weightTf)
> removeSparseTerms(myTdm2, .99)
<<DocumentTermMatrix (documents: 101, terms: 2)>>
Non-/sparse entries: 103/99
Sparsity           : 49%
Maximal term length: 2
Weighting          : term frequency (tf)
Run Code Online (Sandbox Code Playgroud)

以下是一些实际文本和术语的其他示例:

> myText <- c("the quick brown furry fox jumped over a second furry brown fox",
              "the sparse brown furry matrix",
              "the quick matrix")

> require(tm)
> myVCorpus <- VCorpus(VectorSource(myText))
> myTdm <- DocumentTermMatrix(myVCorpus)
> as.matrix(myTdm)
    Terms
Docs brown fox furry jumped matrix over quick second sparse the
   1     2   2     2      1      0    1     1      1      0   1
   2     1   0     1      0      1    0     0      0      1   1
   3     0   0     0      0      1    0     1      0      0   1
> as.matrix(removeSparseTerms(myTdm, .01))
    Terms
Docs the
   1   1
   2   1
   3   1
> as.matrix(removeSparseTerms(myTdm, .99))
    Terms
Docs brown fox furry jumped matrix over quick second sparse the
   1     2   2     2      1      0    1     1      1      0   1
   2     1   0     1      0      1    0     0      0      1   1
   3     0   0     0      0      1    0     1      0      0   1
> as.matrix(removeSparseTerms(myTdm, .5))
    Terms
Docs brown furry matrix quick the
   1     2     2      0     1   1
   2     1     1      1     0   1
   3     0     0      1     1   1
Run Code Online (Sandbox Code Playgroud)

在最后一个示例中sparse = 0.34,仅保留了三分之二文档中出现的术语.

基于文档频率从文档术语矩阵中修剪术语的另一种方法是文本分析包quanteda.这里相同的功能不是指稀疏性,而是直接指代术语的文档频率(如tf-idf).

> require(quanteda)
> myDfm <- dfm(myText, verbose = FALSE)
> docfreq(myDfm)
     a  brown    fox  furry jumped matrix   over  quick second sparse    the 
     1      2      1      2      1      2      1      2      1      1      3 
> dfm_trim(myDfm, minDoc = 2)
Features occurring in fewer than 2 documents: 6 
Document-feature matrix of: 3 documents, 5 features.
3 x 5 sparse Matrix of class "dfmSparse"
       features
docs    brown furry the matrix quick
  text1     2     2   1      0     1
  text2     1     1   1      1     0
  text3     0     0   1      1     1
Run Code Online (Sandbox Code Playgroud)

这种用法对我来说似乎更直截了当.

  • 惊人的解释,谢谢.这应该进入R文档! (5认同)

小智 6

在函数中removeSparseTerms(),参数sparse = x表示:
“删除所有稀疏度大于阈值(x)的项”。
例如:removeSparseTerms(my_dtm, sparse = 0.90)表示删除语料库中稀疏度大于90%的所有术语。

例如,在大小为1000的语料库中出现仅说4次的术语,其出现频率为0.004 = 4/1000。

此词的稀疏度为(1000-4)/1000 = 1- 0.004 = 0.996 = 99.6%
因此,如果稀疏度阈值设置为sparse = 0.90,则该术语将被删除,因为其稀疏度(0.996)大于上限稀疏度(0.90)。
但是,如果稀疏度阈值设置为sparse = 0.999,则该术语将不会被删除,因为其稀疏度(0.996)低于上限稀疏度(0.999)。