Course contentsShow
← Machine Learning and Deep Learning
Lesson 158 of 3,5382. Machine Learning Mindset and Core ConceptsFree lesson

Linear Algebra Operations

Matrix multiplication with np.dot and @, computing norms, and matrix operations fundamental to ML algorithms.

Linear Algebra Operations

What you'll learn: How to perform matrix multiplication and compute norms using NumPy—operations that power nearly every ML algorithm.

Why This Matters

Machine learning models are fundamentally built on linear algebra. When a neural network makes predictions, when you train a regression model, or when you reduce dimensionality—you're doing matrix operations. Understanding these operations unlocks how ML actually works under the hood.

Core Operations

Matrix Multiplication is the heart of ML computations. In NumPy, you can multiply matrices using either np.dot() or the @ operator:

import numpy as np

# Feature matrix (3 samples, 4 features)
X = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12]])

# Weight vector (4 features)
w = np.array([0.1, 0.2, 0.3, 0.4])

# Compute predictions: both methods work identically
predictions = np.dot(X, w)  # Traditional
predictions = X @ w          # Modern (Python 3.5+)

This operation is exactly what happens in linear regression or a neural network layer!

Norms measure the "size" or "length" of vectors—crucial for regularization and distance calculations:

# L2 norm (Euclidean length) - most common
vector = np.array([3, 4])
length = np.linalg.norm(vector)  # Returns 5.0

# L1 norm (Manhattan distance)
l1 = np.linalg.norm(vector, ord=1)  # Returns 7.0

Think of norms as measuring how far a point is from the origin—like measuring the length of an arrow in space.

Key Takeaway: Matrix multiplication (@ or np.dot()) transforms data through learned weights, while norms measure vector magnitudes—these two operations form the computational backbone of machine learning algorithms.