Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती · Python basics

P12.5 NumPy Boolean Indexing: Conditions ने Arrays Filter करा

By Ravindra Bagale

हे का शिकायचं?

उशिरा झालेल्या deliveries किंवा मोठ्या orders च हव्या असतात.

हे काय आहे?

arr > 30 ने True/False mask मिळतो. arr[mask] फक्त True वाल्या values देतो. दोन conditions साठी & आणि | कंसासह वापरा. np.where(condition, a, b) values बदलतो.

कसं करायचं?

Step 1. Condition ने mask बनवा.

Step 2. arr[mask] ने filter करा.

Step 3. गरज असेल तर np.where.

1. amounts = [450, 1200, 300, 2500]2. amounts > 1000 → [F, T, F, T]3. Keep only T positions4. Result: [1200, 2500]
वरून खाली वाचा: code पासून result पर्यंतचा प्रवास

उदाहरणं: copy करा आणि चालवा

उदाहरण A: Mask बनवा

हा code चालवा:

import numpy as np
amounts = np.array([450, 1200, 300, 2500, 800])
mask = amounts > 1000
print(mask)

तुम्हाला दिसणारा output:

[False  True False  True False]

याचा अर्थ: प्रत्येक value साठी True/False मिळालं.

उदाहरण B: Filter करा आणि मोजा

हा code चालवा:

import numpy as np
amounts = np.array([450, 1200, 300, 2500, 800])
big = amounts[amounts > 1000]
print(big)
print((amounts > 1000).sum())

तुम्हाला दिसणारा output:

[1200 2500]
2

याचा अर्थ: arr[mask] ने values मिळाल्या आणि mask.sum() ने count.

उदाहरण C: दोन conditions

हा code चालवा:

import numpy as np
amounts = np.array([450, 1200, 300, 2500, 800])
mid = amounts[(amounts >= 400) & (amounts <= 1000)]
print(mid)

तुम्हाला दिसणारा output:

[450 800]

याचा अर्थ: & वापरताना प्रत्येक condition कंसात हवी.

उदाहरण D: np.where ने values बदला

हा code चालवा:

import numpy as np
amounts = np.array([450, 1200, 300, 2500, 800])
labels = np.where(amounts > 1000, "High", "Normal")
print(labels)

तुम्हाला दिसणारा output:

['Normal' 'High' 'Normal' 'High' 'Normal']

याचा अर्थ: Condition नुसार प्रत्येक value ला label मिळालं.