【发布时间】:2016-10-04 12:29:08
【问题描述】:
首先,这里是重现我遇到的问题的示例数据,我将尝试解释如下: https://drive.google.com/file/d/0B4RCdYlVF8otUll6V2x0cDJORGc/view?usp=sharing
问题是我从 removeSparseTerms 得到了不同的结果,尽管它引入了相同的值。它似乎违背了人类的逻辑,或者至少违背了我的逻辑。我有这个功能:
generateTDM <- function (Room_name, dest.train, RST){
s.dir <- sprintf("%s/%s", dest.train, Room_name)
s.cor <- Corpus(DirSource(directory = s.dir, pattern = "txt", encoding = "UTF-8")) #Crea unos corpora de los archivos txt ya limpios.
s.tdm <- TermDocumentMatrix(s.cor, control = list(bounds = list(local = c(2, Inf)), tokenize = TrigramTokenizer)) #Crea una matriz de terminos a partir de los corpora teniendo en cuenta unigramas, bigramas y trigramas.
s.tdm <- removeSparseTerms(s.tdm, RST) #Mantiene aquellos términos que aparezcan en el (1-RST)% de los archivos, el resto los elimina.
}
好吧,当我这样调用这个函数时:
tdm.train <- lapply(Room_name, generateTDM, dest.train, RST[p])
我获得了不同的输出函数,其中变量 RST 位于向量内,具体取决于其他元素。也就是说,尽管值相同,但我得到了不同的结果。
例如:
案例一:
RST <-seq (0.45, 0.6, 0.05)
p<-4
我将有 RST = (0.45, 0.5, 0.55, 0.6),那么 RST[p] 是 0.6。
这种情况下的结果:
> tdm.train
[[1]]
<<TermDocumentMatrix (terms: 84, documents: 51)>>
Non-/sparse entries: 2451/1833
Sparsity : 43%
Maximal term length: 10
Weighting : term frequency (tf)
[[2]]
<<TermDocumentMatrix (terms: 82, documents: 52)>>
Non-/sparse entries: 2409/1855
Sparsity : 44%
Maximal term length: 11
Weighting : term frequency (tf)
[[3]]
<<TermDocumentMatrix (terms: 68, documents: 51)>>
Non-/sparse entries: 1926/1542
Sparsity : 44%
Maximal term length: 13
Weighting : term frequency (tf)
[[4]]
<<TermDocumentMatrix (terms: 36, documents: 48)>>
Non-/sparse entries: 985/743
Sparsity : 43%
Maximal term length: 10
Weighting : term frequency (tf)
[[5]]
<<TermDocumentMatrix (terms: 48, documents: 50)>>
Non-/sparse entries: 1295/1105
Sparsity : 46%
Maximal term length: 10
Weighting : term frequency (tf)
[[6]]
<<TermDocumentMatrix (terms: 27, documents: 50)>>
Non-/sparse entries: 756/594
Sparsity : 44%
Maximal term length: 8
Weighting : term frequency (tf)
案例 2:
RST <-seq (0.45, 0.8, 0.05)
p<-4
我现在将得到 RST = (0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8),因此这次 RST[p] 是相同的 (0.6)。
那么,为什么我有不同的结果?我无法理解。
> tdm.train
[[1]]
<<TermDocumentMatrix (terms: 84, documents: 51)>>
Non-/sparse entries: 2451/1833
Sparsity : 43%
Maximal term length: 10
Weighting : term frequency (tf)
[[2]]
<<TermDocumentMatrix (terms: 82, documents: 52)>>
Non-/sparse entries: 2409/1855
Sparsity : 44%
Maximal term length: 11
Weighting : term frequency (tf)
[[3]]
<<TermDocumentMatrix (terms: 68, documents: 51)>>
Non-/sparse entries: 1926/1542
Sparsity : 44%
Maximal term length: 13
Weighting : term frequency (tf)
[[4]]
<<TermDocumentMatrix (terms: 36, documents: 48)>>
Non-/sparse entries: 985/743
Sparsity : 43%
Maximal term length: 10
Weighting : term frequency (tf)
[[5]]
<<TermDocumentMatrix (terms: 57, documents: 50)>>
Non-/sparse entries: 1475/1375
Sparsity : 48%
Maximal term length: 10
Weighting : term frequency (tf)
[[6]]
<<TermDocumentMatrix (terms: 34, documents: 50)>>
Non-/sparse entries: 896/804
Sparsity : 47%
Maximal term length: 8
Weighting : term frequency (tf)
我不知道……这很奇怪,对吧?如果RST的值相同,为什么最后两个dirs中removeSparseTerms的结果每次都不一样。请帮助我,不知道原因正在杀死我。
非常感谢您,祝您有愉快的一天。
基于 OP 更新的可重现示例:
library(tm)
library(RWeka)
download.file("https://docs.google.com/uc?authuser=0&id=0B4RCdYlVF8otUll6V2x0cDJORGc&export=download", tf <- tempfile(fileext = ".zip"), mode = "wb")
unzip(tf, exdir = tempdir())
TrigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 1, max = 3))
generateTDM <- function (Room_name, dest.train, rst){
s.dir <- sprintf("%s/%s", dest.train, Room_name)
s.cor <- Corpus(DirSource(directory = s.dir, pattern = "txt", encoding = "UTF-8")) #Crea unos corpora de los archivos txt ya limpios.
s.tdm <- TermDocumentMatrix(s.cor, control = list(bounds = list(local = c(2, Inf)), tokenize = TrigramTokenizer)) #Crea una matriz de terminos a partir de los corpora teniendo en cuenta unigramas, bigramas y trigramas.
t <- table(s.tdm$i) > (s.tdm$ncol * (1 - rst)) # from tm::removeSparseTerms()
termIndex <- as.numeric(names(t[t]))
return(s.tdm[termIndex, ])
}
dest.train <- file.path(tempdir(), "stackoverflow", "TrainDocs")
Room_name <- "Venus"
p <- 4
RST1 <- seq(0.45, 0.6, 0.05)
RST2 <- seq(0.45, 0.8, 0.05)
RST2[p]
# [1] 0.6
RST1[p]
# [1] 0.6
identical(RST2[p], RST1[p])
# [1] FALSE # ?!?
lapply(Room_name, generateTDM, dest.train, RST1[p])
# <<TermDocumentMatrix (terms: 48, documents: 50)>>
lapply(Room_name, generateTDM, dest.train, RST2[p])
# <<TermDocumentMatrix (terms: 57, documents: 50)>> # ?!?
【问题讨论】:
-
恕我直言,最好强调差异并提供示例数据以进行复制,而不是多次强调“我不知道......我无法理解”。 :-)
-
是的,没错。我将准备一个包含重要文件和部分脚本的 zip 文件,以便尽快附在此处。很抱歉。
-
完成。附加的示例数据和句子中的压力水平降低了......有点。 :)
-
有趣。
identical(RST2[p], RST1[p])是FALSE,我不明白,因此table(m$i) > m$ncol * (1 - sparse)在removeSparseTerms中的结果似乎略有不同。 -
感谢 lukeA 的时间和精力。您所做的可重现示例非常棒,而且更加清晰。谢谢你,我已经知道下次如何正确共享代码了。关于这个问题,我使用
all.equal和str来比较两个值(我不知道指令identical),所以我认为它们是相同的,因为应该是这样。真的,真的很奇怪。 :S
标签: r