【问题标题】:Same value but different result? About removeSparseTerms (R)相同的值但不同的结果?关于 removeSparseTerms (R)
【发布时间】: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) &gt; m$ncol * (1 - sparse)removeSparseTerms 中的结果似乎略有不同。
  • 感谢 lukeA 的时间和精力。您所做的可重现示例非常棒,而且更加清晰。谢谢你,我已经知道下次如何正确共享代码了。关于这个问题,我使用all.equalstr 来比较两个值(我不知道指令identical),所以我认为它们是相同的,因为应该是这样。真的,真的很奇怪。 :S

标签: r


【解决方案1】:

问题似乎与热门问题“7.31 Why doesn’t R think these numbers are equal?”有关:

唯一可以在 R 的数字类型中精确表示的数字 是整数和分数,其分母是 2 的幂。所有 其他数字在内部四舍五入(通常)为 53 位二进制数字 准确性。结果,两个浮点数将不能可靠地 相等,除非它们是由相同的算法计算的,而不是 总是在那时

给定

(x <- seq(0.45, 0.6, 0.05))
# [1] 0.45 0.50 0.55 0.60
(y <- seq(0.45, 0.8, 0.05))
# [1] 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80

然后

x==y
# [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
x[4]==y[4]
# [1] FALSE
x[4]-y[4]
# [1] -1.110223e-16
x[3]-y[3]
# [1] 0

自从

MASS::as.fractions(x)
# [1]  9/20   1/2 11/20   3/5

我猜这两个.5 在这里确实相等。因此,您的函数可能会产生不同的结果。

【讨论】:

  • 非常感谢lukeA。根据您的回复,我设法通过创建具有整数值的 RST 向量来解决问题:RST &lt;-seq (45, 60, 5),并且在调用该函数时,我输入了除以 100 的 RST 值:tdm.train &lt;- lapply(Room_name, TDM_roomRST, dest.train, (RST[p]/100))。因此,我得到的结果在每种情况下都已经完全相同。说真的,我真的很感谢你在这里的帮助和暗示。我希望你是最好的。问候,谢谢。
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2019-08-28
  • 2020-02-14
  • 1970-01-01
  • 2022-08-05
  • 1970-01-01
相关资源
最近更新 更多