Fancy indexing is not conventional indexing that involves integers and slicing. If we take the Lena photo again we could draw two black diagonal lines on the photo by setting the diagonal values to 0.
import scipy.misc import matplotlib.pyplot as plt face = scipy.misc.face() xmax = face.shape ymax = face.shape face = face[:min(xmax,ymax),:min(xmax,ymax)] xmax = face.shape ymax = face.shape face[range(xmax),range(ymax)] = 0 face[range(xmax-1,-1,-1), range(ymax)] = 0 plt.imshow(face) #out
This is what we actually did here:
- Set the values of the first diagonal to 0.
To set the diagonal values to 0, we need to specify two different ranges for the x and y values (coordinates in a Cartesian coordinate system):
face[range(xmax), range(ymax)] = 0
- Set the values of the other diagonal to 0.
To set the values of the other diagonal, we need a different set of ranges, but the rules remain the
face[range(xmax-1,-1,-1), range(ymax)] = 0
- The ranges we created are used to fancy index the Lena picture by the internal NumPy iterator object. First, the iterator object is created; second, the iterator object is bound to the array; third, array elements are accessed via the iterator.