Skip to main content

Module 1 — Vectors and matrices: the language of data

Before any formula, one idea: in machine learning, data is numbers laid out in order. A vector lays out the features of one observation; a matrix stacks all observations. Understand this layout and you already read half of what a model does.

The vector: one observation, a list of numbers

A vector is an ordered list of numbers. In learning it describes one observation through its features:

x=(area,bedrooms,age)=(72,3,15)x = (\text{area}, \text{bedrooms}, \text{age}) = (72, 3, 15)

This home becomes the point (72,3,15)(72, 3, 15) in a three-dimensional space. Each feature is an axis; the observation is a point. With 50 features you can no longer draw the space, but the algebra works exactly the same — that is its power.

import numpy as np
home = np.array([72, 3, 15]) # a vector with 3 components
home.shape # (3,) — 3 features

The matrix: the whole dataset

Stack several observations and you get a matrix: one row per observation, one column per feature.

X=(72315904845230)X = \begin{pmatrix} 72 & 3 & 15 \\ 90 & 4 & 8 \\ 45 & 2 & 30 \end{pmatrix}

This is exactly the shape of a pandas DataFrame or a CSV file. The convention is universal in data science: X of shape (n_observations, n_features). Remembering it prevents the most common beginner mistake — swapping rows and columns.

X = np.array([[72, 3, 15],
[90, 4, 8],
[45, 2, 30]])
X.shape # (3, 3) — 3 homes, 3 features
X[0] # first home (first row)
X[:, 0] # column of areas (all observations)

Reading dimensions: the reflex that prevents bugs

Almost every error in applied linear algebra comes from a shape mismatch. Get into the habit of announcing the shape of each object:

ObjectShapeMeaning
An observation vector(d,)d features
The dataset X(n, d)n observations, d features
The target vector y(n,)one value to predict per observation
The weights of a linear model(d,)one weight per feature

When an operation fails, the first question is always: "are the shapes compatible?" This reflex resolves most error messages before you even read the trace.

Basic operations: addition and scalar multiplication

Two vectors of the same size add component by component; multiplying by a number stretches or shrinks the vector:

2(72,3,15)=(144,6,30)2 \cdot (72, 3, 15) = (144, 6, 30)
a = np.array([72, 3, 15])
b = np.array([10, 1, 5])
a + b # array([82, 4, 20]) — component-wise addition
2 * a # array([144, 6, 30]) — scaling

These two seemingly trivial operations are the foundation of everything: a linear model is nothing but a combination of vectors (scale then add), and normalizing data is a scalar multiplication applied column by column.

Why this framing rather than loops

We could have added two lists with a Python loop. But as soon as data grows, writing the operation in vector language lets NumPy delegate it to optimized code — the vectorization from the Python course. Matrix language isn't just elegant notation: it's what makes the computation fast enough to be usable.

Summary

  • A vector describes one observation through its features; it is a point in a d-dimensional space.
  • A matrix X of shape (n, d) stacks n observations as rows, d features as columns — the shape of a CSV.
  • Reading and announcing dimensions is the reflex that prevents most linear-algebra bugs.
  • Component-wise addition and scalar multiplication are the building blocks of every linear model and every normalization.

Next module: the matrix product, the operation that actually transforms data and drives every layer of a network.