这种对稀疏性的解释准确吗?


10

根据软件包removeSparseTerms功能的文档,tm稀疏性是这样的:

A term-document matrix where those terms from x are removed which have at least a sparse percentage of empty (i.e., terms occurring 0 times in a document) elements. I.e., the resulting matrix contains only terms with a sparse factor of less than sparse.

那么,是否正确解释是说如果sparse等于.99,那么我们要删除仅出现在最多1%数据中的术语?


这个问题更适合Stackoverflow,那里有用于tm和文本挖掘的标签。
肯·贝努瓦

Answers:


16

是的,尽管您在这里的困惑是可以理解的,因为在这种情况下很难清楚地定义术语“稀疏”。

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

因此,您的解释是正确的,因为这sparse = 0.99只会删除比0.99 稀疏的术语。确切的解释sparse = 0.99是,对于术语,您将保留所有术语 ,其中是文档数-在这种情况下,可能会保留所有术语(请参见下面的示例) 。d f j > N 1 0.99 Njdfj>N(10.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)

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

> 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

在带有的最后一个示例中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 
> 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

在我看来,这种用法要简单得多。


1
欢迎来到肯(Ken)网站。感谢您的出色回答。希望我们能看到更多。
Glen_b-恢复莫妮卡2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.