Data Science Essentials
NumPy Basics
High-performance numerical computing with multi-dimensional arrays (ndarrays).
Interview: Foundation of data science and ML. Vital for understanding memory layout (C-contiguous vs Fortran-contiguous), vectorization, and matrix manipulation.
NumPy (Numerical Python) is the foundational package for scientific computing in Python. It provides the ndarray (n-dimensional array) object, which offers fast, contiguous memory storage and vectorized operations.
Why NumPy is Fast
Standard Python lists store references to objects scattered in memory, creating overhead. NumPy arrays store elements in a contiguous block of memory with a single data type (homogeneity). This allows computations to be offloaded to optimized C code and utilizes CPU cache lines efficiently.
Vectorization and Broadcasting
- Vectorization: Performing operations on entire arrays without writing explicit loops in Python, removing Python interpreter loop overhead.
- Broadcasting: Rules that allow arithmetic operations to be performed on arrays of different shapes (e.g. adding a scalar value to a 2D matrix).
Interview Insight
Be ready to explain the difference between a copy and a view in NumPy. Indexing/Slicing an array (like b = a[1:5]) does not copy data; it creates a view sharing the same memory block. Modifying b also modifies a. To create a separate copy, call a[1:5].copy().
Use Cases
Image Processing — Treating images as 3D matrices (height, width, channels) to apply filter transformations fast.
Machine Learning — Performing high-speed matrix multiplications (dot products) for neural networks.
Mathematical Modeling — Solving linear algebra equation matrices programmatically.
Common Mistakes
Using loops instead of vectorization — Writing `for` loops to iterate over array elements, which negates all NumPy performance advantages.
Modifying views accidentally — Changing sliced data and unintentionally corrupting the original master array.
Array shape mismatches — Attempting matrix operations on dimensions that do not align or satisfy broadcasting rules.