NumPy Essentials
NumPy is the foundation of the entire Python data science ecosystem. Every pandas DataFrame, scikit-learn model, and PyTorch tensor is built on top of NumPy arrays. Understanding NumPy is not optional â it is essential.
Why NumPy?
Python lists are slow for numerical work because each element is a separate object. NumPy stores data in contiguous blocks of memory and operates on entire arrays at once using optimized C code.
Creating Arrays
Array Indexing and Slicing
NumPy indexing is more powerful than Python list indexing.
Broadcasting Visual
Broadcasting
Broadcasting lets NumPy perform operations on arrays of different shapes without explicit loops.
Vectorization
Vectorization replaces loops with array operations. It is faster, cleaner, and more expressive.
Aggregation Functions
Linear Algebra
Random Sampling
Reshaping and Combining
Where and Conditional Logic
Matrix Operations Reference
Matrix Multiplication:
Element-wise Multiplication (Hadamard Product):
Dot Product:
Transpose:
Vector Norm: . L2 norm: , L1 norm: .
Key Takeaways
- NumPy arrays are 10-100x faster than Python lists for numerical operations.
- Vectorization eliminates loops â think in array operations, not element-by-element.
- Broadcasting handles different shapes automatically, saving you from manual tiling.
- Master
axis=0(column) vsaxis=1(row) for aggregations. - Use
np.where,np.select, and boolean indexing for conditional logic. - Set random seeds (
np.random.seed()) for reproducible results.