- 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.
- GitLab Cookbook
- 零基礎PHP學習筆記
- 簡單高效LATEX
- Interactive Data Visualization with Python
- Web交互界面設計與制作(微課版)
- Programming ArcGIS 10.1 with Python Cookbook
- Python進階編程:編寫更高效、優雅的Python代碼
- 算法精粹:經典計算機科學問題的Python實現
- 用Flutter極速構建原生應用
- Android Wear Projects
- Python圖形化編程(微課版)
- Babylon.js Essentials
- JavaScript腳本特效編程給力起飛
- Java Web從入門到精通(第2版)
- 奔跑吧 Linux內核