Chapter 42. In Depth: Linear Regression
Just as naive Bayes (discussed in Chapter 41) is a good starting point for classification tasks, linear regression models are a good starting point for regression tasks. Such models are popular because they can be fit quickly and are straightforward to interpret. You are already familiar with the simplest form of linear regression model (i.e., fitting a straight line to two-dimensional data), but such models can be extended to model more complicated data behavior.
In this chapter we will start with a quick walkthrough of the mathematics behind this well-known problem, before moving on to see how linear models can be generalized to account for more complicated patterns in data.
We begin with the standard imports:
In
[
1
]:
%
matplotlib
inlineimport
matplotlib.pyplot
as
plt
plt
.
style
.
use
(
'seaborn-whitegrid'
)
import
numpy
as
np
Simple Linear Regression
We will start with the most familiar linear regression, a straight-line fit to data. A straight-line fit is a model of the form:
where is commonly known as the slope, and is commonly known as the intercept.
Consider the following data, which is scattered about a line with a slope of 2 and an intercept of –5 (see Figure 42-1).
In
[
2
]:
rng
=
np
.
random
.
RandomState
(
1
)
x
=
10
*
rng
.
rand
(
50
)
y
=
2
*
x
-
5
+
rng
.
randn
(
50
)
plt
.
scatter
(
x
,
y
);
Get Python Data Science Handbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.