Introduction to NumPy
NumPy is the foundational library for numerical computing in Python, providing fast array operations and mathematical functions.
What is NumPy?
NumPy (Numerical Python) provides the ndarray, a fast, memory-efficient multi-dimensional array, along with a huge collection of mathematical functions to operate on these arrays. Install with pip install numpy.
Why NumPy Instead of Lists?
NumPy arrays are stored in contiguous memory and operations are implemented in C, making them dramatically faster than Python lists for numerical work. NumPy also supports vectorized operations — applying an operation to an entire array at once without explicit loops.
Creating Arrays
Arrays can be created from lists, or using helper functions like np.zeros(), np.ones(), np.arange(), and np.linspace().
Array Attributes
Every array has a shape (dimensions), dtype (data type), and ndim (number of dimensions).
Indexing and Slicing
NumPy arrays support slicing similar to lists, plus powerful boolean and fancy indexing for filtering data.
Vectorized Operations
Arithmetic operators (+, -, *, /) applied to arrays operate element-wise, without writing loops.
Common Functions
np.sum(), np.mean(), np.max(), np.min(), np.std(), and np.reshape() are widely used for analysis.