Skip to content

Latest commit

 

History

History
228 lines (173 loc) Β· 14.1 KB

NumPyTutorial.md

File metadata and controls

228 lines (173 loc) Β· 14.1 KB

πŸ“Š Comprehensive NumPy Array & Range Evolution Master Tutorial Guide

Welcome to the definitive master tutorial guide on NumPy Scientific Computing, Array Manipulation & Range Evolution. This guide mirrors the 3-step sequential curriculum architecture from the repository, guiding developers step-by-step through fundamental array construction (np.array, ones, empty, eye, dtypes, 3D tensors & reshaping), advanced mathematical operations (indexing, slicing, boolean masking, ufuncs, matrix transposition, dot product, @ matmul operator, broadcasting, axis reductions, np.linalg, np.random, vectorized string operations & binary .npy/text .csv I/O), memory benchmarks (range() vs np.arange()), introspection matrices (dir(range) vs dir(np.ndarray)), and CPython performance evolution matrices across Python versions 3.3 to 3.13.


πŸ“Œ Table of Contents

  1. Executive Summary: What is NumPy & When is it Needed Most?
  2. NumPy vs Other Machine Learning & Data Science Frameworks
  3. Curriculum Hierarchy & Subfolder Architecture
  4. Step 1: 01-Fundamentals (Array Creation, Dtypes & Reshaping)
  5. Step 2: 02-Advanced-Math-and-Operators (Indexing, Ufuncs, Broadcasting, Linalg & I/O)
  6. Step 3: 03-Range-Evolution-and-Performance (Memory Benchmarks & Introspection)
  7. Comprehensive NumPy ndarray Running Attributes & Methods Matrix
  8. Time & Space Complexity Benchmarking Matrix
  9. Cross-Version Architectural Evolution (Python 3.3 βž” 3.13)
  10. Comprehensive Unit Test Coverage
  11. Best Practices & Performance Notices

1. Executive Summary: What is NumPy & When is it Needed Most?

What is NumPy?

NumPy (Numerical Python) is the foundational standard library for scientific computing in Python. At its core, NumPy provides the ndarray (N-Dimensional Array), a contiguous C-level memory buffer containing elements of homogeneous type, paired with vectorized SIMD (Single Instruction, Multiple Data) compiled C routines.

When Do You Need NumPy the Most?

  1. High-Performance Multi-Dimensional Vector Math: When native Python for loops are too slow (e.g. processing millions of numbers or multi-dimensional tensors).
  2. Matrix Algebra & Linear Systems: When computing matrix products ($A \times B$), transpositions, determinants, matrix inversions ($A^{-1}$), or solving linear systems ($Ax = b$).
  3. Image & Signal Processing: Images are 3D arrays (Height $\times$ Width $\times$ Color Channels). NumPy allows instant pixel filtering, rotations, and transformations.
  4. Data Normalization & Feature Scaling: Subtracting column means or dividing by standard deviations across multi-gigabyte datasets in zero overhead time.
  5. Memory-Critical In-Memory Computation: Unlike Python lists (which store pointers to individual Python objects), NumPy array buffers store contiguous binary scalar bytes, yielding 80–90% RAM reduction.

2. NumPy vs Other Machine Learning & Data Science Frameworks

NumPy serves as the bedrock upon which the entire Python Machine Learning and Scientific ecosystem is built:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Deep Learning & ML Frameworks                      β”‚
β”‚      PyTorch | TensorFlow | Scikit-Learn | SciPy | XGBoost       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚ Interoperability Layer
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Data Manipulation (Pandas)                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚ Base Memory Layout
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      NumPy (np.ndarray)                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Detailed Framework Comparison Table

Feature / Dimension NumPy PyTorch / TensorFlow Pandas SciPy Scikit-Learn
Primary Focus Fundamental N-D Array & Vector Math Deep Neural Networks & Automatic Differentiation Tabular Data Analysis & Relational Joins Advanced Scientific Computing (Optimization, Signal) Machine Learning Algorithms (Trees, SVMs, Clustering)
Primary Data Structure np.ndarray torch.Tensor / tf.Tensor pd.DataFrame / pd.Series scipy.sparse.csr_matrix Accepts np.ndarray & pd.DataFrame
GPU / Acceleration CPU (SIMD C/BLAS/LAPACK) CPU + GPU (CUDA/ROCm) + TPU Acceleration CPU (Built on top of NumPy arrays) CPU (C/Fortran wrappers) CPU (Vectorized via NumPy under the hood)
Auto-Differentiation ❌ No Yes (autograd / GradientTape) ❌ No ❌ No ❌ No
Memory Layout Dense Contiguous C/Fortran Buffers Contiguous GPU/CPU Memory Buffers Heterogeneous Columns over NumPy Buffers Sparse & Specialized Matrices Standardized NumPy Arrays
When to Use Raw array math, custom algorithms, pre-processing Training Deep Learning models (LLMs, CNNs, Transformers) Data cleaning, CSV/SQL parsing, grouping & joins Advanced ODE solving, FFT, spatial geometry Fitting standard ML models (Linear Models, Random Forest)

