Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning NumPy Array

You're reading from  Learning NumPy Array

Product type Book
Published in Jun 2014
Publisher
ISBN-13 9781783983902
Pages 164 pages
Edition 1st Edition
Languages
Author (1):
Ivan Idris Ivan Idris
Profile icon Ivan Idris

Table of Contents (14) Chapters

Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with NumPy NumPy Basics Basic Data Analysis with NumPy Simple Predictive Analytics with NumPy Signal Processing Techniques Profiling, Debugging, and Testing The Scientific Python Ecosystem Index

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. As arguments, we give one-dimensional sequences, and the function returns a tuple of NumPy arrays. For example, check the following code snippet:

In : ix_([0,1], [2,3])
Out:
(array([[0], [1]]), array([[2, 3]]))

To index the array with a list of locations, perform the following steps:

  1. Shuffle the array indices. Create a random indices array with the shuffle() function of the numpy.random module, as shown in the following lines of code. The function changes the array inplace by the way.

    def shuffle_indices(size):
       arr = np.arange(size)
       np.random.shuffle(arr)
    
       return arr
  2. Now plot the shuffled indices as follows:

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

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

The following code for this section is without comments. The complete code for this can be found in the ix.py file in the...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}