Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between resize and reshape when using arrays in NumPy?

Tags:

python

numpy

I have just started using NumPy. What is the difference between resize and reshape for arrays?

like image 388
Rish_Saxena Avatar asked Jan 07 '17 05:01

Rish_Saxena


People also ask

What is the difference between shape and reshape in NumPy?

The shape tool gives a tuple of array dimensions and can be used to change the dimensions of an array. The reshape tool gives a new shape to an array without changing its data. It creates a new array and does not modify the original array itself.

What does it mean to reshape a NumPy array?

Reshaping numpy array simply means changing the shape of the given array, shape basically tells the number of elements and dimension of array, by reshaping an array we can add or remove dimensions or change number of elements in each dimension.

What is resize in NumPy?

NumPy: resize() function The resize() function is used to create 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. Array to be resized.

What does NumPy reshape function do?

Gives a new shape to an array without changing its data. Array to be reshaped.


1 Answers

Reshape doesn't change the data as mentioned here. Resize changes the data as can be seen here.

Here are some examples:

>>> numpy.random.rand(2,3) array([[ 0.6832785 ,  0.23452056,  0.25131171],        [ 0.81549186,  0.64789272,  0.48778127]]) >>> ar = numpy.random.rand(2,3) >>> ar.reshape(1,6) array([[ 0.43968751,  0.95057451,  0.54744355,  0.33887095,  0.95809916,          0.88722904]]) >>> ar array([[ 0.43968751,  0.95057451,  0.54744355],        [ 0.33887095,  0.95809916,  0.88722904]]) 

After reshape the array didn't change, but only output a temporary array reshape.

>>> ar.resize(1,6) >>> ar array([[ 0.43968751,  0.95057451,  0.54744355,  0.33887095,  0.95809916,          0.88722904]]) 

After resize the array changed its shape.

like image 127
Rahul Reddy Vemireddy Avatar answered Sep 21 '22 19:09

Rahul Reddy Vemireddy