Last active
October 13, 2015 18:21
-
-
Save Jwely/8b6677cacb1170e3e945 to your computer and use it in GitHub Desktop.
syntax for numpy masked arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Jwely' | |
import numpy | |
# in order to handle Nodata values, we must use floats, not integer datatypes. | |
array = numpy.array([0, 5, 6, 3, 0, 11, 5, 2, 3, 6, 4, 1, 2, 5, 12], "float32") | |
# mask this array, where all "0" values are masked | |
# see also: https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html | |
masked_array = numpy.ma.masked_equal(array, 0) | |
# now lets also mask out the really high values because these are "bad" data | |
masked_array = numpy.ma.masked_greater(masked_array, 10) | |
# visual representation of masked array | |
print("masked array") | |
print masked_array | |
# exposes the true nature of the array, values where the mask layer is True are masked. | |
print("separate data and mask arrays") | |
print masked_array.data | |
print masked_array.mask | |
# we can see that zero values are not included when calculating the minimum | |
print("minimum value") | |
print masked_array.min() | |
# we can see that values above 10 are not included when calculating the maximum | |
print("maximum value") | |
print masked_array.max() | |
# lets find the 75th percentile value | |
print("75th percentile value") | |
print numpy.nanpercentile(masked_array.filled(numpy.nan), 75) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment