How to resize numpy array in python using resize() Function ?

Spread the love

In this article, you will learn how to resize numpy array in python using resize() Function ?

Before going to use resize() function, we need to understand the syntax and parameters.

Syntax: numpy.resize(a, new_shape)
Return 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. Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of a.

Parameters:
a : array_like
Array to be resized.

new_shape : int or tuple of int
Shape of resized array.

Returns:
reshaped_array : ndarray
The new array 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.

#program to resize numpy array

import numpy as np a = np.arange(10) print("Original array : \n", a) b = np.resize(a,(2,5)) print("printing 2 * 5 dimentional array after resize",b)

Output:

Original array :
[0 1 2 3 4 5 6 7 8 9]
printing 2 * 5 dimentional array after resize [[0 1 2 3 4]
[5 6 7 8 9]]

Leave a Reply

Your email address will not be published. Required fields are marked *