Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oversample Numpy Array (2D) [duplicate]

Is there a function in numpy/scipy to over-sample a 2D numpy array?

example:

>>> x = [[1,2]
     [3,4]]
>>> 
>>> y = oversample(x, (2, 3))

would returns

y = [[1,1,2,2],
     [1,1,2,2],
     [1,1,2,2],
     [3,3,4,4],
     [3,3,4,4],
     [3,3,4,4]]

At the moment I've implemented my own function:

index_x = np.arange(newdim) / olddim
index_y = np.arange(newdim) / olddim

xx, yy = np.meshgrid(index_x, index_y)
return x[yy, xx, ...]

but it doesn't look like the best way as it only works for 2D reshaping as well as being a bit slow...

Any suggestions? Thank you very much

like image 342
Lorenzo Trojan Avatar asked Feb 07 '26 03:02

Lorenzo Trojan


1 Answers

EDIT Didnt see the comment until after post, delete if needed

Original check np.repeat to repeat patterns. shown verbosely

>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> b=a.repeat(3,axis=0)
>>> b
array([[1, 2],
       [1, 2],
       [1, 2],
       [3, 4],
       [3, 4],
       [3, 4]])
>>> c = b.repeat(2,axis=1)
>>> c
array([[1, 1, 2, 2],
       [1, 1, 2, 2],
       [1, 1, 2, 2],
       [3, 3, 4, 4],
       [3, 3, 4, 4],
       [3, 3, 4, 4]])

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!