- Python Data Analysis(Second Edition)
- Armando Fandango
- 129字
- 2021-07-09 19:04:06
Indexing NumPy arrays with Booleans
Boolean indexing is indexing based on a Boolean array and falls in the family of fancy indexing. Since Boolean indexing is a kind of fancy indexing, the way it works is essentially the same.
The following is the code for this segment (refer to boolean_indexing.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] face=face[:min(xmax,ymax),:min(xmax,ymax)] def get_indices(size): arr = np.arange(size) return arr % 4 == 0 face1 = face.copy() xindices = get_indices(face.shape[0]) yindices = get_indices(face.shape[1]) face1[xindices, yindices] = 0 plt.subplot(211) plt.imshow(face1) face2 = face.copy() face2[(face > face.max()/4) & (face < 3 * face.max()/4)] = 0 plt.subplot(212) plt.imshow(face2) plt.show()
The preceding code implies that indexing occurs with the aid of a special iterator object.
The following steps will give you a brief explanation of the preceding code:
- Image with dots on the diagonal.
This is in some manner similar to the Fancy indexing section. This time we choose modulo 4 points on the diagonal of the picture:
def get_indices(size): arr = np.arange(size) return arr % 4 == 0
Then, we just use this selection and plot the points:
face1 = face.copy() xindices = get_indices(face.shape[0]) yindices = get_indices(face.shape[1]) face1[xindices, yindices] = 0 plt.subplot(211) plt.imshow(face1)
- Set to
0
based on value.Select array values between one quarter and three quarters of the maximum value and set them to
0
:face2[(face > face.max()/4) & (face < 3 * face.max()/4)] = 0
The diagram with the two new pictures is presented as follows:
- Getting Started with React
- 簡單高效LATEX
- 實(shí)戰(zhàn)Java程序設(shè)計(jì)
- Python進(jìn)階編程:編寫更高效、優(yōu)雅的Python代碼
- 小程序,巧運(yùn)營:微信小程序運(yùn)營招式大全
- Java項(xiàng)目實(shí)戰(zhàn)精編
- The HTML and CSS Workshop
- 單片機(jī)應(yīng)用與調(diào)試項(xiàng)目教程(C語言版)
- 智能手機(jī)APP UI設(shè)計(jì)與應(yīng)用任務(wù)教程
- Troubleshooting Citrix XenApp?
- 平面設(shè)計(jì)經(jīng)典案例教程:CorelDRAW X6
- Python期貨量化交易實(shí)戰(zhàn)
- 會(huì)當(dāng)凌絕頂:Java開發(fā)修行實(shí)錄
- Learning Cocos2d-JS Game Development
- 自己動(dòng)手構(gòu)建編程語言:如何設(shè)計(jì)編譯器、解釋器和DSL