Boolean Indexing कैसे करें
सिर्फ वे values रखने के लिए जो आप चाहते हैं एक array को एक true/false condition से filter करें।
In this page:
Syntax
mask = arr > value
arr[mask]
arr[(arr > a) & (arr < b)]
Boolean Indexing
arr > 3 जैसा एक comparison same shape का एक boolean array देता है। उस mask को एक index की तरह इस्तेमाल करना सिर्फ True elements return करता है। Conditions को parentheses में wrap किए & और | से combine करें।
Note:
Boolean indexing हमेशा एक copy return करता है, slicing के उलट।
उदाहरण: Boolean indexing
import numpy as np
a = np.array([1, 5, 8, 3, 10, 2])
mask = a > 4
print(mask)
print(a[mask])
print(a[(a > 2) & (a < 9)])
a[a > 8] = 0
print(a)
# Output:
# [False True True False True False]
# [ 5 8 10]
# [5 8 3]
# [1 5 8 3 0 2]
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- & / | की बजाय and / or इस्तेमाल करना
- हर condition के चारों ओर parentheses भूल जाना
- एक view return होने की उम्मीद करना
चैप्टर सारांश
- Comparisons boolean masks produce करते हैं
- Masks matching elements select करते हैं
- & | ~ से combine करें
- Result एक copy है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: