Created
March 25, 2014 00:54
-
-
Save tansey/9753126 to your computer and use it in GitHub Desktop.
Numpy Array vs. Numpy Masked Array -- Madness
This file contains 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
import numpy as np | |
import numpy.ma as ma | |
# Create a 3x3 array in regular numpy | |
a = np.arange(9).reshape((3,3)) | |
# Get the middle row | |
b = a[1] | |
# Change the middle value in the middle row | |
b[1] = 20 | |
# The value at a[1,1] will be 20 | |
print 'Row slice: {0}'.format(b) | |
print 'Regular array:\n{0}'.format(a) | |
# Create a 3x3 masked array in numpy.ma | |
a = ma.masked_array(np.arange(9).reshape((3,3)), mask=np.ones((3,3))) | |
# Get the middle row | |
b = a[1] | |
# Change the middle value in the middle row | |
b[1] = 20 | |
# The value at a[1,1] will still be marked as missing. | |
print 'Row slice: {0}'.format(b) | |
print 'Masked array:\n{0}'.format(a) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment