← Back to NumPy Course | Chapter 4: Indexing & Slicing | Lesson 6 of 7

Boolean Indexing कैसे करें

सिर्फ वे values रखने के लिए जो आप चाहते हैं एक array को एक true/false condition से filter करें।

In this page:

  1. Boolean Indexing
Syntax
python
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

python
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. #}
आम गलतियां
  1. & / | की बजाय and / or इस्तेमाल करना
  2. हर condition के चारों ओर parentheses भूल जाना
  3. एक 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.