Implementation of Numpy methods on numeric dataset from list

एक raw Python list को एक vectorized NumPy array में बदलना आपको एक भी मैनुअल for-loop लिखे बिना हज़ारों data metrics को तुरंत filter, scale, और manipulate करने देता है।

11 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Multi-Row मैनुअल Math का दर्द

आपके Semester 1 C labs में, अगर आपके पास 100 pocket-money transaction spends की एक list होती और आपको हर एक item को 5% tax से बढ़ाना या केवल 50 Rs से ज़्यादा वाले transactions filter करने होते, आपको अलग loop counters declare करने, कदम-दर-कदम iterate करने, और items को एक नए array में हाथ से append करने पड़ते। standard Python में भी, एक primitive list पर math चलाने के लिए एक स्पष्ट for loop या एक list comprehension चाहिए। NumPy इन loop lines को पूरी तरह क्यों ख़त्म कर देता है, आपको numbers की पूरी lists को mathematical syntax की एक अकेली line से manipulate करने देते हुए?

Theory

Single-Passenger Courier बनाम एकीकृत Freight Train

एक courier service की कल्पना कीजिए जिसे 10 parcels अलग घरों तक पहुँचाने हैं। एक basic Python list इस्तेमाल करना एक अकेले bike courier को भेजने जैसा है जो एक parcel लेता है, एक गंतव्य तक चलाता है, लौटता है, दूसरा parcel लेता है, और यात्रा 10 बार दोहराता है। NumPy arrays एक high-speed freight train की तरह काम करते हैं जो एक बार में सारे 10 parcels से भरी है। जब एक mathematical instruction दी जाती है, यह पूरी train को एक साथ मारती है, एक अकेले processing sweep में हर एक parcel को समानांतर में बदलते हुए।

Theory

Vectorization और Boolean Masking औपचारिक रूप से

जब आप एक standard Python list को np.array() इस्तेमाल करके एक NumPy array में बदलते हैं, collection vectorization की शक्ति पाता है। Vectorization स्पष्ट loops को high-performance, compiled C-code expressions से बदलती है। यह broadcasting सक्षम करती है, जहाँ एक operation (जैसे arr * 1.05) हर एक element पर अपने-आप लागू होता है। इसके अलावा, arr > 50 जैसी conditions जाँचना एक filtered list return नहीं करता; बजाय इसके, यह एक Boolean Mask (true/false flags का एक array) generate करता है जिसे elements को तुरंत filter करने के लिए वापस brackets में passed किया जा सकता है।

At a glance

Table 1: numeric list processing के लिए vectorized operations और boolean filtering syntax।

Array Vector Operationयह Dataset को कैसे बदलता हैपरिणामी Output Structure
spends_arr * 1.10Broadcasting: हर element को 1.10 से गुणा करता है (10% inflation जोड़ता है)।एक नया array जिसमें सारे numbers fractionally scaled up हैं।
spends_arr > 50Logical Evaluation: हर cell को scalar value 50 के ख़िलाफ़ test करता है।केवल True और False flags रखता एक boolean array।
spends_arr[spends_arr > 50]Boolean Indexing: cells filter करता और रखता है जहाँ condition True है।केवल 50 से ज़्यादा elements रखता एक छोटा array।
spends_arr.round(2)Element-wise Precision: fractional digits को दो जगहों तक round करता है।financial logging के लिए साफ़-सुथरे formatted floats का एक array।

Theory

Worked Example: Spends Dataset का Vectorized Auditing

आइए इन concepts को एक विशिष्ट pocket-money ledger पर लागू करें। हम रोज़मर्रा के spends की एक raw numerical Python list को एक NumPy array में load करेंगे, broadcasting के ज़रिए एक uniform cashback discount लागू करेंगे, और boolean arrays इस्तेमाल करके high-value transactions तुरंत अलग करेंगे।

Practical

PocketMoney Vectorized Analyzer

import numpy as np

# Step 1: Raw Python list tracking student canteen spends
raw_spends = [40, 120, 35, 75, 200, 50]

# Step 2: Convert the list into a high-speed NumPy array
spends_arr = np.array(raw_spends)

# Step 3: Vectorized Broadcasting - Apply a flat 5 Rs cashback discount
discounted_spends = spends_arr - 5

# Step 4: Boolean Masking - Identify transactions that exceed 60 Rs
high_value_mask = discounted_spends > 60
filtered_spends = discounted_spends[high_value_mask]

print("Original Spends Array:", spends_arr)
print("Discounted Spends Array:", discounted_spends)
print("Logical Boolean Mask:", high_value_mask)
print("Filtered High Spends:", filtered_spends)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Element-Wise Shifts को ट्रेस करें

