Skip to content

Instantly share code, notes, and snippets.

@tansey
Created March 25, 2014 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tansey/9753126 to your computer and use it in GitHub Desktop.
Save tansey/9753126 to your computer and use it in GitHub Desktop.
Numpy Array vs. Numpy Masked Array -- Madness
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