numpy.resize() in Python
numpy.resize() in Python
The numpy.resize() returns a new array with the specified shape. If the new array is larger than the original array, then the new array is filled with repeated copies of a.
Syntax
numpy.resize(a, new_shape)
Parameter
The numpy.resize() parameter consists of two parameters, which are as follows:
a : This parameter represents the array to be resized.
new_shape: This parameter represents the shape of the resized array.
Return
This function returns the new array that is formed from the data in the old array, repeated if necessary to fill out the required number of elements. The data are repeated in the order that they are stored in memory.
Example 1
# Python program explaining # numpy.resize() function import numpy as np # passing input array inp_arr = np.array([7, 2, 3, 9, 5, 8]) print("Input array:") print(inp_arr) # Reshaping the input array permanently inp_arr.resize(2, 3) print("After reshaping the array: ") print(inp_arr)
Output
Input array: [7 2 3 9 5 8] After reshaping the array: [[7 2 3] [9 5 8]]
Example 2
# Python program explaining # numpy.resize() function import numpy as np # input array inp_arr = np.array([11, 12, 13, 14, 15, 16]) # Required values 12, existing values 6 inp_arr.resize(4, 3) print(inp_arr)
Output
[[11 12 13] [14 15 16] [ 0 0 0] [ 0 0 0]]