Broadcasting in NumPy
Broadcasting is NumPy's rule system for combining arrays with different but compatible shapes. It expands dimensions of size one conceptually, without copying the values into a larger array. The NumPy broadcasting guide covers additional examples.
Vectorization lets one expression operate over an entire array. NumPy executes the underlying loop in optimized compiled code, which is often faster than an explicit Python loop.
In the example above, NumPy adds elements at corresponding positions. We describe the operation once instead of indexing every element in a Python loop.
Broadcasting Rules
To decide whether two shapes are compatible, NumPy compares their dimensions from right to left.
There are three main rules in broadcasting:
- Rule : If the ranks differ, treat the lower-rank shape as if dimensions of size were added on the left
- Rule : Two aligned dimensions are compatible when they are equal or one of them has size ; a size-one dimension is virtually expanded to the other size
- Rule : If any aligned pair is incompatible, NumPy raises a
ValueError
Array with Scalar
Two-Dimensional Array with One-Dimensional Array
When you work with arrays that have different dimensions, NumPy will try to automatically adjust their shapes. This process is very useful when you want to apply the same operation to each row or column of a matrix.
Failed Broadcasting Case
Array Arithmetic Operations
NumPy arithmetic operators work element by element unless an operation explicitly has different semantics, such as matrix multiplication.
When an array is combined with a scalar, broadcasting applies that scalar to every element. No manual indexing loop is required.
Operations Between Arrays
It's important to understand the difference between element-wise multiplication (*) and matrix multiplication (@ or np.dot()). Element-wise multiplication multiplies elements at the same position, while matrix multiplication follows linear algebra rules.
Comparison and Logic
NumPy also supports comparison operations that produce boolean arrays. These operations are very useful for data filtering or creating complex conditions.
For element-wise boolean logic, use ~ for NOT, & for AND, and | for OR, or use the corresponding np.logical_* functions. Put each comparison in parentheses because the operators have different precedence from comparisons. Python's scalar operators not, and, and or do not perform element-wise array logic.
Statistical Functions and Reductions
Reduction functions combine many elements into fewer values. Without an axis they can reduce the whole array to one value; with an axis they reduce only that dimension. For a table of exam scores, this can produce an average for each subject or each student.
NumPy provides various statistical functions that are very useful for data analysis. These functions can be applied to the entire array or only to specific axes.
Operations with Axes
For a two-dimensional array, axis=0 collapses the row dimension and produces one value per column. axis=1 collapses the column dimension and produces one value per row.
Understanding axes helps you control how statistical functions work on multidimensional data. For example, if you have monthly sales data for various products, you can calculate total sales per product or per month.
Array Shape Manipulation
Shape operations reorganize how the same elements are indexed. A reshape keeps the number of elements unchanged while assigning them to different dimensions.
New NumPy arrays are commonly C-contiguous, meaning the last index changes fastest in memory. Views created by slicing or transposing can have different strides, so an array is not always stored in one simple row-major layout. The order and stride rules matter when reshaping or flattening.
Flatten and Ravel
Both flatten() and ravel() return a one-dimensional array. flatten() always creates a copy, while ravel() returns a view when possible and a copy when the memory layout requires it.
Reshape and Resize
reshape() changes the shape as long as the number of elements stays the same and returns a view when possible. The ndarray.resize() method used below changes the original array in place.
Transpose
A transpose permutes an array's axes. For a two-dimensional array it swaps rows and columns. NumPy provides the transpose() method and the shorter .T attribute.
Data Standardization with Z-Transform
Z-score standardization centers each feature at a mean of and scales it to a standard deviation of . It is defined only for features with nonzero standard deviation and puts features on comparable numerical scales.
The Z-Transform formula is , where:
- is the feature matrix of size
- is the number of observations (rows)
- is the number of features (columns)
- is the mean vector for each column
Standardization prevents units alone from creating large scale differences. It does not guarantee equal importance or influence: the model and the data still determine that. For example, height in centimeters and weight in kilograms become numerically comparable after standardization.
For more details about NumPy array operations, you can visit the official NumPy documentation, which provides guides and practical examples.