Skip to content
MLG 007 Logistic Regression episode artwork

Feb 19, 2017 34 min

MLG 007 Logistic Regression

from Machine Learning Guide

Logistic regression is a classifier: it passes linear regression through the sigmoid function to get a probability, then calls anything over 0.5 a yes. Also covers the decision boundary, log loss, gradient descent, argmax and softmax for more classes, and logistic regression as a neuron. Links Notes and resources at ocdevel.com/mlg/7 Join the free MLG group: listen to my other AI shows, ask me questions, and talk with other listeners What is logistic regression? A supervised learning algorithm for classification. It predicts a class, such as house, tree or dog, where linear regression (MLG 005) predicts a number. Despite its name, it's a classifier: it runs linear regression and passes the result through the logistic function. The example: expensive or not The Portland house spreadsheet from MLG 005: one row per house, with columns for square footage, bedrooms, bathrooms and distance to downtown. Replace each price label (y) with 1 if the house cost over $300,000 and 0 if not, and the problem becomes binary classification. Logistic regression takes the same three steps as linear regression, with different functions: Predict with the hypothesis function, starting from random θ (theta) values. Measure the error with the cost function. Train θ with gradient descent to shrink that error. Step 1: the sigmoid hypothesis function The hypothesis computes linear regression's output, z = θᵀx (theta transpose x), called the logit. The sigmoid, or logistic function, turns it into a probability between 0 and 1: h(x) = 1 / (1 + e^(−θᵀx)) θ is the vector of learned weights and x is a row of features. θᵀx is the vectorized form of θ₀ + θ₁x₁ + θ₂x₂ + ... (the newer Specialization writes it w·x + b). An output of 0.7 means the model is 70% confident the house is expensive. Over 0.5 is a yes, under 0.5 a no. What does the sigmoid curve look like? A stretched-out S. Plotted against z, it approaches 0 as z goes to negative infinity, climbs steeply through 0.5 where it crosses the y-axis at z = 0, and levels off toward 1 as z goes to positive infinity. The decision boundary is where θᵀx = 0 and the output is 0.5. Training moves it to separate the expensive houses from the rest. Step 2: the log loss cost function Linear regression's mean squared error, wrapped around the sigmoid, gives a bumpy cost with many local minima, so gradient descent can get stuck. Logistic regression instead uses log loss, also called the negative log-likelihood or binary cross-entropy. It's convex, with a single minimum, and it charges more the more confident a wrong guess was: Expensive house (y = 1): the cost is −log(h(x)), 0 when the guess is 1 and rising toward infinity as the guess nears 0. Not expensive (y = 0): the cost is −log(1 − h(x)), 0 when the guess is 0 and rising toward infinity as the guess nears 1. Guessing 0.2 for an expensive house costs less than guessing 0. One formula covers both cases, averaged over the m rows: cost = −(1/m) × Σ [ y × log(h(x)) + (1 − y) × log(1 − h(x)) ] For each row one term drops out: y = 1 zeroes the second, y = 0 the first. Step 3: gradient descent Gradient descent takes the derivative of the cost, which says which direction and how far to move each θ, and repeats until the cost reaches its minimum (MLG 005). The update rule has the same form as linear regression's, with the sigmoid as h(x): θⱼ := θⱼ − α × (1/m) × Σ (h(x) − y) × xⱼ α (alpha) is the learning rate and xⱼ is the row's value for feature j. Once trained, a new house goes through h(x), and over 0.5 means expensive. More than two classes: argmax and softmax With several classes, say house, tree, dog and cat, the model computes one logit per class, and argmax picks the class with the highest. A sigmoid on each class gives probabilities like 0.7 house, 0.5 tree and 0.3 dog, which don't sum to 1. When the architecture needs a proper probability distribution, softmax turns the logits into probabilities that do (MLG 027). Classification and action: the lion example On the savanna, recognizing a lion is classification, and deciding to run is a second learned step, choosing an action. In machine learning, a convolutional neural network (MLG 025) can do the seeing and a deep Q-network (MLG 029) the choosing. Both build on units like linear and logistic regression. How is logistic regression a neuron? Machine learning models nest functions inside each other, as in functional programming. For logistic regression, gradient descent wraps the cost function, the cost wraps the sigmoid, and the sigmoid wraps linear regression. Deep learning stacks these units: a neuron with a sigmoid activation is a logistic regression unit (MLG 009). Modern hidden layers mostly use ReLU and its variants, keeping sigmoid or softmax for the output layer. Logistic regression resources Andrew Ng's Machine Learning Specialization on Coursera, which replaced the original course the episode assigns. Its first course, Supervised Machine Learning: Regression and Classification, teaches logistic regression in week 3. MLG 006 weighs courses against certificates and degrees. More in the resources. Next: MLG 008, on the math for machine learning. MLG 010 covers languages and frameworks.

Episode details come from the podcast's RSS feed.

Embed this episode

Ready to play

MLG 007 Logistic Regression

0:00 34:39

No transcript yet.

Requests help us decide what to transcribe next.

Frequently asked questions

How long is this episode of Machine Learning Guide?

This episode is 34 minutes long.

When was this Machine Learning Guide episode published?

This episode was published on February 19, 2017.

Can I download this Machine Learning Guide episode?

Yes. Use the download control on the episode player to save the publisher-provided media file.