子集化data.table的速度取决于奇怪的特定键值?

时间:2015-06-06 05:14:19

标签: r data.table

有人可以解释以下输出吗?除非我遗漏了某些东西(我可能是这样),否则似乎对data.table进行子集化的速度取决于存储在其中一列中的特定值,即使它们属于同一类并且除了它们之外没有明显的差异值。

这怎么可能?

> dim(otherTest)
[1] 3572069       2
> dim(test)
[1] 3572069       2
> length(unique(test$keys))
[1] 28741
> length(unique(otherTest$keys))
[1] 28742
> sapply(test,class)
 thingy        keys 
"character" "character" 
> sapply(otherTest,class)
 thingy        keys 
"character" "character" 
> class(test)
[1] "data.table" "data.frame"
> class(otherTest)
[1] "data.table" "data.frame"
> start = Sys.time()
>   newTest = otherTest[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.5438871 secs
> start = Sys.time()
>   newTest = test[keys%in%partition]
>   end  = Sys.time() 
>   print(end - start)
Time difference of 42.78009 secs

摘要编辑:因此,速度的差异与不同大小的data.tables无关,也不与不同数量的唯一值有关。正如您在上面的修改示例中所看到的,即使在生成密钥以使它们具有相同数量的唯一值(并且在相同的一般范围内并且共享至少1个值,但通常是不同的)之后,我得到了相同的表现差异。

关于共享数据,遗憾的是我无法共享测试表,但我可以分享其他测试。整个想法是我试图尽可能地复制测试表(相同的大小,相同的类/类型,相同的键,NA值的数量等),以便我可以发布到SO - 但奇怪的是我的制作up data.table表现得非常不同,我无法弄清楚原因!

另外,我要补充一点,我怀疑这个问题的唯一原因来自data.table是几周前我遇到了一个类似的问题,对数据进行了子集化。结果证明这是一个真正的bug新的data.table版本(我最后删除了这个问题因为它是重复的)。该错误还涉及使用%in%函数来对data.table进行子集化 - 如果在%in%的右边参数中有重复,则返回重复的输出。因此,如果x = c(1,2,3)和y = c(1,1,2,2),%y中的x%将返回长度为8的向量。我有树脂封装data.table包,所以我不要认为它可能是同一个错误 - 但也许相关?

编辑(re Dean MacGregor')

> sapply(test,class)
 thingy        keys 
"character" "character" 
> sapply(otherTest,class)
 thingy        keys 
"character" "character" 


# benchmarking the original test table
>   test2 =data.table(sapply(test ,as.numeric))
>   otherTest2 =data.table(sapply(otherTest ,as.numeric))
>   start = Sys.time()
>   newTest = test[keys%in%partition])
>   end  = Sys.time()
>   print(end - start)
Time difference of 52.68567 secs
> start = Sys.time()
>   newTest = otherTest[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.3503151 secs

#benchmarking after converting to numeric
> partition = as.numeric(partition)
> start = Sys.time()
>   newTest = otherTest2[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.7240109 secs
> start = Sys.time()
>    newTest = test2[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 42.18522 secs

#benchmarking again after converting back to character
> partition = as.character(partition)
> otherTest2 =data.table(sapply(otherTest2 ,as.character))
> test2 =data.table(sapply(test2 ,as.character))
> start = Sys.time()
>   newTest =test2[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 48.39109 secs
> start = Sys.time()
>   newTest = data.table(otherTest2[keys%in%partition])
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.1846113 secs

所以减速并不依赖于阶级。

编辑:问题显然来自data.table,因为我可以转换为矩阵并且问题消失,然后转换回data.table并且问题又回来了。

编辑:我注意到问题与data.table函数如何处理重复有关,这听起来是正确的,因为它类似于我上周在上面描述的数据表1.9.4中发现的错误。

>   newTest =test[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 39.19983 secs
> start = Sys.time()
>   newTest =otherTest[keys%in%partition]
>   end  = Sys.time()
 >   print(end - start)
 Time difference of 0.3776946 secs
> sum(duplicated(test))/length(duplicated(test))
[1] 0.991954
> sum(duplicated(otherTest))/length(duplicated(otherTest))
[1] 0.9918879
> otherTest[duplicated(otherTest)] =NA
 > test[duplicated(test)]= NA
> start = Sys.time()
>   newTest =otherTest[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.2272599 secs
> start = Sys.time()
>   newTest =test[keys%in%partition]
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.2041721 secs

因此,即使它们具有相同数量的重复项,两个data.tables(或者更具体地说,data.table中的%in%函数)显然也不同地处理重复项。与重复有关的另一个有趣的观察是这个(注意我再次从原始表开始):

> start = Sys.time()
>   newTest =test[keys%in%unique(partition)]
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.6649222 secs
> start = Sys.time()
>   newTest =otherTest[keys%in%unique(partition)]
>   end  = Sys.time()
>   print(end - start)
Time difference of 0.205637 secs

因此,将右侧参数中的重复项删除到%in%也可以解决问题。

所以考虑到这个新信息,问题仍然存在:为什么这两个data.tables以不同方式处理重复值?

3 个答案:

答案 0 :(得分:3)

data.table match%in%match操作定义)和你的矢量大小时,你会专注于library(microbenchmark) set.seed(1492) # sprintf to keep the same type and nchar of your values keys_big <- sprintf("%014d", sample(5000, 4000000, replace=TRUE)) keys_small <- sprintf("%014d", sample(5000, 30000, replace=TRUE)) partition <- sample(keys_big, 250) microbenchmark( "big"=keys_big %in% partition, "small"=keys_small %in% partition ) ## Unit: milliseconds ## expr min lq mean median uq max neval cld ## big 167.544213 184.222290 205.588121 195.137671 205.043641 376.422571 100 b ## small 1.129849 1.269537 1.450186 1.360829 1.506126 2.848666 100 a 应该关注。一个可重复的例子:

match

来自文档:

  

%chin%返回其第二个参数(第一个)匹配位置的向量。

这固有地意味着它将依赖于向量的大小以及如何接近顶部&#34;找到(或找不到)匹配。

然而,您可以使用data.table中的library(data.table) microbenchmark( "big"=keys_big %chin% partition, "small"=keys_small %chin% partition ) ## Unit: microseconds ## expr min lq mean median uq max neval cld ## big 36312.570 40744.2355 47884.3085 44814.3610 48790.988 119651.803 100 b ## small 241.045 264.8095 336.1641 283.9305 324.031 1207.864 100 a 加速整个事情,因为您使用了字符向量:

fastmatch

你也可以使用data.table包(但你已经加载了library(fastmatch) # gives us similar syntax & functionality as %in% and %chin% "%fmin%" <- function(x, table) fmatch(x, table, nomatch = 0) > 0 microbenchmark( "big"=keys_big %fmin% partition, "small"=keys_small %fmin% partition ) ## Unit: microseconds ## expr min lq mean median uq max neval cld ## big 75189.818 79447.5130 82508.8968 81460.6745 84012.374 124988.567 100 b ## small 443.014 471.7925 525.2719 498.0755 559.947 850.353 100 a 并且正在处理字符向量,所以6/1 | 0.5 * 12):

library(ggplot2)
library(gridExtra)

microbenchmark(
  "small_in"=keys_small %in% partition,
  "small_ch"=keys_small %chin% partition,
  "small_fm"=keys_small %fmin% partition,
  unit="us"
) -> small

microbenchmark(
  "big_in"=keys_big %in% partition,
  "big_ch"=keys_big %chin% partition,
  "big_fm"=keys_big %fmin% partition,
  unit="us"
) -> big

grid.arrange(autoplot(small), autoplot(big))

无论如何,任一矢量的大小最终将决定操作的速度/速度。但后两种选择至少可以让你获得更快的结果。这里是小型和大型载体的所有三者之间的比较:

data.table

enter image description here

更新

基于OP评论,这是使用和不使用dat_big <- data.table(keys=keys_big) microbenchmark( "dt" = dat_big[keys %in% partition], "not_dt" = dat_big$keys %in% partition, "dt_ch" = dat_big[keys %chin% partition], "not_dt_ch" = dat_big$keys %chin% partition, "dt_fm" = dat_big[keys %fmin% partition], "not_dt_fm" = dat_big$keys %fmin% partition ) ## Unit: milliseconds ## expr min lq mean median uq max neval cld ## dt 11.74225 13.79678 15.90132 14.60797 15.66586 129.2547 100 a ## not_dt 160.61295 174.55960 197.98885 184.51628 194.66653 305.9615 100 f ## dt_ch 46.98662 53.96668 66.40719 58.13418 63.28052 201.3181 100 c ## not_dt_ch 37.83380 42.22255 50.53423 45.42392 49.01761 147.5198 100 b ## dt_fm 78.63839 92.55691 127.33819 102.07481 174.38285 374.0968 100 e ## not_dt_fm 67.96827 77.14590 99.94541 88.75399 95.47591 205.1925 100 d 子集进行思考的另一个基准:

x(y?)z

答案 1 :(得分:1)

如果您的数据涉及速度较慢,那么您可以考虑在每次加载后设置数据密钥,以使用聚簇键和索引继续进行任何查询。
由于精确和现代的排序算法的实现,设置键相对便宜。

library(data.table)
library(microbenchmark)

set.seed(1492)

keys_big <- sprintf("%014d", sample(5000, 4000000, replace=TRUE))
keys_small <- sprintf("%014d", sample(5000, 30000, replace=TRUE))

partition <- sample(keys_big, 250)

dat_big <- data.table(keys=keys_big, key = "keys")

microbenchmark(

    "dt"        = dat_big[keys %in% partition],
    "not_dt"    = dat_big$keys %in% partition,

    "dt_ch"     = dat_big[keys %chin% partition],
    "not_dt_ch" = dat_big$keys %chin% partition,

    "dt_key"    = dat_big[partition]

)
# Unit: milliseconds
#       expr        min         lq       mean     median         uq       max neval
#         dt   5.810935   6.100602   6.830618   6.493006   6.825171  20.47223   100
#     not_dt 237.730092 246.318824 266.484226 257.507188 272.433109 461.17918   100
#      dt_ch  62.822514  66.169728  71.522330  69.865380  75.056333 103.45799   100
#  not_dt_ch  51.292627  52.551307  58.236860  54.920637  59.762000 215.65466   100
#     dt_key   5.941748   6.210253   7.251318   6.568678   7.004453  23.45361   100

设置密钥的时间

dat_big <- data.table(keys=keys_big)
system.time(setkey(dat_big, keys))
#    user  system elapsed 
#   0.230   0.008   0.238 

这是最近的1.9.5。

答案 2 :(得分:0)

我希望操作时间与thingotherThing的大小成正比,而我在输出中看不到它们的大小,所以很难确切知道会发生什么。

但是,otherthing$keys中的唯一值比thing$keys中的要多得多(124.28倍),所以您不希望操作需要更长的时间吗?它必须检查表中的值以查找它找到的每个唯一值(并且您似乎知道这一点,因为您打印了值)。

注意时间比例约为60.8。