Understanding Linear Algebra for Machine Learning: A Comprehensive Guide

Puja Chaudhury
4 min readSep 18, 2023

--

Linear algebra is the cornerstone of machine learning and data science. Whether you’re working on computer vision, natural language processing, or any other machine learning domain, a strong grasp of linear algebra is essential. This blog post aims to provide a comprehensive guide to understanding the key concepts and operations in linear algebra that are crucial for machine learning.

What is Linear Algebra?

Linear algebra is a branch of mathematics that deals with vector spaces and linear equations. It provides the mathematical foundation for machine learning algorithms, allowing us to understand data structures, transformations, and operations that are fundamental to the field.

The Tensor Data Structure

Scalars
A scalar is a zero-dimensional tensor, essentially a single numerical value. In Python, you can represent a scalar using a standard numerical type like an integer or a float.


# Scalar in Python
scalar = 42

Vectors
A vector is a one-dimensional tensor. It’s an ordered list of numbers, often used to represent a point in space. In Python, you can use lists or NumPy arrays to represent vectors.

# Vector in Python
import numpy as np
vector = np.array([1, 2, 3])

Matrices
A matrix is a two-dimensional tensor. It’s an array of numbers arranged in rows and columns. Matrices are often used to represent transformations, systems of equations, and more.

# Matrix in Python
matrix = np.array([[1, 2], [3, 4], [5, 6]])

Higher-Dimensional Tensors
Tensors can have more than two dimensions. These higher-dimensional tensors are common in machine learning, especially in tasks like image processing.

# 4D Tensor in Python using PyTorch
import torch
tensor_4d = torch.zeros((32, 28, 28, 3))

Special Types of Vectors

Unit Vectors
Unit vectors are vectors whose L2 norm (Euclidean length) is equal to 1. These vectors are often used as directional components and are crucial in transformations and physics simulations. In machine learning, unit vectors are commonly used for normalization.

# Create a vector
vector = np.array([1, 2, 2])

# Normalize to make it a unit vector
unit_vector = vector / np.linalg.norm(vector)

Orthogonal Vectors
Orthogonal vectors are vectors that are perpendicular to each other, meaning their dot product is zero. These vectors are essential in operations like QR decomposition and in methods like Principal Component Analysis (PCA).

# Two orthogonal vectors
vector1 = np.array([1, 0])
vector2 = np.array([0, 1])

# Their dot product should be zero
np.dot(vector1, vector2)

Orthonormal Vectors
Orthonormal vectors are both orthogonal and have a unit norm. They are often used as basis vectors in linear transformations.

# Orthonormal vectors are both unit vectors and orthogonal
# vector1 and vector2 from the previous examples are orthonormal

Vector Norms

L1 Norm
The L1 norm is the sum of the absolute values of the elements in the vector. It is often used in optimization problems and regularization.

# L1 norm
l1_norm = np.sum(np.abs(vector))

L2 Norm
The L2 norm is the square root of the sum of the squares of the elements. It gives the “Euclidean length” of the vector and is the most commonly used norm.

# L2 norm
l2_norm = np.linalg.norm(vector)

Squared L2 Norm
The squared L2 norm is simply the sum of the squares of the elements. It’s computationally cheaper and is often used in machine learning algorithms for optimization.

# Squared L2 norm
squared_l2_norm = np.sum(np.square(vector))

Max Norm
The max norm is the maximum absolute value among the elements of the vector. It’s less common but can be useful in specific optimization problems.

# Max norm
max_norm = np.max(np.abs(vector))

Common Tensor Operations

Dot Product
The dot product measures the similarity between two vectors. It’s a fundamental operation in machine learning algorithms like SVM and K-means clustering.

# Dot product
dot_product = np.dot(vector1, vector2)

Matrix Multiplication
Matrix multiplication is used to transform one matrix into another. It’s a cornerstone in neural networks and many optimization algorithms.

# Matrix multiplication
result = np.dot(matrix1, matrix2)

Element-wise Operations
Element-wise operations are performed on corresponding elements of tensors. These are crucial for neural network activation functions, among other things.

# Element-wise addition
sum_matrix = np.add(matrix1, matrix2)

Creating Tensors in Python

NumPy
NumPy is the go-to library for numerical operations, offering a powerful n-dimensional array object.

import numpy as np
matrix_numpy = np.array([[1, 2], [3, 4]])

PyTorch
PyTorch is a popular library for deep learning. It offers dynamic computation graphs and a wide array of pre-built layers and functions.

import torch
matrix_pytorch = torch.tensor([[1, 2], [3, 4]])

TensorFlow
TensorFlow is another widely-used machine learning library. It’s known for its performance and scalability.

import tensorflow as tf
matrix_tf = tf.constant([[1, 2], [3, 4]])

Conclusion

Understanding linear algebra is crucial for anyone diving into machine learning. From data structures like vectors and matrices to operations like dot products and norms, linear algebra provides the building blocks for creating and understanding machine learning algorithms. Whether you’re a beginner or an experienced practitioner, a solid grasp of these concepts will go a long way in your machine-learning journey.

--

--

Puja Chaudhury
Puja Chaudhury

Written by Puja Chaudhury

Masters' student in Robotics focusing on machine learning. Experienced in AI, NLP, and CV through internships, research projects, & published papers.

No responses yet