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

Olivier Delalleau shish at keba.be
Sat Dec 3 12:47:48 EST 2011


Ah sorry, I hadn't read carefully enough what you were trying to achieve. I
think the double repeat solution looks like your best option then.

-=- Olivier

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

> That does repeat the elements, but doesn't get them into the desired order.
>
> In [4]: print a
> [[1 2]
>  [3 4]]
>
> In [7]: np.tile(a, 4)
> Out[7]:
> array([[1, 2, 1, 2, 1, 2, 1, 2],
>        [3, 4, 3, 4, 3, 4, 3, 4]])
>
> In [8]: np.tile(a, 4).reshape(4,4)
> Out[8]:
> array([[1, 2, 1, 2],
>        [1, 2, 1, 2],
>        [3, 4, 3, 4],
>        [3, 4, 3, 4]])
>
> It's close, but I want to repeat the elements along the two
> axes, effectively stretching it by the lower right corner:
>
> array([[1, 1, 2, 2],
>        [1, 1, 2, 2],
>        [3, 3, 4, 4],
>        [3, 3, 4, 4]])
>
> It would take some more reshaping/axis rolling to get there, but it seems
> doable.
>
> Anyone know what combination of manipulations would work with the result
> of np.tile?
>
> -Robin
>
>
>
> On Dec 3, 2011, at 11:05 AM, Olivier Delalleau wrote:
>
> You can also use numpy.tile
>
> -=- Olivier
>
> 2011/12/3 Robin Kraft
>
> 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/38bb8e00/attachment.html>


More information about the NumPy-Discussion mailing list