File: misc.rst

package info (click to toggle)
numpy 1%3A2.4.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 87,160 kB
  • sloc: python: 259,644; asm: 232,483; ansic: 213,962; cpp: 160,235; f90: 1,585; sh: 785; fortran: 567; makefile: 443; sed: 139; xml: 109; java: 97; perl: 82; cs: 62; javascript: 53; objc: 33; lex: 13; yacc: 9
file content (59 lines) | stat: -rw-r--r-- 1,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
:orphan:

*************
Miscellaneous
*************

IEEE 754 floating point special values
--------------------------------------

Special values defined in numpy: :data:`~numpy.nan`, :data:`~numpy.inf`

NaNs can be used as a poor-man's mask (if you don't care what the
original value was)

Note: cannot use equality to test NaNs. E.g.: ::

 >>> myarr = np.array([1., 0., np.nan, 3.])
 >>> np.nonzero(myarr == np.nan)
 (array([], dtype=int64),)

::

 >>> np.nan == np.nan  # is always False! Use special numpy functions instead.
 False

::

 >>> myarr[myarr == np.nan] = 0. # doesn't work
 >>> myarr
 array([  1.,   0.,  nan,   3.])

::

 >>> myarr[np.isnan(myarr)] = 0. # use this instead find
 >>> myarr
 array([1.,  0.,  0.,  3.])

Other related special value functions:

- :func:`~numpy.isnan` - True if value is nan
- :func:`~numpy.isinf` - True if value is inf
- :func:`~numpy.isfinite` - True if not nan or inf
- :func:`~numpy.nan_to_num` - Map nan to 0, inf to max float, -inf to min float

The following corresponds to the usual functions except that nans are excluded
from the results:

- :func:`~numpy.nansum`
- :func:`~numpy.nanmax`
- :func:`~numpy.nanmin`
- :func:`~numpy.nanargmax`
- :func:`~numpy.nanargmin`

 >>> x = np.arange(10.)
 >>> x[3] = np.nan
 >>> x.sum()
 nan
 >>> np.nansum(x)
 42.0