In the print dialog, choose "Save as PDF" as the destination.
Machine Learning, Week 3
Linear Regression
Predict a house price with a straight line: fit it by hand with least squares, then model 5000 real houses in scikit-learn.
Objectives
- ▸Explain what linear regression predicts, and simple versus multiple regression
- ▸Define a residual and the least-squares line, and compute the slope and intercept by hand
- ▸Compute MAE, MSE, RMSE and R² for a fitted line
- ▸Explore and prepare a dataset with
info,describe, a histogram and a correlation heatmap - ▸Fit
LinearRegression, read its coefficients, and predict a house by hand - ▸Evaluate the model on test data and apply the steps to your project
Week 3 of the plan
Where This Sits in the Course
- ▸Notebook: Linear_Regression_Practice, the prices of 5000 houses in the USA
- ▸Real-world scenario: Predict House Pricing
- ▸Project milestone this week: Project Data Exploration and Data Cleaning
Plan for the Two Hours
| Part | What we do | Time |
|---|---|---|
| 1 | The idea, least squares, a worked example by hand | 35 min |
| 2 | Notebook: explore and prepare the housing data | 20 min |
| 3 | Notebook: fit the model, read the coefficients | 20 min |
| 4 | Evaluate on the test set, your project | 15 min |
| 5 | Practice with answers, then takeaways | 30 min |
Part 1
The Idea of Linear Regression
A straight line through the data
What Is Linear Regression?
- Linear regression
- A linear approach for modeling the relationship between a scalar dependent variable
yand one or more explanatory variablesX.
- ▸One explanatory variable: simple linear regression
- ▸More than one: multiple linear regression
- ▸Usually fitted with the least squares approach
The Model Is a Straight Line
- ▸
x: the input, for example the area income - ▸
ŷ("y hat"): the predicted value, for example the price - ▸
b₁: the slope, the change inŷwhenxgrows by 1 - ▸
b₀: the intercept, the value ofŷatx = 0
Residuals: The Error of Each Point
- ▸
yis the true value,ŷthe prediction - ▸A point above the line has a positive residual, below it a negative one
- ▸On a plot, the residual is the vertical distance from the point to the line
Least Squares
- ▸
SSE: the sum of squared errors over allnpoints - ▸The least-squares line is the line with the smallest SSE
- ▸Squaring makes every error positive and punishes big errors more
By hand
Worked Example: Five Real Houses
Rows of USA_Housing.csv. x: income in $10,000, y: price in $100,000, rounded
| House | Income | Price | (x, y) |
|---|---|---|---|
| H1 | $50,016 | $497,368 | (5, 5) |
| H2 | $59,769 | $897,291 | (6, 9) |
| H3 | $69,529 | $995,137 | (7, 10) |
| H4 | $79,628 | $1,196,064 | (8, 12) |
| H5 | $89,548 | $1,403,803 | (9, 14) |
Step 1: The Means
Step 2: Deviations from the Means
| House | (x − x̄, y − ȳ) | Product | (x − x̄)² |
|---|---|---|---|
| H1 | (-2, -5) | 10 | 4 |
| H2 | (-1, -1) | 1 | 1 |
| H3 | (0, 0) | 0 | 0 |
| H4 | (1, 2) | 2 | 1 |
| H5 | (2, 4) | 8 | 4 |
Step 3: Slope and Intercept
Step 4: Predictions and Residuals
| House | ŷ | e = y − ŷ | e² |
|---|---|---|---|
| H1 | 5.8 | -0.8 | 0.64 |
| H2 | 7.9 | 1.1 | 1.21 |
| H3 | 10.0 | 0.0 | 0.00 |
| H4 | 12.1 | -0.1 | 0.01 |
| H5 | 14.2 | -0.2 | 0.04 |
Step 5: Is It Really the Best Line?
| Line | SSE |
|---|---|
| ŷ = −3 + 2x | 7.0 |
| ŷ = −4 + 2x | 2.0 |
| ŷ = −4.7 + 2.1x | 1.9 |

