π NumPy
Descriptionβ
< What is it? >β
NumPy is Python's core library for numerical arrays and vectorized computation. Its ndarray type and linear-algebra
syntax are widely used in machine learning; PyTorch tensors deliberately feel similar.
Key pointsβ
< What is np.asarray()? >β
np.asarray(a, dtype=None) converts array-like inputβsuch as a Python list, tuple, or existing arrayβinto a NumPy
ndarray. It is useful at a function boundary when the caller may provide either a list or an array.
Unlike np.array(), np.asarray() avoids a copy when the input is already an ndarray with a compatible dtype and
memory order. It may still copy when conversion is required, such as when requesting a different dtype.
values = [1, 2, 3]
x = np.asarray(values, dtype=np.float32)
# array([1., 2., 3.], dtype=float32)
existing = np.array([1, 2, 3])
np.asarray(existing) is existing # True: no copy is needed
< What is X.shape[1]? >β
X.shape is a tuple containing an array's dimensions. Python uses zero-based indexing, so X.shape[1] is the size of
the second axis.
X = np.array([[1, 2, 3],
[4, 5, 6]])
X.shape # (2, 3)
X.shape[0] # 2: first axis, usually rows
X.shape[1] # 3: second axis, usually columns
With the common tabular ML convention X.shape == (n_samples, n_features), X.shape[1] is the number of features.
In column-vector mathematical notation, data may instead be arranged as (n_features, n_examples), where
X.shape[1] is the number of examples. Always check the full shape rather than assume its meaning. A one-dimensional
array has shape (n,), so X.shape[1] raises an IndexError.
< What is np.unique()? >β
np.unique(x) returns the distinct values in an array, sorted by default. With no axis argument, it flattens a
multi-dimensional array first.
labels = np.array([2, 1, 2, 3, 1, 2])
np.unique(labels)
# array([1, 2, 3])
classes, counts = np.unique(labels, return_counts=True)
# classes -> array([1, 2, 3])
# counts -> array([2, 3, 1])
Use return_counts=True to count each value, return_index=True to obtain each value's first position, and
return_inverse=True to reconstruct the original array from the unique values. For a 2-D array,
np.unique(X, axis=0) finds unique rows.
< What is np.dot()? >β
np.dot(a, b) combines two arrays by contracting particular axes. Its meaning depends on their dimensions:
| Inputs | Result |
|---|---|
| Two 1-D vectors | Inner product: sum of element-wise products |
| Two 2-D arrays | Matrix multiplication |
| A scalar and an array | Scalar multiplication; use * instead for clarity |
| Higher-dimensional arrays | Contracts the last axis of a with the second-to-last axis of b |
import numpy as np
x = np.array([1, 2])
y = np.array([3, 4])
np.dot(x, y) # 1 * 3 + 2 * 4 = 11
< What is @? >β
@ is Python's matrix-multiplication operator. For NumPy arrays, A @ B is shorthand for np.matmul(A, B).
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
A @ B
# array([[19, 22],
# [43, 50]])
For ordinary matrices, the inner dimensions must match:
(m, k) @ (k, n) -> (m, n)
Comparisonβ
< np.dot(), @, and * >β
In the examples below, A = np.array([1, 2]), B = np.array([3, 4]), and
C = np.array([[1, 2], [3, 4]]).
| Expression | Meaning | Example | How the example works | Recommendation |
|---|---|---|---|---|
np.dot(A, B) | Inner product for vectors; matrix product for two matrices | np.dot(A, B) β 11 | 1 Γ 3 + 2 Γ 4 | Fine for vector dot products; be deliberate with higher-rank arrays |
A @ B | Matrix multiplication; broadcasts leading batch dimensions | C @ A β [5, 11] | [1 x 1 + 2 x 2, 3 x 1 + 4 x 2]Takes one dot product per row of C | Prefer for neural-network and linear-algebra matrix products |
A * B | Element-wise multiplication | A * B β [3, 8] | [1 x 3, 2 x 4] Multiplies matching entries | Use when corresponding elements should be multiplied |
For 1-D vectors and ordinary 2-D matrices, np.dot(A, B) and A @ B produce the same result. They differ for
higher-rank arrays: @ treats the final two axes as matrices and broadcasts the leading axes, whereas np.dot() uses
a different axis-contraction rule. Also, @ does not accept scalar operands; use * for scalar multiplication.
Related ideasβ
- Linear Algebra covers vectors, matrices, and matrix multiplication.
- PyTorch uses similar tensor operations;
torch.matmuland@perform matrix multiplication.