0

I'm confused with how numpy methods are applied to nd-arrays. for example:

import numpy as np    
a = np.array([[1,2,2],[5,2,3]])
b = a.transpose()
a.sort()

Here the transpose() method is not changing anything to a, but is returning the transposed version of a, while the sort() method is sorting a and is returning a NoneType. Anybody an idea why this is and what is the purpose of this different functionality?

StefanVR
  • 67
  • 1
  • 8

2 Answers2

1

Because numpy authors decided that some methods will be in place and some won't. Why? I don't know if anyone but them can answer that question.

'in-place' operations have the potential to be faster, especially when dealing with large arrays, as there is no need to re-allocate and copy the entire array, see answers to this question

BTW, most if not all arr methods have a static version that returns a new array. For example, arr.sort has a static version numpy.sort(arr) which will accept an array and return a new, sorted array (much like the global sorted function and list.sort()).

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

In a Python class (OOP) methods which operate in place (modify self or its attributes) are acceptable, and if anything, more common than ones that return a new object. That's also true for built in classes like dict or list.

For example in numpy we often recommend the list append approach to building an new array:

In [296]: alist = []
In [297]: for i in range(3):
     ...:     alist.append(i)
     ...:     
In [298]: alist
Out[298]: [0, 1, 2]

This is common enough that we can readily write it as a list comprehension:

In [299]: [i for i in range(3)]
Out[299]: [0, 1, 2]

alist.sort operates in-place, sorted(alist) returns a new list.

In numpy methods that return a new array are much more common. In fact sort is about the only in-place method I can think of off hand. That and a direct modification of shape: arr.shape=(...).

A number of basic numpy operations return a view. That shares data memory with the source, but the array object wrapper is new. In fact even indexing an element returns a new object.

So while you ultimately need to check the documentation, it's usually safe to assume a numpy function or method returns a new object, as opposed to operating in-place.

More often users are confused by the numpy functions that have the same name as a method. In most of those cases the function makes sure the argument(s) is an array, and then delegates the action to its method. Also keep in mind that in Python operators are translated into method calls - + to __add__, [index] to __getitem__() etc. += is a kind of in-place operation.

hpaulj
  • 221,503
  • 14
  • 230
  • 353