3. Curriculum Hierarchy & Subfolder Architecture

numpy_tutorial/
β”œβ”€β”€ 01-Fundamentals/
β”‚   β”œβ”€β”€ array_creation_and_dtypes.py    # Basic ndarrays, ones, zeros, eye, empty
β”‚   β”œβ”€β”€ array_shapes_and_dimensions.py # 1D/2D/3D shapes, ndim, reshape
β”‚   └── test_fundamentals.py           # Unit tests for fundamentals
β”œβ”€β”€ 02-Advanced-Math-and-Operators/
β”‚   β”œβ”€β”€ indexing_slicing_and_masking.py# Sub-arrays, row/col extraction, fancy indexing
β”‚   β”œβ”€β”€ array_math_and_ufuncs.py        # Ufuncs, dot product, transposition, @ operator
β”‚   β”œβ”€β”€ broadcasting_and_axes.py       # Broadcasting rules, axis=0/1 sums, vstack/hstack
β”‚   β”œβ”€β”€ linalg_and_random.py           # np.linalg (solve Ax=b, det, inv) & np.random
β”‚   β”œβ”€β”€ string_arrays_and_io.py        # Vectorized strings, binary/text I/O (.npy, .csv)
β”‚   └── test_advanced_operations.py    # Unit tests for math & matrix ops
β”œβ”€β”€ 03-Range-Evolution-and-Performance/
β”‚   β”œβ”€β”€ arange_vs_range.py             # range() vs np.arange() step comparisons
β”‚   β”œβ”€β”€ range_performance_and_evolution.py # Memory footprint & speed benchmarks
β”‚   β”œβ”€β”€ reflection_and_introspection.py  # dir(range) & dir(np.ndarray) attribute matrices
β”‚   └── test_range_performance.py     # Unit tests for performance & reflection
β”œβ”€β”€ numpy_basics.py                    # Executable master curriculum runner
β”œβ”€β”€ test_numpy_tutorial.py             # Master test suite (11 unit tests)
└── NumPyTutorial.md                   # Master documentation & reference guide

4. Step 1: 01-Fundamentals (Array Creation, Dtypes & Reshaping)

N-Dimensional Array Initializers & Data Types (dtype)

# [Subfolder Title: 01-Fundamentals -> array_creation_and_dtypes.py]
import numpy as np

# 1. 1D Array from list
first_array = np.array([22, 17, 9, 18, 33, 81, 50], dtype=np.int64)

# 2. 2D Matrix from nested list
two_dim_array = np.array([[22, 17, 9], [21, 19, 4]], dtype=np.int64)

# 3. Structural matrices
ones_matrix = np.ones((10, 10), dtype=np.float64)
empty_matrix = np.empty((8, 8), dtype=np.float64)
identity_matrix = np.eye(5, dtype=np.float64)

5. Step 2: 02-Advanced-Math-and-Operators (Indexing, Ufuncs, Broadcasting, Linalg & I/O)

Indexing, Slicing & Boolean Masking

matrix = np.array([
    ["a", "b", "c", "d", "e"],
    ["ab", "cd", "ef", "gh", "ij"],
    ["kl", "mn", "op", "qr", "st"]
])

third_column = matrix[:, 2]      # ['c', 'ef', 'op']
first_row = matrix[0, :]          # ['a', 'b', 'c', 'd', 'e']

# Boolean Condition Masking
arr = np.array([10, 20, 30, 40, 50])
masked = arr[arr > np.mean(arr)]  # [40, 50]

Broadcasting Rules & Axis Reductions (axis=0, axis=1)

# Broadcasting 1D vector across 2D matrix rows
matrix_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])  # (3, 3)
row_vector = np.array([10, 20, 30])                        # (3,)
broadcast_sum = matrix_2d + row_vector                     # Adds [10, 20, 30] to each row

# Axis-specific reductions
col_sums = matrix_2d.sum(axis=0)                           # [12, 15, 18] (Column sum)
row_means = matrix_2d.mean(axis=1)                         # [2.0, 5.0, 8.0] (Row mean)