Step 6: Predict a New Area
Try It: Least Squares, Live
Measuring the Error of a Line
- ▸MAE: the average size of an error, in the units of
y - ▸MSE: the average squared error, in squared units
- ▸RMSE: back in the units of
y, and big errors weigh more than in MAE
Worked Example: MAE, MSE, RMSE
R²: Better Than the Mean?
- ▸
SST: the SSE of the flat lineŷ = ȳ, the best guess without a feature - ▸R² = the share of that error the model removes: 1 is perfect, 0 is no better than the mean
Try It: What R² Measures
Part 2
Explore the Housing Data
The notebook, and the milestone of this week
Open the Notebook in Colab
https://colab.research.google.com/github/
tirthajyoti/Machine-Learning-with-Python/blob/
master/Regression/Linear_Regression_Practice.ipynbLoad and Inspect the Data
url = ("https://raw.githubusercontent.com/"
"tirthajyoti/Machine-Learning-with-Python/"
"master/Datasets/USA_Housing.csv")
df = pd.read_csv(url)
df.info(verbose=True)- ▸5000 rows, 7 columns, all
5000 non-null: no missing values - ▸Five numeric features and the target
Price - ▸
Addressis text: a linear model only multiplies numbers, so it stays out ofX
The Distribution of the Price
df['Price'].plot.hist(bins=25, figsize=(8,4))- ▸One hill around the mean of about $1.23 million
- ▸Only 97 houses below $0.5 million and 74 above $2 million
- ▸
1e6under the axis: the ticks are in millions

Correlations with the Price
df.corr(numeric_only=True)
sns.heatmap(df.corr(numeric_only=True),
annot=True, linewidths=2)
Reading the Price Row
| Feature | Correlation with Price |
|---|---|
| Avg. Area Income | 0.640 |
| Avg. Area House Age | 0.453 |
| Area Population | 0.409 |
| Avg. Area Number of Rooms | 0.336 |
| Avg. Area Number of Bedrooms | 0.171 |
Part 3
Fit the Model
LinearRegression in scikit-learn
Features, Target, and the Split
X = df[l_column[0:len_feature-2]] # 5 features
y = df[l_column[len_feature-2]] # Price
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=123)sklearn.cross_validation, which no longer exists: use sklearn.model_selection.Fit and Read the Coefficients
lm = LinearRegression()
lm.fit(X_train, y_train)
print(lm.intercept_)
cdf = pd.DataFrame(data=lm.coef_,
index=X_train.columns,
columns=['Coefficients'])| Feature | Coefficient |
|---|---|
| Income | 21.597602 |
| House age | 165201.104954 |
| Rooms | 119061.463868 |
| Bedrooms | 3212.585606 |
| Population | 15.228121 |
Reading a Coefficient
- ▸A coefficient is the change in
ŷwhen its feature grows by 1 and the others stay the same
Worked Example: One Test House by Hand
Row 2648, the first test house. Real price: $800,146.23
| Feature | x | b × x |
|---|---|---|
| Income | 63824.394539 | 1,378,453.87 |
| House age | 4.991750 | 824,642.62 |
| Rooms | 5.003836 | 595,764.04 |
| Bedrooms | 4.00 | 12,850.34 |
| Population | 40086.458749 | 610,441.44 |
Worked Example: Add the Intercept
Which Features Matter Most?
The notebook divides each coefficient by its standard error: the t-statistic
| Feature | t-statistic |
|---|---|
| Avg. Area Income | 134.68 |
| Avg. Area House Age | 95.91 |
| Area Population | 89.64 |
| Avg. Area Number of Rooms | 70.18 |
| Avg. Area Number of Bedrooms | 2.33 |
Part 4
Evaluate the Model
Predict the 1500 test houses
Actual Against Predicted
predictions = lm.predict(X_test)
plt.scatter(x=y_test, y=predictions)- ▸
predictions: 1500 prices, one per test house - ▸A perfect model puts every point on the 45 degree line
- ▸The points form a narrow band along it, for cheap and expensive houses alike

Checking the Residuals


