numpy.tile非整数次

时间:2014-10-15 04:45:44

标签: python arrays numpy

numpy中是否有更好的方法将数组平铺非整数次?这可以完成工作,但是很笨重,并且不容易推广到n维:

import numpy as np
arr = np.arange(6).reshape((2, 3))
desired_shape = (5, 8)
reps = tuple([x // y for x, y in zip(desired_shape, arr.shape)])
left = tuple([x % y for x, y in zip(desired_shape, arr.shape)])
tmp = np.tile(arr, reps)
tmp = np.r_[tmp, tmp[slice(left[0]), :]]
tmp = np.c_[tmp, tmp[:, slice(left[1])]]

这会产生:

array([[0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1]])

编辑:效果

对三维答案的一些测试推广到n维。这些定义放在文件newtile.py

import numpy as np

def tile_pad(a, dims):
    return np.pad(a, tuple((0, i) for i in (np.array(dims) - a.shape)),
                  mode='wrap')

def tile_meshgrid(a, dims):
    return a[np.meshgrid(*[np.arange(j) % k for j, k in zip(dims, a.shape)],
                         sparse=True, indexing='ij')]

def tile_rav_mult_idx(a, dims):
    return a.flat[np.ravel_multi_index(np.indices(dims), a.shape, mode='wrap')]

以下是bash行:

python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_pad(np.arange(30).reshape(2, 3, 5), (3, 5, 7))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_meshgrid(np.arange(30).reshape(2, 3, 5), (3, 5, 7))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_rav_mult_idx(np.arange(30).reshape(2, 3, 5), (3, 5, 7))'

python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_pad(np.arange(2310).reshape(2, 3, 5, 7, 11), (13, 17, 19, 23, 29))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_meshgrid(np.arange(2310).reshape(2, 3, 5, 7, 11), (13, 17, 19, 23, 29))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_rav_mult_idx(np.arange(2310).reshape(2, 3, 5, 7, 11), (13, 17, 19, 23, 29))'

以下是小阵列(2 x 3 x 5)的结果:

pad:               10000 loops, best of 3: 106 usec per loop
meshgrid:          10000 loops, best of 3: 56.4 usec per loop
ravel_multi_index: 10000 loops, best of 3: 50.2 usec per loop

以下是较大阵列(2 x 3 x 5 x 7 x 11)的结果:

pad:               10 loops, best of 3: 25.2 msec per loop
meshgrid:          10 loops, best of 3: 300 msec per loop
ravel_multi_index: 10 loops, best of 3: 218 msec per loop

因此使用np.pad的方法可能是性能最佳的选择。

4 个答案:

答案 0 :(得分:2)

这是一个非常简洁的方法:

In [57]: a
Out[57]: 
array([[0, 1, 2],
       [3, 4, 5]])

In [58]: old = a.shape

In [59]: new = (5, 8)

In [60]: a[(np.arange(new[0]) % old[0])[:,None], np.arange(new[1]) % old[1]]
Out[60]: 
array([[0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1]])

这是一个n维概括:

def rep_shape(a, shape):
    indices = np.meshgrid(*[np.arange(k) % j for j, k in zip(a.shape, shape)],
                          sparse=True, indexing='ij')
    return a[indices]

例如:

In [89]: a
Out[89]: 
array([[0, 1, 2],
       [3, 4, 5]])

In [90]: rep_shape(a, (5, 8))
Out[90]: 
array([[0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1]])

In [91]: rep_shape(a, (4, 2))
Out[91]: 
array([[0, 1],
       [3, 4],
       [0, 1],
       [3, 4]])

In [92]: b = np.arange(24).reshape(2,3,4)

In [93]: b
Out[93]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

In [94]: rep_shape(b, (3,4,5))
Out[94]: 
array([[[ 0,  1,  2,  3,  0],
        [ 4,  5,  6,  7,  4],
        [ 8,  9, 10, 11,  8],
        [ 0,  1,  2,  3,  0]],

       [[12, 13, 14, 15, 12],
        [16, 17, 18, 19, 16],
        [20, 21, 22, 23, 20],
        [12, 13, 14, 15, 12]],

       [[ 0,  1,  2,  3,  0],
        [ 4,  5,  6,  7,  4],
        [ 8,  9, 10, 11,  8],
        [ 0,  1,  2,  3,  0]]])

以下是第一个示例的工作原理......

我们的想法是使用数组来索引a。看看np.arange(new[0] % old[0])

In [61]: np.arange(new[0]) % old[0]
Out[61]: array([0, 1, 0, 1, 0])

该数组中的每个值都会在结果中使用a行。与之相似,

In [62]: np.arange(new[1]) % old[1]
Out[62]: array([0, 1, 2, 0, 1, 2, 0, 1])

在结果中使用a列。要使这些索引数组创建二维结果,我们必须将第一个重新整形为一列:

In [63]: (np.arange(new[0]) % old[0])[:,None]
Out[63]: 
array([[0],
       [1],
       [0],
       [1],
       [0]])

当数组用作索引时,它们广播。这是广播索引的样子:

n [65]: i, j = np.broadcast_arrays((np.arange(new[0]) % old[0])[:,None], np.arange(new[1]) % old[1])

In [66]: i
Out[66]: 
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0]])

In [67]: j
Out[67]: 
array([[0, 1, 2, 0, 1, 2, 0, 1],
       [0, 1, 2, 0, 1, 2, 0, 1],
       [0, 1, 2, 0, 1, 2, 0, 1],
       [0, 1, 2, 0, 1, 2, 0, 1],
       [0, 1, 2, 0, 1, 2, 0, 1]])

这些是我们生成具有形状(5,8)的数组所需的索引数组:

In [68]: a[i,j]
Out[68]: 
array([[0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1],
       [3, 4, 5, 3, 4, 5, 3, 4],
       [0, 1, 2, 0, 1, 2, 0, 1]])

当索引数组在开头的示例中给出时(即在第一个索引槽中使用(np.arange(new[0]) % old[0])[:,None]),numpy实际上并没有像我{{1}那样在内存中生成这些索引数组}和i。广播发生时,ji会显示有效内容。

函数j执行相同的操作,使用rep_shape为每个" slot"生成索引数组。具有正确的广播形状。

答案 1 :(得分:2)

可能效率不高但非常简洁:

arr = np.arange(6).reshape((2, 3))
desired_shape = (5, 8)

arr.flat[np.ravel_multi_index(np.indices(desired_shape), arr.shape, mode='wrap')]

答案 2 :(得分:2)

另一个更简洁的解决方案:

arr = np.arange(6).reshape((2, 3))
desired_shape = np.array((5, 8))

pads = tuple((0, i) for i in (desired_shape-arr.shape))
# pads = ((0, add_rows), (0, add_columns), ...)
np.pad(arr, pads, mode="wrap")

但对于小型阵列来说速度较慢(尽管大型阵列要快得多)。奇怪的是,np.pad不接受np.array的垫片。

答案 3 :(得分:0)

不确定 n 维度,但您可以考虑使用hstackvstack

arr = np.arange(6).reshape((2, 3))

nx, ny = shape(arr)
Nx, Ny = 5, 8 # These are the new shapes
iX, iY = Nx//nx+1, Ny//ny+1

result = vstack(tuple([ hstack(tuple([arr]*iX))[:, :Nx] ]*iY))[:Ny, :  ]

有一个dstack,但我怀疑这是否会有所帮助。不完全确定3和更高的尺寸。