Matrix multiplication is a fundamental operation in linear algebra with critical applications in machine learning, neural networks, and data science. This guide covers matrix multiplication mechanics, Python implementation, and important conventions.
The dot product has a geometric interpretation:x⋅y=∣x∣∣y∣cos(θ)where θ is the angle between the vectors.
Orthogonality Test: If vectors are orthogonal (perpendicular), θ=90° and cos(90°)=0, so their dot product equals zero.
# Test orthogonal vectorsi = np.array([1, 0, 0])j = np.array([0, 1, 0])print(f"Dot product of i and j: {np.dot(i, j)}")# Output: Dot product of i and j: 0
Vectorized operations are significantly faster than loops, especially for large datasets. Always prefer NumPy’s built-in functions for production code.
If A is an m×n matrix and B is an n×p matrix, the product C=AB is an m×p matrix where:cij=k=1∑naikbkjEach element cij is the dot product of the i-th row of A and the j-th column of B.
try: result = np.matmul(B, A) # 2x3 times 3x3 = OK (2x3) print("This works! Shape:", result.shape)except ValueError as err: print(err)try: result = B @ A # Same as above print("This works! Shape:", result.shape)except ValueError as err: print(err)
Critical Rule: The number of columns in the first matrix must equal the number of rows in the second matrix. This is essential for neural networks and deep learning.
NumPy automatically handles certain dimension mismatches:
x = np.array([1, -2, -5])y = np.array([4, 3, -1])print("Shape of x:", x.shape) # Output: (3,)print("Dimensions:", x.ndim) # Output: 1# This works due to broadcastingresult = x @ yprint(f"Result: {result}")# Output: Result: 3 (dot product)
NumPy treats 1-D vectors specially: in x @ y, vector x is automatically transposed to make the operation valid, computing the dot product xTy.
try: result = np.matmul( x.reshape((3, 1)), y.reshape((3, 1)) )except ValueError as err: print(err) # Output: matmul: Input operand 1 has a mismatch in its core dimension