【问题标题】:Fast column slices in PyTablesPyTables 中的快速列切片
【发布时间】:2016-10-04 16:23:03
【问题描述】:

我在 PyTables 中有一个大型整数 CArray(100 万行,50,000 列):

In [22]: fmat
Out[22]: 
/fmat (CArray(1025461, 54123), shuffle, blosc(5)) ''
  atom := Int32Atom(shape=(), dflt=0)
  maindim := 0
  flavor := 'numpy'
  byteorder := 'little'
  chunkshape := (9, 54123)

选择行很好:

In [24]: %timeit fmat[0]
10000 loops, best of 3: 46.5 µs per loop

但是选择列需要很长时间:

In [25]: %timeit fmat[:,0]
1 loop, best of 3: 25 s per loop

是否有一种有效的方法来索引列或转置数组以允许快速切片?

【问题讨论】:

    标签: python arrays slice transpose pytables


    【解决方案1】:

    答案在创建数组时的chunkshape参数中。

    如果只需要列切片,只需将列设置为块形。例如,对于 NxP 矩阵(N 行和 P 列),选择:

    fmat = f.create_carray(f.root, 'fmat', tb.Int32Atom(), shape=(N, P), filters=filters, chunkshape=[N,1])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多