\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)
Methods vs functions in NumPyΒΆ
Many things are implemented in NumPy as both functions and methods. For
example, there is a np.sum
function, that adds up all the elements:
>>> import numpy as np
>>> arr = np.array([1, 2, 0, 1])
>>> np.sum(arr)
4
There is also a sum
method of the numpy array
object:
>>> type(arr)
<class 'numpy.ndarray'>
>>> arr.sum()
4
Nearly all the method versions do the same thing as the function versions.
Examples are mean
, min
, max
, sum
, reshape
. Choosing the
method or the function will usually depend on which one is easier to read.