- Python Data Analysis(Second Edition)
- Armando Fandango
- 241字
- 2021-07-09 19:04:05
Fancy indexing
Fancy indexing is indexing that does not involve integers or slices, which is conventional indexing. In this tutorial, we will practice fancy indexing to set the diagonal values of the Lena photo to 0
. This will draw black lines along the diagonals, crossing through them.
The following is the code for this example:
import scipy.misc import matplotlib.pyplot as plt face = scipy.misc.face() xmax = face.shape[0] ymax = face.shape[1] face=face[:min(xmax,ymax),:min(xmax,ymax)] xmax = face.shape[0] ymax = face.shape[1] face[range(xmax), range(ymax)] = 0 face[range(xmax-1,-1,-1), range(ymax)] = 0 plt.imshow(face) plt.show()
The following is a brief explanation of the preceding code:
- 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 same:
face[range(xmax-1,-1,-1), range(ymax)] = 0
At the final stage, we produce the following picture with the diagonals crossed out:
We specified different ranges for the x values and y values. These ranges were used to index the Lena array. Fancy indexing is done based on an internal NumPy iterator object. The following three steps are performed:
- The iterator object is created.
- The iterator object gets bound to the array.
- Array elements are accessed via the iterator.
- Ceph Cookbook
- Mastering Concurrency in Go
- 深入理解Elasticsearch(原書第3版)
- 0 bug:C/C++商用工程之道
- Django 3.0入門與實踐
- Kubernetes進階實戰
- C++ Fundamentals
- 并行編程方法與優化實踐
- Mudbox 2013 Cookbook
- Tkinter GUI Programming by Example
- Learning Zimbra Server Essentials
- Implementing NetScaler VPX?(Second Edition)
- 自學Python:編程基礎、科學計算及數據分析
- Learning VMware vCloud Air
- 零基礎學西門子PLC編程:入門、提高、應用、實例