Grokking Machine Learning cover
welcome to this free extract from
an online version of the Manning book.
to read more
or

3 Drawing a line close to our points: Linear regression

 

This chapter covers

· What is linear regression?

· How to predict the price of a house based on known prices of other houses

· How to fit a line through a set of data points.

· How to code the linear regression algorithm in Python.

· Examples of linear regression in the real world, such as medical applications and recommender systems.

In this chapter we learn linear regression. Linear regression is a very powerful and common method to estimate values, such as the price of a house, the value of a certain stock, the life expectancy of an individual, the amount of time a user will watch a video or spend in a website, etc. In most books you will find linear regression as a plethora of complicated formulas, matrices, derivatives, determinants, etc. Here, this will not happen, I promise. All you need to have is the ability to visualize points and lines in a plane, and visualize the lines moving around to get closer to the points.

The mental picture of linear regression is simple. Let us say we have some points, that roughly look like they are forming a line, like in figure3.1.

Figure 3.1. Some points that roughly look like forming a line.

The goal of linear regression is to draw the line that passes as close as possible to these points. What line would you draw, that goes close to those points? The one I drew is in figure3.2.

Figure 3.2. A line that goes close to the points.

3.1       The problem: We need to predict the price of a house

3.2       The solution: Building a regression model for housing prices

3.2.1   The remember step: looking at the prices of existing houses

3.2.2   The formulate step: formulating a rule that estimates the price of the house

3.2.3   The predict step: what do we do when a new house comes in the market?

3.2.4   Some questions that arise and some quick answers

3.3       How to get the computer to draw this line: the linear regression algorithm

3.3.1   Crash course on slope and y-intercept

3.3.2   A simple trick to move a line closer to a set of points, one point at a time.

3.3.3   The square trick: A much more clever way of moving our line closer to one of the points

3.3.4   The linear regression algorithm: Repeating the square trick many times

3.3.5   Plotting dots and lines

3.3.6   Using the linear regression algorithm in our dataset

3.4       Applications of linear regression

3.4.1   Video and music recommendations

sitemap