AI Hints
Home›Statistics & Math for ML Tutorial›Length, Similarity and Matrices
Statistics & Math for ML · Lesson 81 of 138

Matrices

Tables of numbers.

⏱ 2 min read▶ Video at 1:35:37🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

A vector is one row of data. Stack many vectors together, and you get a matrix, a rectangle of numbers. Your whole dataset is a matrix, with one row per example, and one column per feature.

Matrices
From the video: Matrices
  • Stack vectors together
  • A rectangle of numbers
  • Your whole dataset

A data matrix

Here are four houses, each with size, rooms, and age. The shape is four by three, four rows and three columns. We can pick a row, which is one house, or a column, which is one feature. And dot T, the transpose, flips rows and columns. Run it.

matrix.pypython
import numpy as np

X = np.array([[120, 4, 10],
              [ 80, 3, 35],
              [200, 6,  2],
              [150, 5, 20]])

print("shape:", X.shape)
print("house 0:", X[0])
print("sizes:", X[:, 0])
print("transpose shape:", X.T.shape)
Output
shape: (4, 3)
house 0: [120   4  10]
sizes: [120  80 200 150]
transpose shape: (3, 4)

Rows and columns

By convention, a matrix with M rows and N columns is called M by N. In machine learning, the data matrix is usually called capital X, with rows as examples and columns as features. Images are matrices too, with one number per pixel. A colour image is three matrices stacked together, for red, green, and blue.

keyvalue
m × nm rows, n columns
Xexamples × features
X[:, j]one feature
X.Tthe transpose

Summary

A matrix is a table of numbers. Your dataset is a matrix, X, with examples as rows and features as columns. Next, multiplying matrices.

Key takeaways
  • shape (rows, cols)
  • X[i] and X[:, j]
  • X.T flips it
Next up: Matrix Multiplication — rows times columns