Linear Algebra (np.linalg) & Random Number Generators (np.random)

# Solving Linear Matrix System: Ax = b
A = np.array([[2.0, 1.0], [1.0, 3.0]])
b = np.array([8.0, 13.0])

x_solution = np.linalg.solve(A, b)                         # [2.2, 3.6]
determinant = np.linalg.det(A)                             # 5.0
inv_A = np.linalg.inv(A)                                   # Inverse matrix

# Reproducible Random Sampling
rng = np.random.default_rng(seed=42)
uniform_samples = rng.uniform(0.0, 1.0, size=5)

6. Step 3: 03-Range-Evolution-and-Performance (Memory Benchmarks & Introspection)

Container Memory Model RAM Footprint ($N = 100,000$) Iteration / Sum Performance
range(100_000) $O(1)$ Lazy Object 48 Bytes Python Bytecode Loop (~4.8 ms)
np.arange(100_000) $O(N)$ Contiguous C Buffer 800,112 Bytes C SIMD Vectorization (~0.25 ms - 18x–50x Faster)

7. Comprehensive NumPy ndarray Running Attributes & Methods Matrix

Attribute / Method Category Description range np.ndarray Code Example
.ndim Structural Number of dimensions ❌ βœ… arr.ndim (e.g. 2)
.shape Structural Dimensions tuple ❌ βœ… arr.shape (e.g. (3, 4))
.size Structural Total element count ❌ βœ… arr.size (e.g. 12)
.dtype Memory Element data type ❌ βœ… arr.dtype (e.g. int64)
.T Linear Alg Transposed view ❌ βœ… arr.T
.sum(axis) / .mean(axis) Statistics Reduction along specified axis ❌ βœ… arr.sum(axis=0)
np.linalg.solve(A, b) Linear Alg Solves exact linear system $Ax=b$ ❌ βœ… np.linalg.solve(A, b)
.reshape(shape) Transform Returns array view with new shape ❌ βœ… arr.reshape((4, 6))

8. Time & Space Complexity Benchmarking Matrix

Operation Input Container Time Complexity (Init) Time Complexity (Op) Space Complexity Mutates Source?
np.array(list) Python List $O(N)$ $O(N)$ $O(N)$ C buffer ❌ No
mat_a @ mat_b Matrix $M \times N \times P$ $O(MNP)$ $O(MNP)$ $O(MP)$ product ❌ No
arr.sum(axis=0) np.ndarray $O(1)$ $O(N)$ SIMD $O(\text{cols})$ vector ❌ No
np.linalg.solve(A, b) $N \times N$ Matrix $O(1)$ $O(N^3)$ LU Decomposition $O(N)$ solution ❌ No

9. Cross-Version Architectural Evolution (Python 3.3 βž” 3.13)

Python Version Core Feature Updates & Behavioral Evolution NumPy Integration & Code Demonstration
Python 3.3 range sequence slicing ($O(1)$ lazy ranges); yield from generator delegation syntax. r = range(100)[::2]; arr = np.fromiter(r, dtype=int)
Python 3.5 PEP 465: Dedicated matrix multiplication operator (@) for ndarray.__matmul__. A = np.array([[1, 2], [3, 4]]); C = A.T @ A
Python 3.8 PEP 570: Positional-only parameters (/); PEP 572: Walrus operator (:=). arr = np.array([10, 20, 30]); if (m := arr.mean()) > 15: filtered = arr[arr > m]
Python 3.11 PEP 659: Specializing Adaptive Interpreter accelerates CPython loop dispatching by 10–25%. Optimized CPython loop dispatching over NumPy arrays.
Python 3.13 PEP 703: Free-threaded CPython (GIL removal) and Tier 2 JIT compilation engine. Parallel execution of NumPy vectorized routines without GIL bottlenecks.

10. Comprehensive Unit Test Coverage

To run the full unit test suite executing all 11 curriculum tests:

.venv/bin/pytest tests/test_numpy_tutorial.py -v

11. Best Practices & Performance Notices

  1. Prefer Vectorized Ufuncs Over Python Loops: Avoid for loops when performing arithmetic on arrays. Vectorized ufuncs run compiled C SIMD instructions up to 100x faster.
  2. Understand Broadcasting Before Iterating: Leverage NumPy broadcasting rules to perform automatic dimension expansion rather than duplicating matrix rows manually.
  3. Use np.linalg.solve Over Explicit Inversion: Prefer np.linalg.solve(A, b) over np.linalg.inv(A) @ b when solving linear systems for far greater numerical stability and performance.