MLG 005 Linear Regression
Linear regression predicts a number, such as a house price, by fitting a line to data. Also covers the cost function, gradient descent, multivariate regression and the Andrew Ng course to take next. Links Notes and resources at ocdevel.com/mlg/5 Join the free MLG group: listen to my other AI shows, ask me questions, and talk with other listeners What linear regression is Linear regression is a supervised learning algorithm that predicts a number (regression) rather than a class (classification) by fitting a line to past examples. It is the first algorithm everyone learns in machine learning. The example, carried over from MLG 004: estimate a Portland house's price from its square footage, bedrooms, bathrooms and distance to downtown. The three steps of every algorithm in the series, each with several names: predict (hypothesis, objective, estimate), measure the error (cost, loss) and train (learn, fit). The hypothesis function The hypothesis function makes the prediction. It multiplies each feature by a weight, written theta (θ) and also called a coefficient or parameter, and adds the results. Training means finding the θ values. h(x) = θ₀ + θ₁x₁ + θ₂x₂ + ... With one feature, square footage, this is the slope-intercept line y = mx + b from algebra: h(x) = θ₀ + θ₁x, where θ₁ is the slope. θ₀ is the bias, where the line crosses the y-axis: the prediction when you have no other information, like the average house price in Portland. Plot square footage against each house's actual price (its label, y) and you get a cloud of dots running up and to the right. A good hypothesis is a line through the middle of it. The cost function: mean squared error The cost function scores how wrong the predictions are. For linear regression it is the mean squared error. The first pass uses random θ values, so the guesses start far off, like $20 for a $200,000 house. cost = 1/(2m) × Σ (h(x) − y)² m is the number of training examples, h(x) is the prediction (also written ŷ) and y is the actual price. Squaring keeps positive and negative errors from canceling out. The square and the 2 in the denominator cancel when you take the derivative. Picture a bowl: θ₀ and θ₁ are the floor axes and the cost is the height, so each pair of θ values is a point on the bowl. Gradient descent Gradient descent is the training step. It moves that point down the bowl to the bottom, where the cost is lowest. Each step takes the derivative of the cost function at the current point, which is the slope there, and moves each θ downhill, turning the line toward the center of the data. The steeper the slope, the bigger the step. θ₀ := θ₀ − α × (1/m) × Σ (ŷ − y) θ₁ := θ₁ − α × (1/m) × Σ (ŷ − y) × x α (alpha) is the learning rate, which the episode leaves to the course below. The cost never reaches zero on real data, because the points don't all sit on one line. Univariate and multivariate linear regression One feature is univariate linear regression. Real problems use many, which is multivariate linear regression: the same three steps with one θ per feature. More features generally mean better predictions, up to a point. Assigned course: Andrew Ng's Machine Learning The episode assigns one resource before you go further: Andrew Ng's machine learning course on Coursera, now the Machine Learning Specialization. It is self-paced, with video visualizations, quizzes and programming exercises, and teaches the math as it goes. The host finished the original course in three weeks; 10 to 20 minutes a day is enough. Next in the series: logistic regression, the classifier counterpart.
Embed this episode
No transcript yet.