[Numpy-discussion] "upsample" or scale an array

Olivier Delalleau shish at keba.be
Sat Dec 3 11:05:47 EST 2011


You can also use numpy.tile

-=- Olivier

2011/12/3 Robin Kraft <rkraft4 at gmail.com>

> Thanks Warren, this is great, and even handles giant arrays just fine if
> you've got enough RAM.
>
> I also just found this StackOverflow post with another solution.
>
> a.repeat(2, axis=0).repeat(2, axis=1).
> http://stackoverflow.com/questions/7525214/how-to-scale-a-numpy-array
>
> np.kron lets you do more, but for my simple use case the repeat() method
> is faster and more ram efficient with large arrays.
>
> In [3]: a = np.random.randint(0, 255, (2400, 2400)).astype('uint8')
>
> In [4]: timeit a.repeat(2, axis=0).repeat(2, axis=1)
> 10 loops, best of 3: 182 ms per loop
>
> In [5]: timeit np.kron(a, np.ones((2,2), dtype='uint8'))
> 1 loops, best of 3: 513 ms per loop
>
>
> Or for a 43200x4800 array:
>
> In [6]: a = np.random.randint(0, 255, (2400*18, 2400*2)).astype('uint8')
>
> In [7]: timeit a.repeat(2, axis=0).repeat(2, axis=1)
> 1 loops, best of 3: 6.92 s per loop
>
> In [8]: timeit np.kron(a, np.ones((2, 2), dtype='uint8'))
> 1 loops, best of 3: 27.8 s per loop
>
> In this case repeat() peaked at about 1gb of ram usage while np.kron hit
> about 1.7gb.
>
> Thanks again Warren. I'd tried way too many variations on reshape and
> rollaxis, and should have come to the Numpy list a lot sooner!
>
> -Robin
>
>
> On Dec 3, 2011, at 12:51 AM, Warren Weckesser wrote:
>
> On Sat, Dec 3, 2011 at 12:35 AM, Robin Kraft wrote:
>
> >* I need to take an array - derived from raster GIS data - and upsample or*>* scale it. That is, I need to repeat each value in each dimension so that,*>* for example, a 2x2 array becomes a 4x4 array as follows:*>**>* [[1, 2],*>*  [3, 4]]*>**>* becomes*>**>* [[1,1,2,2],*>*  [1,1,2,2],*>*  [3,3,4,4]*>*  [3,3,4,4]]*>**>* It seems like some combination of np.resize or np.repeat and reshape +*>* rollaxis would do the trick, but I'm at a loss.*>**>* Many thanks!*>**>* -Robin*>**
>
> Just a day or so ago, Josef Perktold showed one way of accomplishing this
> using numpy.kron:
>
> In [14]: a = arange(12).reshape(3,4)
>
> In [15]: a
> Out[15]:
> array([[ 0,  1,  2,  3],
>        [ 4,  5,  6,  7],
>        [ 8,  9, 10, 11]])
>
> In [16]: kron(a, ones((2,2)))
> Out[16]:
> array([[  0.,   0.,   1.,   1.,   2.,   2.,   3.,   3.],
>        [  0.,   0.,   1.,   1.,   2.,   2.,   3.,   3.],
>        [  4.,   4.,   5.,   5.,   6.,   6.,   7.,   7.],
>        [  4.,   4.,   5.,   5.,   6.,   6.,   7.,   7.],
>        [  8.,   8.,   9.,   9.,  10.,  10.,  11.,  11.],
>        [  8.,   8.,   9.,   9.,  10.,  10.,  11.,  11.]])
>
>
> Warren
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111203/9fc1df38/attachment.html>


More information about the NumPy-Discussion mailing list