The Error Measures
Mean absolute error (MAE): 81739.77482718184
Mean square error (MSE): 10489638335.804983
Root mean square error (RMSE): 102418.93543581179
R-squared value of predictions: 0.919Reading the Numbers
| Measure | Value | Meaning |
|---|---|---|
| MAE | $81,740 | average size of an error |
| RMSE | $102,419 | typical error, about 8% of the mean price |
| Test R² | 0.919 | about 92% of the price spread explained |
| Training R² | 0.917 | as good on new houses as on seen ones |
Before you practise
Common Mistakes
- ▸Running the old calls as they are:
cross_validation,df.corr()with a text column,distplot - ▸Reading
./Datasets/USA_Housing.csvin Colab - ▸Leaving the text column
AddressinX - ▸Ranking features by coefficient size when their units differ
- ▸Reporting only the training R²
- ▸Trusting a prediction far outside the training data, like the intercept at income 0
Your team project
Project Milestone: Explore and Clean Your Data
- 1Check
shape, the columns and the types withinfo() - 2Count missing values with
isnull().sum(), thenfillnaordropna(week 1) - 3Run
describe()and look for impossible values - 4Plot the target as a histogram, and each feature against it
- 5Draw the correlation heatmap with
numeric_only=True - 6Drop columns a model cannot use, such as free text or IDs
Part 5
Practice: Your Turn
About 30 minutes, answers follow each task
About 10 minutes
Practice 1: Fit a Line by Hand
| Area | x (income, $10,000) | y (price, $100,000) |
|---|---|---|
| A1 | 4 | 6 |
| A2 | 5 | 7 |
| A3 | 6 | 10 |
| A4 | 7 | 11 |
| A5 | 8 | 11 |
- ▸Compute
x̄,ȳ, the slopeb₁and the interceptb₀ - ▸Compute the residuals, SSE, MSE, RMSE, MAE and R²
- ▸Predict the price for an income of $65,000
Answers
Practice 1: Answer, the Line
Answers
Practice 1: Answer, the Error
| Area | ŷ | e | e² |
|---|---|---|---|
| A1 | 6.2 | -0.2 | 0.04 |
| A2 | 7.6 | -0.6 | 0.36 |
| A3 | 9.0 | 1.0 | 1.00 |
| A4 | 10.4 | 0.6 | 0.36 |
| A5 | 11.8 | -0.8 | 0.64 |
About 5 minutes
Practice 2: A Second Test House
Row 2456, real price $707,345.06, intercept -2,631,028.90. Predict, then find the residual.
| Feature | x | Coefficient b |
|---|---|---|
| Income | 67041.967661 | 21.597602 |
| House age | 6.021458 | 165201.104954 |
| Rooms | 5.346830 | 119061.463868 |
| Bedrooms | 3.39 | 3212.585606 |
| Population | 15633.099048 | 15.228121 |
About 5 minutes
Practice 3: Read the Coefficients
- ▸Two areas differ only by 2 years of house age. How far apart are the predictions?
- ▸They differ only by $5,000 of income. How far apart now?
- ▸Why does the bedrooms coefficient, 3212.59, not mean bedrooms matter more than income?
About 10 minutes
Practice 4: In Colab
- 1Run the notebook with the three fixes of today, up to the error measures
- 2Fit
lm1onX_train[['Avg. Area Income']]only: print intercept, coefficient, test R², MAE, RMSE - 3Fit a model on all features except
Avg. Area Number of Bedrooms(drop(..., axis=1)) and print its test R² - 4Compare the three R² values: what do they say about the features?
Answers
Practice 4: Answer
| Model | Test R² | Test RMSE |
|---|---|---|
| All five features | 0.919 | 102,418.94 |
| Income only | 0.417 | 275,407.48 |
| All except bedrooms | 0.920 | 102,282.54 |
Key Takeaways
- 1Linear regression predicts a number as
b0plus a weighted sum of the features - 2A residual is
y − ŷ; least squares makes the sum of their squares smallest - 3Explore and clean first: types, missing values, text columns, correlations
- 4A coefficient is the change per unit of its feature: rank features by t-statistic
- 5Judge the model on the test set with MAE, RMSE, R² and the residual plots
Open this lesson
Mahmoud Abas|Linear Regression