Matrices in Machine Learning – Basics with Python & NumPy

Matrices in Machine Learning – Basics with Python & NumPy

In our journey through the math of machine learning, we've mastered vectors. We've added them, scaled them, and used the dot product to measure their similarity. But in the real world, we often need to work with entire collections of vectors at once. That's where Matrices come in.

Watch the video for a quick visual walkthrough, then read on for the code and detailed explanations.

You can also watch it directly on YouTube.

The Intuition: A Grid of Numbers

So, what exactly is a matrix? The simplest way to think of it is as a stack of vectors or a rectangular grid of numbers, arranged in rows and columns. This structure makes them incredibly useful for organizing data.

In machine learning, matrices are the backbone of data representation — from storing datasets in tabular form to holding the weights of a neural network.

  • An image can be a matrix, where each number represents the brightness of a pixel.
  • A dataset can be a matrix, where each row is a user and each column is a feature (like age, height, or location).

We describe the size of a matrix by its dimensions: rows by columns. For example, a matrix with 2 rows and 3 columns is a 2 * 3 (2 by 3) matrix.

2 * 3 matrix

A simple 2 * 3 matrix with 2 rows and 3 columns.

Basic Operations

Just like with vectors, we can perform basic mathematical operations on matrices.

Matrix Addition

To add two matrices, they must have the exact same dimensions. The process is simple: you just add the corresponding elements together.

A = [[1, 2, 5], [3, 4, 6]]
B = [[8, 1, 0], [2, 5, 3]]
A + B = [[9, 3, 5], [5, 9, 9]]

Scalar Multiplication

This is also just like it is with vectors. You take a single number (a scalar) and multiply every single element inside the matrix by it.

2 * A = [[2, 4, 10], [6, 8, 12]]

The Code: Using NumPy

NumPy is the standard library for numerical operations in Python, and it makes working with matrices incredibly easy.

Creating a Matrix in NumPy

import numpy as np

# Create a matrix directly
A = np.array([
    [1, 2, 5],
    [3, 4, 6]
])

# Or create from row vectors
v1 = np.array([1, 2, 5])
v2 = np.array([3, 4, 6])
A_from_stack = np.vstack([v1, v2])

print(A_from_stack)
# Output:
# [[1 2 5]
#  [3 4 6]]

# Or create from column vectors
v1 = np.array([1, 3])
v2 = np.array([2, 4])
B_from_stack = np.column_stack([v1, v2])

print(B_from_stack)
# Output:
# [[1 2]
#  [3 4]]

Matrix Addition in NumPy

A = np.array([[1, 2, 5], [3, 4, 6]])
B = np.array([[8, 1, 0], [2, 5, 3]])

# Addition
C = A + B
print(C)
# Output:
# [[9 3 5]
#  [5 9 9]]

Scalar Multiplication in NumPy

# Scalar Multiplication
D = 2 * A
print(D)
# Output:
# [[ 2  4 10]
#  [ 6  8 12]]

What's Next?

These basic operations are the foundation for manipulating data. But the real magic happens when we multiply one matrix by another. Matrix multiplication is the core operation that powers everything from 3D graphics to the calculations in every layer of a neural network.

Read next: Matrix Multiplication – Explained Visually

Your Turn...

In matrix addition, the matrices must be the same size. Do you think the same rule applies to matrix multiplication? Why or why not? Share your thoughts in the comments!

This post is part of the "Decoding the Math of ML" series. For the previous part, check out: The Dot Product (Explained Visually).

Comments

Popular posts from this blog

Character to Numeric conversion in RPG - Allow Blanks & Thousands Separator - IBM i

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

All about READ in RPGLE & Why we use it with SETLL/SETGT?