Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why np.resize() out_place, while ndarray.resize() in_place?

According to my understanding, with classes instance.method(parameters)=class.method(instance,parameters), so it's just a notation difference. But np.resize(ndarray) changes out_place, while ndarray.resize() changes in_place.

What am I missing?

like image 530
Chris Avatar asked Aug 23 '18 07:08

Chris


1 Answers

Yes, but numpy isn't the class, it is the module. You want numpy.ndarray as the class. Observe:

In [1]: import numpy as np

In [2]: arr = np.array([1,2,3])

In [3]: np.ndarray.resize(arr, (3,1))

In [4]: arr
Out[4]:
array([[1],
       [2],
       [3]])
In [5]: np.ndarray.resize(arr, (3,))

In [6]: arr
Out[6]: array([1, 2, 3])

So, numpy.resize is just a module-level function, that returns a new array instead of modifying the array in-place.

like image 68
juanpa.arrivillaga Avatar answered Nov 04 '22 11:11

juanpa.arrivillaga