Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Predicting Continuous Target Variables with Regression Analysis

Open in Colab Open in Kaggle

Predicting Continuous Target Variables with Regression Analysis

Package version checks

Add folder to path in order to load from the check_packages.py script:

Check recommended package versions:

[OK] numpy 2.0.2
[OK] mlxtend 0.23.4
[OK] matplotlib 3.10.0
[OK] sklearn 1.6.1
[OK] pandas 2.2.2

Chapter 09 - Predicting Continuous Target Variables with Regression Analysis



Overview



Introducing linear regression

Simple linear regression

<IPython.core.display.Image object>

Multiple linear regression

<IPython.core.display.Image object>


Exploring the Ames Housing dataset

Loading the Ames Housing dataset into a data frame

  • 'Overall Qual': Rates the overall material and finish of the house

     10	Very Excellent
     9	Excellent
     8	Very Good
     7	Good
     6	Above Average
     5	Average
     4	Below Average
     3	Fair
     2	Poor
     1	Very Poor
  • 'Overall Cond': Rates the overall condition of the house

     10	Very Excellent
     9	Excellent
     8	Very Good
     7	Good
     6	Above Average
     5	Average
     4	Below Average
     3	Fair
     2	Poor
     1	Very Poor
  • 'Gr Liv Area': Above grade (ground) living area square feet

  • 'Central Air': Central air conditioning

     N	No
     Y	Yes
  • 'Total Bsmt SF': Total square feet of basement area

  • 'SalePrice': Sale price $$

Loading...
(2930, 6)
Loading...
Loading...


Visualizing the important characteristics of a dataset

<Figure size 1200x1000 with 36 Axes>
<Figure size 640x480 with 2 Axes>


Implementing an ordinary least squares linear regression model

...

Solving regression for regression parameters with gradient descent

<__main__.LinearRegressionGD at 0x79aae40a8830>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
Sale price: $292507.07
Slope: 0.707
Intercept: -0.000


Estimating the coefficient of a regression model via scikit-learn

Slope: 111.666
Intercept: 13342.979
<Figure size 640x480 with 1 Axes>

Normal Equations alternative:

Slope: 111.666
Intercept: 13342.979


Fitting a robust regression model using RANSAC

<Figure size 640x480 with 1 Axes>
Slope: 106.348
Intercept: 20190.093
np.float64(37000.0)
<Figure size 640x480 with 1 Axes>
Slope: 105.631
Intercept: 18314.587


Evaluating the performance of linear regression models

<Figure size 700x300 with 2 Axes>
MSE train: 1497216245.85
MSE test: 1516565821.00
MAE train: 25983.03
MAE test: 24921.29
R^2 train: 0.77
R^2 test: 0.75


Using regularized methods for regression

[26251.38276394   804.70816337    41.94651964 11364.80761309
    55.67855548]
MSE train: 1497216262.014, test: 1516576825.348
R^2 train: 0.769, 0.752

Ridge regression:

LASSO regression:

Elastic Net regression:



Turning a linear regression model into a curve - polynomial regression

<Figure size 640x480 with 1 Axes>
Training MSE linear: 569.780, quadratic: 61.330
Training R^2 linear: 0.832, quadratic: 0.982


Modeling nonlinear relationships in the Ames Housing dataset

<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>


Dealing with nonlinear relationships using random forests

...

Decision tree regression

<Figure size 640x480 with 1 Axes>
0.5144569334885711


Random forest regression

MAE train: 8305.18
MAE test: 20821.77
R^2 train: 0.98
R^2 test: 0.85
<Figure size 700x300 with 2 Axes>


Summary

...


Readers may ignore the next cell.

Skipping: convert_notebook_to_script.py not present in this environment.