Indexing with a list of locations

We are going to use again the Lena photo and apply the ix_() function to shuffle it.

import scipy.misc
import matplotlib.pyplot as plt
import numpy as np

face = scipy.misc.face() 
xmax = face.shape[0]
ymax = face.shape[1]

def shuffle_indices(size):
    arr = np.arange(size)
    np.random.shuffle(arr)

    return arr

xindices = shuffle_indices(xmax)
np.testing.assert_equal(len(xindices), xmax)
yindices = shuffle_indices(ymax)
np.testing.assert_equal(len(yindices), ymax)
plt.imshow(face[np.ix_(xindices, yindices)])
plt.show()

#out
Indexing with a list of locations

First we declare a function that creates an array. We use the shuffle() function of the numpy.random subpackage to shuffle the array indices.

def shuffle_indices(size):
    arr = np.arange(size)
    np.random.shuffle(arr)

    return arr

We then plot the shuffled indices:

plt.imshow(face[np.ix_(xindices, yindices)])

Leave a Reply