NumPy

From David's Wiki
Revision as of 20:35, 5 January 2021 by David (talk | contribs) (Created page with "NumPy is a python library for working with arrays. ==Memory Usage== To optimize memory usage, try to use views instead of actual arrays where possible. Indexing into an arr...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
\( \newcommand{\P}[]{\unicode{xB6}} \newcommand{\AA}[]{\unicode{x212B}} \newcommand{\empty}[]{\emptyset} \newcommand{\O}[]{\emptyset} \newcommand{\Alpha}[]{Α} \newcommand{\Beta}[]{Β} \newcommand{\Epsilon}[]{Ε} \newcommand{\Iota}[]{Ι} \newcommand{\Kappa}[]{Κ} \newcommand{\Rho}[]{Ρ} \newcommand{\Tau}[]{Τ} \newcommand{\Zeta}[]{Ζ} \newcommand{\Mu}[]{\unicode{x039C}} \newcommand{\Chi}[]{Χ} \newcommand{\Eta}[]{\unicode{x0397}} \newcommand{\Nu}[]{\unicode{x039D}} \newcommand{\Omicron}[]{\unicode{x039F}} \DeclareMathOperator{\sgn}{sgn} \def\oiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x222F}\,}{\unicode{x222F}}{\unicode{x222F}}{\unicode{x222F}}}\,}\nolimits} \def\oiiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x2230}\,}{\unicode{x2230}}{\unicode{x2230}}{\unicode{x2230}}}\,}\nolimits} \)

NumPy is a python library for working with arrays.

Memory Usage

To optimize memory usage, try to use views instead of actual arrays where possible.
Indexing into an array will return a view. See numpy indexing for details.
Fancy indexing, using a list of indices, will not return a view.

The following functions also return views

Most function calls will return a new array. E.g. a+b will return a new array with the broadcasted size which requires memory allocation.
To output to an existing array c, you can use np.add(a, b, out=c).
If you need to perform an operation over multiple arrays, you can use np.ufunc.reduce. E.g. np.logical_and.reduce((a, b, c)).