My Coding > Programming language > Python > Python libraries and packages > Python NumPy > How to reduce size of the numpy array

How to reduce size of the numpy array

This is not very correct question. It is possible to ask how to reduce resolution of the numpy array or how to downsize numpy array and all these questions will nor really reflect the problem.

Physically, this is very easy to explain, We have some numerical simulations in multidimensional orthogonal box and we would like to reduce the resolution of this box. In two dimensional case this is more know as reducing image resolution.

For this task I will reduce resolution only into integer times.

Simple size reduction in 2D case

Let’s make simple task and make reduction of the resolution step by step.

Task for Numpy Resizing.
Task for Numpy Resizing.
This is a few examples of different NumPy resizing for 1D and 2D cases.
Original image: 664 x 488

The image above shows how to do this resizing in principle. So, lets generate last example.


import numpy as np
in3 = np.arange(0, 24).reshape(6, 4)
print(in3)
#[[ 0  1  2  3]
# [ 4  5  6  7]
# [ 8  9 10 11]
# [12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]

And we need to reduce this array into two times by axis 0 and 2 times by axis 1.

To do this need to split it into array with cells, which we can join for reduction. We need to do something like this one:


#[[[[ 0  1] [ 2  3]]
#  [[ 4  5] [ 6  7]]]

# [[[ 8  9] [10 11]]
#  [[12 13] [14 15]]]

# [[[16 17] [18 19]]
#  [[20 21] [22 23]]]]

In this array it is really easy to see, that we need to join (calculate mean value) for cells (0, 1, 4, 5), for cells (2, 3, 6, 7), (8, 9, 12, 13) and so on.

How to make this array? It is easy to do with reshaping. We need to make axis 0 as previous axis 0 devided by resizing factor, then, next shape should be resizing faster itself, then next axis divided by resizing factor, then resizing factor.


res = in3.reshape(3,2,2,2)
print(res)
#[[[[ 0  1]
#   [ 2  3]]

#  [[ 4  5]
#   [ 6  7]]]

# [[[ 8  9]
#   [10 11]]

#  [[12 13]
#   [14 15]]]

# [[[16 17]
#   [18 19]]

#  [[20 21]
#   [22 23]]]]

You can check that this is the same array as I’ve show before. And now we need to find mean value towards axis 1 and 3.


print(res.mean(axis=(1,3))
#[[ 2.5  4.5]
# [10.5 12.5]
# [18.5 20.5]]

On the output after reshaping, you can try to find where the axis 1 and 3 respectively. It is better to do independently, to understand where the axis on the multidimensional numpy array.

Now we need to make this code into a general function, which can work with any dimensional arrays.

Reducing NumPy size for multidimensional array

In this task we will make universal function and apply it to array


in3 = np.arange(0, 24).reshape(6, 4)
#[[ 0  1  2  3]
# [ 4  5  6  7]
# [ 8  9 10 11]
# [12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]

#[[ 2.  3.  4.  5.]
# [10. 11. 12. 13.]
# [18. 19. 20. 21.]]

Hint – for simple mixing two numpy array, it is better to stack them as a columns ( np.column_stack()) and then flatten by rows (ravel())


# Array for reduccing size
in3 = np.arange(0, 24).reshape(6, 4)
# Divider as a tuple
div = (2,1)
# Conver tuple to numpy array
DIV = np.asanyarray(div)
# Calculating new shape
ns = in3.shape//DIV
# array([3, 4])
#Stack these to arrays as a columns
cs = np.column_stack([ns, DIV])
#array([[3, 2],
#       [4, 1]])
# Flattent this array by rows
rs = cs.ravel()
# array([3, 2, 4, 1])
# generate tuple of new axis, we need to make list of 1, 3, 5, 7, etc
ax = tuple(range(1,DIV.size*2,2))
# (1, 3)
# join all these data together
new = in3.reshape(rs).mean(axis=ax)
#[[ 2.  3.  4.  5.]
# [10. 11. 12. 13.]
# [18. 19. 20. 21.]]

If it is necessary, it is easy to make this steps as a function. I hope it was not very difficult to understand, but if something unclear, watch video again and ask some questions in the comments.


Published: 2022-10-02 19:56:36

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.