Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numpy Upsamle np.array /increase size of np.array by adding mean value of cosecuitive elements

I have an np.array which I need to make its size double/triple/quadruple. I want to do it by adding 1/2/3 elements between every 2 consecutive elements.
for example:

np.array([1,2,3,4,5])

to be

np.array([1,1.5,2,2.5,3,3.5,4,4.5,5,5.5])

There is no problem doing that using python. but I need the fastest possible way, preferably using Numpy/Scipy.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

np.interp:

a = np.array([1,2,3,4,5])
ur = 2  # upsample rate
np.interp(np.arange((len(a)-1)*ur+1)/ur, xp=np.arange(len(a)), fp=a)
# output: array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5.])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading