In this article, we will learn how to change the size of a numpy array in Python.
First, let’s understand what a numpy array is.
A NumPy array is a part of the NumPy library which is an array processing package.
import numpy as np eg_arr = np.array[[[1,2],[3,4]]] print[eg_arr]
Run this code
online
Using np.array, we store an array of shape [2,2] and size 4 in the variable eg_arr.
Now, let’s see how can we change the size of the array.
Changing size of numpy Array in Python
Size of a numpy array can be changed by using resize[] function of the NumPy library.
numpy.ndarray.resize[] takes these parameters-
- New size of the array
- refcheck- It is a boolean that checks the reference count. It checks if the array buffer is referenced to any other object. By default, it is set to True. You can also set it to False if you haven’t referenced the array to any other object.
During resizing, if the size of the new array is greater than the given size, then the array is filled with 0’s. Also, it resizes the array in-place.
Now let’s understand it with some examples.
Changing size of array with numpy.resize[]
Example 1 –
import numpy as np cd = np.array[[2,4,6,8]] cd.resize[[3,4],refcheck=False] print[cd]
Run this code online
The resize
function changes the shape of the array from [4,] to [3,4]. Since the size of the new array is greater, the array is filled with 0’s.
So this gives us the following output-
Example 2 –
import numpy as np cd2 = np.array[[[1,2],[3,4]]] cd2.resize[[5,6],refcheck=False] print[cd2]
Run this code online
The resize function changes the array from [2,2] to [5,6] and fills the remaining portion of the array with 0’s.
Here’s the output-
import numpy as np cd3=np.array[[[1,2],[3,4]]] cd3.resize[[2,1],refcheck=False] print[cd3]
Run this program online
Here, the size of the new array is smaller, so this gives the following output-
I hope you all liked the article!
Also read-
Understanding NumPy array dimensions in Python
In Python, if the input is a numpy array, you can use np.lib.pad
to pad zeros around it -
import numpy as np
A = np.array[[[1, 2 ],[2, 3]]] # Input
A_new = np.lib.pad[A, [[0,1],[0,2]], 'constant', constant_values=[0]] # Output
Sample run -
In [7]: A # Input: A numpy array
Out[7]:
array[[[1, 2],
[2, 3]]]
In [8]: np.lib.pad[A, [[0,1],[0,2]], 'constant', constant_values=[0]]
Out[8]:
array[[[1, 2, 0, 0],
[2, 3, 0, 0],
[0, 0, 0, 0]]] # Zero padded numpy array
If you don't want to do the math of how many zeros to pad, you can let the code do it for you given the output array size -
In [29]: A
Out[29]:
array[[[1, 2],
[2, 3]]]
In [30]: new_shape = [3,4]
In [31]: shape_diff = np.array[new_shape] - np.array[A.shape]
In [32]: np.lib.pad[A, [[0,shape_diff[0]],[0,shape_diff[1]]],
'constant', constant_values=[0]]
Out[32]:
array[[[1, 2, 0, 0],
[2, 3, 0, 0],
[0, 0, 0, 0]]]
Or, you can start off with a zero initialized output array and then put back those input elements from
A
-
In [38]: A
Out[38]:
array[[[1, 2],
[2, 3]]]
In [39]: A_new = np.zeros[new_shape,dtype = A.dtype]
In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A
In [41]: A_new
Out[41]:
array[[[1, 2, 0, 0],
[2, 3, 0, 0],
[0, 0, 0, 0]]]
In MATLAB, you can use padarray
-
A_new = padarray[A,[1 2],'post']
Sample run -
>> A
A =
1 2
2 3
>> A_new = padarray[A,[1 2],'post']
A_new =
1 2 0 0
2 3 0 0
0 0 0 0
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
With the help of Numpy numpy.resize[], we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e [2, 2], [2, 3] and many more. During resizing numpy append zeros if values at a particular place is missing.
Parameters:
new_shape : [tuple of ints, or n ints] Shape of resized array
refcheck : [bool, optional] This parameter is used to check the reference counter. By Default it is True.Returns: None
Most of you are now thinking that what is the difference between reshape and resize. When we talk about reshape then an array changes it’s shape as temporary but when we talk about resize then the changes made permanently.
Example #1:
In this example we can see that with the help of .resize[]
method, we have changed the shape of an array from 1×6 to 2×3.
import
numpy as np
gfg
=
np.array[[
1
,
2
,
3
,
4
,
5
,
6
]]
gfg.resize[
2
,
3
]
print
[gfg]
Output:
[[1 2 3] [4 5 6]]
Example #2:
In this example we can see that, we are trying to resize the array of that shape which is type of out of bound values. But numpy handles this situation to append the zeros when values are not existed in the array.
import
numpy as np
gfg
=
np.array[[
1
,
2
,
3
,
4
,
5
,
6
]]
gfg.resize[
3
,
4
]
print
[gfg]
Output:
[[1 2 3 4] [5 6 0 0] [0 0 0 0]]