O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Indexing with a list of locations

Let's use the ix_() function to shuffle the Lena image. This function creates a mesh from multiple sequences.

How to do it...

We will start by randomly shuffling the array indices:

  1. Create a random indices array with the shuffle() function of the numpy.random module:
    def shuffle_indices(size):
       arr = np.arange(size)
       np.random.shuffle(arr)
    
       return arr
  2. Plot the shuffled indices:
    plt.imshow(lena[np.ix_(xindices, yindices)])
    

    What we get is a completely scrambled Lena image, as shown in the following screenshot:

    How to do it...

    Here is the complete code for the recipe from the ix.py file in this book's code bundle:

    import scipy.misc import matplotlib.pyplot ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required