python - Quick way to upsample numpy array by nearest neighbor tiling -
this question has answer here:
i have 2d array of integers mxn, , expand array (bm)x(bn) b length of square tile side each element of input array repeated bxb block in final array. below example nested loop. there quicker/builtin way?
import numpy np = np.arange(9).reshape([3,3]) # input array - 3x3 b=2. # block size - 2 = np.zeros([a.shape[0]*b,a.shape[1]*b]) # output array - 6x6 # loop, filling tiled values of @ each index i,l in enumerate(a): # lines in j,aij in enumerate(l): # a[i,j] a[b*i:b*(i+1),b*j:b*(j+1)] = aij
result ...
a= [[0 1 2] [3 4 5] [6 7 8]] = [[ 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.]]
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 wasteful due intermediate array it's concise @ least.
Comments
Post a Comment