tracking script के चरणों को ध्यान से देखिए। Logical Boolean Mask और Filtered High Spends arrays के लिए कौन से बिल्कुल components print होंगे?

Show the answer

script output करेगी:

Original Spends Array: [ 40 120 35 75 200 50]

Discounted Spends Array: [ 35 115 30 70 195 45]

Logical Boolean Mask: [False True False True True False]

Filtered High Spends: [115 70 195]

क्यों? 5 घटाना हर element को समान रूप से घटाता है। फिर, > 60 evaluate करना हर discounted item जाँचता है: 35 false है, 115 true है, 30 false है, 70 true है, 195 true है, और 45 false है। इस boolean mask array को वापस brackets में pass करना केवल True marked indices निकालता है, अंतिम high-value subset बनाते हुए।

Quiz

अगर आपके पास एक NumPy array `vals = np.array([10, 20, 30])` है और आप statement `res = vals + 2` execute करते हैं, `res` के अंदर क्या store होता है?

  1. `[12, 20, 30]` रखता एक array क्योंकि केवल पहला item मेल खाता है।
  2. `[10, 20, 30, 2]` रखता एक array क्योंकि यह scalar append करता है।
  3. vectorized broadcasting के कारण `[12, 22, 32]` रखता एक array।
  4. एक तुरंत TypeError क्योंकि आप एक scalar को एक array container में सीधे नहीं जोड़ सकते।
Show the answer

vectorized broadcasting के कारण `[12, 22, 32]` रखता एक array।

NumPy एक पूरे array sequence में single numbers को अपने-आप scale या adjust करने के लिए broadcasting इस्तेमाल करता है। scalar 2 अंतर्निहित रूप से broadcast होता है और 10, 20, और 30 में अलग-अलग जोड़ा जाता है, तुरंत [12, 22, 32] बनाते हुए।

Quiz

Python analytics में numeric datasets filter करते समय एक Boolean Mask की प्राथमिक भूमिका क्या है?

  1. यह terminal display output screen से संवेदनशील financial values छुपाता है।
  2. यह True/False values का एक array बनाता है जो एक index filter के रूप में लागू होने पर target elements अलग करता है।
  3. यह strings के एक array को floating-point numbers में convert होने पर मजबूर करता है।
  4. यह संरचनात्मक matrix reshapes अपने-आप सँभालता है।
Show the answer

यह True/False values का एक array बनाता है जो एक index filter के रूप में लागू होने पर target elements अलग करता है।

एक boolean mask एक सटीक criteria filter के रूप में काम करता है। जब arr[mask] जैसे index brackets के रूप में लागू होता है, NumPy चुनिंदा रूप से केवल वे indices रखता है जहाँ mask एक True flag रखता है, False values को पूरी तरह हटाते हुए।

Watch out

Classic जाल: Missing Filtering Brackets की गिरावट

university laboratory examinations में सबसे आम marks-गँवाने वाली ग़लती filtered = spends_arr > 50 लिखना और इससे असल numbers की उम्मीद करना है। ऐसा करना केवल raw [True, False, ...] flag mask save करता है! असल values retrieve करने के लिए, आपको वह mask वापस array brackets में pass करना होगा: filtered = spends_arr[spends_arr > 50]। बाहरी array wrapping मत भूलिए!

Theory

Vectorized Lists को Semester 3 से जोड़ना

Vectorized arithmetic layouts deep big-data engines पर सीधे map होते हैं। Semester 3 (BCA303/BCA304) में, जब unstructured records साफ़ करते हैं, invalid sensor anomalies हटाते हैं, या SQL databases पर column scaling करते हैं, आप clean tables को analytical reporting systems में feed करने से पहले corrupt log entries तुरंत filter करने के लिए boolean masks इस्तेमाल करेंगे।

Summary

Key takeaways

  • standard lists को NumPy arrays में बदलना high-speed vectorized operations सक्षम करता है।
  • Broadcasting मैनुअल loops के बिना एक पूरे collection पर mathematical modifications एक साथ लागू करता है।
  • Logical expressions संरचनात्मक true या false indicator flags से भरे Boolean Masks generate करते हैं।
  • Boolean Indexing इन flag arrays को subsets तुरंत filter करने के लिए वापस brackets में route करता है।
  • Memory Hook: Scalars हर cell में broadcast होते हैं, conditions true/false masks बनाती हैं, brackets data filter करते हैं!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Libraries

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Implementation of Numpy methods on numeric dataset from list · Programming Skills · Gri-Learn