Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/numpy/ma/__init__.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

10 statements  

1""" 

2============= 

3Masked Arrays 

4============= 

5 

6Arrays sometimes contain invalid or missing data. When doing operations 

7on such arrays, we wish to suppress invalid values, which is the purpose masked 

8arrays fulfill (an example of typical use is given below). 

9 

10For example, examine the following array: 

11 

12>>> x = np.array([2, 1, 3, np.nan, 5, 2, 3, np.nan]) 

13 

14When we try to calculate the mean of the data, the result is undetermined: 

15 

16>>> np.mean(x) 

17nan 

18 

19The mean is calculated using roughly ``np.sum(x)/len(x)``, but since 

20any number added to ``NaN`` [1]_ produces ``NaN``, this doesn't work. Enter 

21masked arrays: 

22 

23>>> m = np.ma.masked_array(x, np.isnan(x)) 

24>>> m 

25masked_array(data = [2.0 1.0 3.0 -- 5.0 2.0 3.0 --], 

26 mask = [False False False True False False False True], 

27 fill_value=1e+20) 

28 

29Here, we construct a masked array that suppress all ``NaN`` values. We 

30may now proceed to calculate the mean of the other values: 

31 

32>>> np.mean(m) 

332.6666666666666665 

34 

35.. [1] Not-a-Number, a floating point value that is the result of an 

36 invalid operation. 

37 

38.. moduleauthor:: Pierre Gerard-Marchant 

39.. moduleauthor:: Jarrod Millman 

40 

41""" 

42from . import core 

43from .core import * 

44 

45from . import extras 

46from .extras import * 

47 

48__all__ = ['core', 'extras'] 

49__all__ += core.__all__ 

50__all__ += extras.__all__ 

51 

52from numpy._pytesttester import PytestTester 

53test = PytestTester(__name__) 

54del PytestTester