Chris@87
|
1 """
|
Chris@87
|
2 =============
|
Chris@87
|
3 Masked Arrays
|
Chris@87
|
4 =============
|
Chris@87
|
5
|
Chris@87
|
6 Arrays sometimes contain invalid or missing data. When doing operations
|
Chris@87
|
7 on such arrays, we wish to suppress invalid values, which is the purpose masked
|
Chris@87
|
8 arrays fulfill (an example of typical use is given below).
|
Chris@87
|
9
|
Chris@87
|
10 For example, examine the following array:
|
Chris@87
|
11
|
Chris@87
|
12 >>> x = np.array([2, 1, 3, np.nan, 5, 2, 3, np.nan])
|
Chris@87
|
13
|
Chris@87
|
14 When we try to calculate the mean of the data, the result is undetermined:
|
Chris@87
|
15
|
Chris@87
|
16 >>> np.mean(x)
|
Chris@87
|
17 nan
|
Chris@87
|
18
|
Chris@87
|
19 The mean is calculated using roughly ``np.sum(x)/len(x)``, but since
|
Chris@87
|
20 any number added to ``NaN`` [1]_ produces ``NaN``, this doesn't work. Enter
|
Chris@87
|
21 masked arrays:
|
Chris@87
|
22
|
Chris@87
|
23 >>> m = np.ma.masked_array(x, np.isnan(x))
|
Chris@87
|
24 >>> m
|
Chris@87
|
25 masked_array(data = [2.0 1.0 3.0 -- 5.0 2.0 3.0 --],
|
Chris@87
|
26 mask = [False False False True False False False True],
|
Chris@87
|
27 fill_value=1e+20)
|
Chris@87
|
28
|
Chris@87
|
29 Here, we construct a masked array that suppress all ``NaN`` values. We
|
Chris@87
|
30 may now proceed to calculate the mean of the other values:
|
Chris@87
|
31
|
Chris@87
|
32 >>> np.mean(m)
|
Chris@87
|
33 2.6666666666666665
|
Chris@87
|
34
|
Chris@87
|
35 .. [1] Not-a-Number, a floating point value that is the result of an
|
Chris@87
|
36 invalid operation.
|
Chris@87
|
37
|
Chris@87
|
38 """
|
Chris@87
|
39 from __future__ import division, absolute_import, print_function
|
Chris@87
|
40
|
Chris@87
|
41 __author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
|
Chris@87
|
42 __version__ = '1.0'
|
Chris@87
|
43 __revision__ = "$Revision: 3473 $"
|
Chris@87
|
44 __date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
|
Chris@87
|
45
|
Chris@87
|
46 from . import core
|
Chris@87
|
47 from .core import *
|
Chris@87
|
48
|
Chris@87
|
49 from . import extras
|
Chris@87
|
50 from .extras import *
|
Chris@87
|
51
|
Chris@87
|
52 __all__ = ['core', 'extras']
|
Chris@87
|
53 __all__ += core.__all__
|
Chris@87
|
54 __all__ += extras.__all__
|
Chris@87
|
55
|
Chris@87
|
56 from numpy.testing import Tester
|
Chris@87
|
57 test = Tester().test
|
Chris@87
|
58 bench = Tester().bench
|