![]() |
VOOZH | about |
The numpy.resize() function is used to change the size of an existing NumPy array. It modifies the array permanently and adjusts its shape to the new dimensions. If the new shape requires more elements than available, NumPy repeats the array elements. If less space is required, elements are truncated.
Example 1: This example resizes a 1D array of 6 elements into a 2×3 array. No values need repetition or truncation.
[[1 2 3] [4 5 6]]
numpy.resize(a, new_shape)
Parameters:
Example 2: This example resizes a 6-element array into a 3×4 shape (12 elements needed). NumPy repeats the array elements to fill the new size.
[[1 2 3 4] [5 6 0 0] [0 0 0 0]]
Example 3: This example resizes an array into a 2×2 shape. Since fewer elements are required, the extra values are removed.
[[10 20] [30 40]]