1. Introduction to NumPy & ndarray Architecture
NumPy (Numerical Python) is the core scientific computing package in Python. Standard Python lists store pointers to scattered objects across memory, requiring type-checking at every access. In contrast, NumPy introduces the `ndarray` (N-dimensional array), which stores homogeneous elements in contiguous blocks of computer memory. This architectural difference allows vectorized calculations — performing arithmetic operations simultaneously across millions of array elements compiled directly in C, eliminating slow Python `for` loops.
Computing raw material requirements for an industrial batch of multi-ingredient Paracetamol tablets in a single vectorized statement.
import numpy as np
# Ingredients per single tablet in milligrams: [API, Binder (PVP K30), Disintegrant (SSG), Lubricant (Mg Stearate)]
single_tablet_mg = np.array([500.0, 25.0, 30.0, 5.0])
batch_size_tablets = 500000
# Vectorized operation: Multiply entire array and convert to kilograms (1 kg = 1,000,000 mg)
total_raw_material_kg = (single_tablet_mg * batch_size_tablets) / 1e6
ingredients = ["Paracetamol API", "PVP K30 Binder", "Sodium Starch Glycolate", "Magnesium Stearate"]
for ing, qty in zip(ingredients, total_raw_material_kg):
print(f"{ing}: {qty:.2f} kg required for batch")- Homogeneous Data: All elements in an `ndarray` must share the same data type (e.g. `np.float64`, `np.int32`).
- Contiguous Memory: Direct buffer storage enables extremely high-performance mathematical throughput.
- Broadcasting: NumPy automatically expands arrays with compatible shapes to perform arithmetic operations without copying data.
- Array Creation: `np.array()`, `np.zeros()`, `np.ones()`, `np.arange()`, `np.linspace()`.
NumPy डेटा को मेमोरी में लगातार ब्लॉक्स में रखता है। इससे लाखों टैबलेट्स के फॉर्मूलेशन की गणना बिना लूप लगाए एक ही लाइन के कोड में बहुत तेज़ी से हो जाती है।