Okay, now that we understand matrices as structures for organizing data, let's look at the fundamental operations we can perform on them. Just like with vectors, these operations have both mathematical definitions and practical applications in data manipulation and algorithm implementation. We'll cover addition, subtraction, scalar multiplication, and transposition. Performing these operations efficiently is important, and we'll see how NumPy makes this straightforward.
Adding or subtracting matrices is intuitive if you're familiar with vector addition. The rule is simple: you can only add or subtract matrices if they have the exact same dimensions (same number of rows and same number of columns). The operation is performed element-wise.
If you have two matrices, A and B, both with dimensions m×n, their sum C=A+B is also an m×n matrix where each element cij is the sum of the corresponding elements from A and B:
cij=aij+bijSimilarly, for subtraction D=A−B, each element dij is:
dij=aij−bijExample:
Let's say we have two 2×3 matrices:
A=[142536],B=[708192]Their sum A+B is:
A+B=[1+74+02+85+13+96+2]=[84106128]Their difference A−B is:
A−B=[1−74−02−85−13−96−2]=[−64−64−64]Implementation with NumPy:
NumPy makes these operations trivial using the standard +
and -
operators.
import numpy as np
# Define matrices A and B
A = np.array([[1, 2, 3],
[4, 5, 6]])
B = np.array([[7, 8, 9],
[0, 1, 2]])
# Matrix Addition
C = A + B
print("Matrix A:\n", A)
print("\nMatrix B:\n", B)
print("\nMatrix Sum (A + B):\n", C)
# Matrix Subtraction
D = A - B
print("\nMatrix Difference (A - B):\n", D)
# Attempting addition with different shapes will raise an error
# E = np.array([[1, 2], [3, 4]])
# try:
# F = A + E
# except ValueError as e:
# print(f"\nError adding A and E: {e}")
Relevance in ML: While less common than matrix multiplication, addition and subtraction appear when combining or comparing datasets represented as matrices, calculating differences between predicted and actual values (error matrices), or updating weight matrices in certain optimization algorithms.
Multiplying a matrix by a scalar (a single number) is also an element-wise operation. You simply multiply every element in the matrix by that scalar.
If A is an m×n matrix and c is a scalar, then the product cA is an m×n matrix where each element is c⋅aij.
Example:
Using matrix A from before and a scalar c=3:
A=[142536] 3A=[3×13×43×23×53×33×6]=[312615918]Implementation with NumPy:
Use the standard *
operator between a scalar and a NumPy array.
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6]])
scalar = 3
# Scalar Multiplication
scaled_A = scalar * A
print("Matrix A:\n", A)
print(f"\nScalar: {scalar}")
print("\nScaled Matrix (scalar * A):\n", scaled_A)
# Can also multiply array by scalar
scaled_A_alt = A * scalar
print("\nScaled Matrix (A * scalar):\n", scaled_A_alt)
Relevance in ML: Scalar multiplication is frequently used when scaling features in a dataset, adjusting learning rates in gradient descent (where you multiply the gradient matrix by the learning rate), or applying regularization terms.
The transpose of a matrix is obtained by swapping its rows and columns. The element at row i, column j in the original matrix A moves to row j, column i in the transposed matrix, denoted as AT.
If A is an m×n matrix, its transpose AT will be an n×m matrix.
Example:
A=[142536](2×3 matrix)Its transpose AT is:
AT=123456(3×2 matrix)Notice how the first row [1,2,3] became the first column, and the second row [4,5,6] became the second column.
A property to remember is that the transpose of a transpose returns the original matrix: (AT)T=A.
Implementation with NumPy:
NumPy arrays have a convenient .T
attribute for transposition. Alternatively, you can use the np.transpose()
function.
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6]])
# Transpose using .T attribute
A_transpose_T = A.T
print("Original Matrix A (2x3):\n", A)
print("\nTranspose using .T (3x2):\n", A_transpose_T)
# Transpose using np.transpose()
A_transpose_func = np.transpose(A)
print("\nTranspose using np.transpose() (3x2):\n", A_transpose_func)
# Transposing a row vector (1D array treated as row)
row_vec = np.array([7, 8, 9]) # Shape (3,)
print("\nOriginal row vector (shape {}):\n".format(row_vec.shape), row_vec)
# Note: .T on a 1D array does nothing!
print("\n.T on 1D array (shape {}):\n".format(row_vec.T.shape), row_vec.T)
# To transpose a 1D array, first make it 2D (e.g., 1xN or Nx1)
row_vec_2d = row_vec[np.newaxis, :] # Shape (1, 3)
col_vec = row_vec_2d.T # Shape (3, 1)
print("\nRow vector reshaped to 2D (shape {}):\n".format(row_vec_2d.shape), row_vec_2d)
print("\nTransposed to column vector (shape {}):\n".format(col_vec.shape), col_vec)
Note: Transposing a 1-dimensional NumPy array (like
np.array([1, 2, 3])
) using.T
doesn't change its shape. To treat it as a mathematical row vector and transpose it into a column vector, you often need to explicitly give it two dimensions (e.g., shape(1, n)
for a row vector or(n, 1)
for a column vector) using techniques likereshape()
or adding a new axis withnp.newaxis
ornp.expand_dims()
.
Relevance in ML: Transposition is a fundamental operation used extensively in linear algebra formulas that appear in machine learning. For example, the normal equation for solving linear regression involves (XTX)−1XTy. It's also used when calculating covariance matrices (XTX, assuming X is centered) or when manipulating data shapes to match requirements for matrix multiplication or other library functions.
These basic operations form the building blocks for more complex matrix manipulations we'll encounter later, particularly matrix multiplication, which is central to linear transformations. Mastering these fundamentals with NumPy provides a solid base for implementing ML algorithms.
© 2025 ApX Machine Learning