How to reduce the size of NumPy 1dim array

Arnaud
Dec 21, 2020

--

We often have to deal with very long lists of element, like audio data, and sometimes we want to reduce size to increase processing speed. There are many functions for image but not for 1dim array. I propose you my solution using only NumPy function.

Here is my solution !

def resize(inputArray, newLength):
block_size = len(inputArray)//newLength
outputArray = inputArray[0:block_size*newLength].reshape((block_size, newLength))
outputArray = np.mean(outputArray, axis=0)//np.max/np.min...
outputArray = outputArray.reshape((newLength))
return outputArray

We reshape the array into a 2dims array constituted of block we will represent by one element using Mean, Max, Min or any function you want to use to reduce size of your array.

Note: this method only works to reduce size and cannot be used to increase the size of the array.

--

--

Arnaud
Arnaud

Written by Arnaud

Working in computer science.

No responses yet