Quick way to upsample numpy array by nearest neighbor tiling

Here's a potentially fast way using stride tricks and reshaping:

from numpy.lib.stride_tricks import as_strided

def tile_array(a, b0, b1):
    r, c = a.shape                                    # number of rows/columns
    rs, cs = a.strides                                # row/column strides 
    x = as_strided(a, (r, b0, c, b1), (rs, 0, cs, 0)) # view a as larger 4D array
    return x.reshape(r*b0, c*b1)                      # create new 2D array

The underlying data in a is copied when reshape is called, so this function does not return a view. However, compared to using repeat along multiple axes, fewer copying operations are required.

The function can be then used as follows:

>>> a = np.arange(9).reshape(3, 3)
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

>>> tile_array(a, 2, 2)
array([[0, 0, 1, 1, 2, 2],
       [0, 0, 1, 1, 2, 2],
       [3, 3, 4, 4, 5, 5],
       [3, 3, 4, 4, 5, 5],
       [6, 6, 7, 7, 8, 8],
       [6, 6, 7, 7, 8, 8]])

>>> tile_array(a, 3, 4)
array([[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
       [3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5],
       [3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5],
       [3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5],
       [6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
       [6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
       [6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8]])

Now, for small blocks, this method is a little slower than using repeat but faster than kron.

For slightly larger blocks, however, it becomes quicker than other alternatives. For instance, using a block shape of (20, 20):

>>> %timeit tile_array(a, 20, 20)
100000 loops, best of 3: 18.7 µs per loop

>>> %timeit a.repeat(20, axis=0).repeat(20, axis=1)
10000 loops, best of 3: 26 µs per loop

>>> %timeit np.kron(a, np.ones((20,20), a.dtype))
10000 loops, best of 3: 106 µs per loop

The gap between the methods increases as the block size increases.

Also if a is a large array, it may be quicker than alternatives:

>>> a2 = np.arange(1000000).reshape(1000, 1000)
>>> %timeit tile_array(a2, 2, 2)
100 loops, best of 3: 11.4 ms per loop

>>> %timeit a2.repeat(2, axis=0).repeat(2, axis=1)
1 loops, best of 3: 30.9 ms per loop

One option is

>>> a.repeat(2, axis=0).repeat(2, axis=1)
array([[0, 0, 1, 1, 2, 2],
       [0, 0, 1, 1, 2, 2],
       [3, 3, 4, 4, 5, 5],
       [3, 3, 4, 4, 5, 5],
       [6, 6, 7, 7, 8, 8],
       [6, 6, 7, 7, 8, 8]])

This is slightly wasteful due to the intermediate array but it's concise at least.


Probably not the fastest, but..

np.kron(a, np.ones((B,B), a.dtype))

It does the Kronecker product, so it involves a multiplication for each element in the output.