Skip to main content

πŸ“ 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:

InputsResult
Two 1-D vectorsInner product: sum of element-wise products
Two 2-D arraysMatrix multiplication
A scalar and an arrayScalar multiplication; use * instead for clarity
Higher-dimensional arraysContracts 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]]).

ExpressionMeaningExampleHow the example worksRecommendation
np.dot(A, B)Inner product for vectors; matrix product for two matricesnp.dot(A, B) β†’ 111 Γ— 3 + 2 Γ— 4Fine for vector dot products; be deliberate with higher-rank arrays
A @ BMatrix multiplication; broadcasts leading batch dimensionsC @ 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 * BElement-wise multiplicationA * B β†’ [3, 8][1 x 3, 2 x 4] Multiplies matching entriesUse 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.

  • Linear Algebra covers vectors, matrices, and matrix multiplication.
  • PyTorch uses similar tensor operations; torch.matmul and @ perform matrix multiplication.

Reference​