Podcast Lookup

MLG 008 Math for Machine Learning

Machine Learning Guide

  • February 23, 2017
  • 28 min
  • Season 1, episode 8
Episode page

About this episode

The math for machine learning is linear algebra, statistics and calculus, and you can learn it through machine learning instead of before it. Also covers matrices, vectors and tensors, probability and inference, gradient descent, convex optimization, and where to learn each.

Links What math do you need for machine learning?

Linear algebra, statistics and calculus, and you don't have to learn them first. Intro courses like Andrew Ng's open with a primer on the math they use. Go deeper afterward, or give math 20% of your learning time, one day a week. In cooking terms, linear algebra is chopping vegetables, statistics is the cookbook and calculus is the oven.

Linear algebra: matrices and vectors

Linear algebra is math on matrices and vectors, the easiest of the three. The house spreadsheet from MLG 004 is a matrix, capital X: rows are houses and columns are features. The learned weights, theta (θ), are a vector, a list of numbers. A prediction multiplies each feature by its weight and adds the results:

h(x) = θ₀ + θ₁x₁ + θ₂x₂ + θ₃x₃

Nested Python for loops are too slow for a million rows. Linear algebra does it in one operation, run on a CPU or GPU with SIMD (single instruction, multiple data).

What is a tensor?

A tensor is a list of numbers with any number of dimensions:

  • 0D: a number
  • 1D: a vector
  • 2D: a matrix
  • 3D: a cube, like an image, whose pixels each hold red, green and blue values

TensorFlow is named for it, and it's the core data type of PyTorch, now the most used framework. MLA 005 covers tensor shapes.

Statistics: probability and inference

Statistics supplies the algorithms and is the hardest of the three. Hypothesis functions (linear and logistic regression) and error functions (mean squared error, log likelihood) are statistics formulas, like the logistic regression cost function:

cost = −(1/m) × Σ [ y × log(h(x)) + (1 − y) × log(1 − h(x)) ]
  • Probability describes how data is distributed, like prices in a housing market.
  • Inference predicts a new example, like one house, from that distribution.
Calculus: derivatives and gradient descent

Calculus does the training. The derivative of the loss function is the slope of the error graph at the current θ values, telling each θ which way is downhill and how big a step to take. Repeating that step is gradient descent (MLG 005). Derivatives mostly come from memorized rules, like the power rule and chain rule.

Convex optimization

Optimization is the branch of math that uses calculus to minimize a function. Convex means the error graph is shaped like a cup. It's often listed as a fourth prerequisite, but you learn it with calculus.

Math courses, textbooks and primers

The full math track is in the resources.

The Great Courses: Mathematical Decision Making

Mathematical Decision Making: Predictive Models and Optimization is a 24-lecture video course by Scott P. Stevens on operations research: the math of scheduling employees, train departures and factory settings, very close to machine learning's. The Great Courses also has calculus, statistics and linear algebra courses, in the resources.

They're a supplement: the lecturers narrate what they draw, so the videos work as audio. Convert video files to mp3 has the bash script.

Next: MLG 009, on deep learning.