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 arrayThis 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 caseLet’s make simple task and make reduction of the resolution step by step.
The image above shows how to do this resizing in principle. So, lets generate last example.
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:
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.
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.
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 arrayIn this task we will make universal function and apply it to array
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())
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.
|
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. |