Data Handling using NumPy Arrays Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- NumPy Library: Core library for scientific computing in Python, providing high-performance multidimensional array object (`ndarray`) and vectorized mathematical routines.
- NumPy Array vs Python List: NumPy arrays store homogeneous elements in contiguous memory buffers; support vectorized element-wise arithmetic without explicit loops; lower memory overhead and faster execution.
- Array Creation: `np.array()`, `np.zeros()`, `np.ones()`, `np.empty()`, `np.arange()`, `np.linspace()`, `np.eye()`.
- Array Attributes: `ndim` (number of dimensions), `shape` (tuple of array dimensions), `size` (total elements), `dtype` (data type of elements), `itemsize` (bytes per element).
- Indexing and Slicing: 1D array indexing; 2D array indexing `arr[row, col]`; slicing `arr[row_start:row_stop, col_start:col_stop]`.
- Vectorized Operations & Statistical Functions: Element-wise arithmetic (`+`, `-`, `*`, `/`), universal functions (`np.sqrt()`, `np.exp()`), statistical methods (`np.sum()`, `np.mean()`, `np.median()`, `np.std()`, `np.min()`, `np.max()`).
Chapter Formulas & Equations (12 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | NumPy Array Size-Shape Product Identity |
|
Total count of scalar elements stored inside the ndarray. |
| 2 | Total Memory Consumed by ndarray |
|
Calculates total physical RAM buffer occupied by the homogeneous array. |
| 3 | Linspace Step Size Formula |
|
Generates n linearly spaced samples over the closed interval [start, stop]. |
| 4 | Vectorized Element-wise Arithmetic |
|
Operations execute compiled C-speed vector instructions in underlying CPU SIMD registers. |
| 5 | Broadcasting Compatibility Rule |
|
Allows NumPy to perform operations between arrays of different shapes without copying data. |
| 6 | 2D Slicing Coordinate Rule |
|
Comma separates the row slice from the column slice. |
| 7 | NumPy Statistical Mean and Standard Deviation |
|
By default, np.std() uses denominator N (population std), unlike pandas which uses N-1. |
| 8 | Array Reshaping Condition |
|
Changes array view dimensions without duplicating or altering underlying buffer data. |
| 9 | Boolean Mask Filtering |
|
Constructs a boolean array mask to filter elements in a single vectorized pass. |
| 10 | Matrix Transpose Attribute |
|
Flips rows into columns; creates a transposed view without copying data. |
| 11 | Array Concatenation Along Axis |
|
Requires all dimensions other than the concatenation axis to match exactly. |
| 12 | Arange vs Linspace Specification |
|
Use linspace for precise decimal steps to avoid floating point rounding errors. |
Key Scientific Diagrams & Concept Schematics: Data Handling using NumPy Arrays
Visual Reference: numpy-arrays
Official NCERT board curriculum concept schematic and mathematical/scientific reference diagram.
CBSE Board Exam Tips & Frequent Pitfalls
- Remember that Python lists are HETEROGENEOUS (can hold mixed types), but NumPy ndarrays are strictly HOMOGENEOUS (all elements must have the same dtype).
- In 2D slicing arr[1:3, 0:2], the first part (1:3) refers to ROWS and the second part (0:2) refers to COLUMNS.
- np.std() by default uses N in the denominator (degrees of freedom ddof = 0); pandas .std() uses N - 1 (ddof = 1).
Step-by-Step Worked Board Exam Examples
Example 1: Given a 2D NumPy array a = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]), what is the output of: (a) a.shape, (b) a.size, (c) a[1:, 1:]?
Step 1: The array has 3 rows and 3 columns.
Step 2: a.shape returns the dimensions tuple: (3, 3).
Step 3: a.size returns the total elements: 3 * 3 = 9.
Step 4: a[1:, 1:] slices from row index 1 to end (rows 1 and 2) and column index 1 to end (columns 1 and 2):
- Row 1: [50, 60]
- Row 2: [80, 90]
Step 5: Resulting sub-array: [[50, 60], [80, 90]].
Example 2: Write NumPy code to create an array of 5 equally spaced values between 0 and 1 (inclusive). What will be the generated values?
Step 1: To generate a specific count of equally spaced numbers inclusive of endpoints, use np.linspace().
Step 2: Function call: np.linspace(0, 1, 5).
Step 3: Step size = (stop - start) / (num - 1) = (1 - 0) / (5 - 1) = 1 / 4 = 0.25.
Step 4: Values generated: [0.0, 0.25, 0.5, 0.75, 1.0].
Common Mistakes to Avoid in Exam
- Confusing np.arange() with np.linspace() (arange takes step size; linspace takes number of samples).
- Writing a[1:3][0:2] instead of a[1:3, 0:2] for 2D slicing; using two separate brackets slices the outer list rather than the 2D matrix.
- Trying to store mixed strings and integers in a NumPy array without expecting type coercion (NumPy coerces all items to string dtype).
Target Practice Questions
Q1. What will be the output of: import numpy as np; arr = np.zeros((2, 3)); print(arr.dtype)?
Correct Answer: float64 (np.zeros defaults to float dtype).
Q2. Can an array of shape (2, 6) be reshaped into shape (3, 4)?
Correct Answer: Yes, because total size is preserved (2 * 6 = 12 = 3 * 4).
Q3. Write the NumPy function used to find the arithmetic mean along columns of a 2D array.
Correct Answer: np.mean(arr, axis=0).
Frequently Asked Questions
Q. Why are NumPy arrays faster and more memory-efficient than Python lists?
Python lists store arrays of pointers to individual boxed objects scattered throughout memory, requiring type inspection on every operation. NumPy arrays store raw, contiguous, homogeneous binary data in a single memory block, enabling fast C-level loops and modern CPU SIMD vectorized instructions.
Q. What is the difference between `ndim`, `shape`, and `size` in NumPy?
`ndim` is the number of axes/dimensions (e.g. 1 for vector, 2 for 2D matrix). `shape` is a tuple indicating the length of each axis (e.g. (3, 4) for 3 rows and 4 columns). `size` is the total count of scalar elements in the entire array (equal to 3 * 4 = 12).
Q. What is Broadcasting in NumPy?
Broadcasting is the rule-based mechanism that allows NumPy to perform arithmetic operations between arrays of different shapes (such as adding a scalar or a 1D vector to a 2D matrix) without making costly duplicate memory copies.
Q. How does boolean masking work on an ndarray?
A boolean expression like `arr > 50` creates an array of True/False flags of identical shape. Passing this mask as an index, `arr[arr > 50]`, extracts and returns only those elements where the condition evaluated to True as a 1D array.