- Python Data Analysis(Second Edition)
- Armando Fandango
- 187字
- 2021-07-09 19:04:06
Indexing with a list of locations
Let's apply the ix_()
function to shuffle the Lena photo. The following is the code for this example without comments. The finished code for the recipe can be found in ix.py
in this book's code bundle:
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()
This function produces a mesh from multiple sequences. We hand in parameters as one-dimensional sequences and the function gives back a tuple of NumPy arrays, for instance, as follows:
In : ix_([0,1], [2,3]) Out: (array([[0],[1]]), array([[2, 3]]))
To index the NumPy array with a list of locations, execute the following steps:
- Shuffle the array indices.
Make an array with random index numbers with the
shuffle()
function of thenumpy.random
subpackage. The function modifies the array in place:def shuffle_indices(size): arr = np.arange(size) np.random.shuffle(arr) return arr
- Plot the shuffled indices, as shown in the following code:
plt.imshow(face[np.ix_(xindices, yindices)])
What we obtain is a totally scrambled Lena:

推薦閱讀
- Mobile Web Performance Optimization
- LabVIEW2018中文版 虛擬儀器程序設計自學手冊
- Building a Home Security System with Raspberry Pi
- Java完全自學教程
- Data Analysis with IBM SPSS Statistics
- Java系統化項目開發教程
- HTML5權威指南
- CRYENGINE Game Development Blueprints
- Mastering Elasticsearch(Second Edition)
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- JavaWeb從入門到精通(視頻實戰版)
- Java程序設計教程
- Java EE實用教程
- Mastering JavaScript Promises
- Mastering Magento Theme Design