2017-12-14 06:31:03 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
======================================================
|
|
|
|
|
Effect of transforming the targets in regression model
|
|
|
|
|
======================================================
|
|
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
In this example, we give an overview of
|
|
|
|
|
:class:`~sklearn.compose.TransformedTargetRegressor`. We use two examples
|
|
|
|
|
to illustrate the benefit of transforming the targets before learning a linear
|
2017-12-14 06:31:03 +08:00
|
|
|
regression model. The first example uses synthetic data while the second
|
2020-05-14 17:55:48 +08:00
|
|
|
example is based on the Ames housing data set.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2017-12-14 06:31:03 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Guillaume Lemaitre <guillaume.lemaitre@inria.fr>
|
|
|
|
|
# License: BSD 3 clause
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
from sklearn.datasets import make_regression
|
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
|
from sklearn.linear_model import RidgeCV
|
2018-03-16 21:27:03 +08:00
|
|
|
from sklearn.compose import TransformedTargetRegressor
|
2017-12-14 06:31:03 +08:00
|
|
|
from sklearn.metrics import median_absolute_error, r2_score
|
|
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2020-05-14 17:55:48 +08:00
|
|
|
# Synthetic example
|
|
|
|
|
##############################################################################
|
2018-12-06 09:05:41 +08:00
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2020-05-14 17:55:48 +08:00
|
|
|
# A synthetic random regression dataset is generated. The targets ``y`` are
|
|
|
|
|
# modified by:
|
|
|
|
|
#
|
|
|
|
|
# 1. translating all targets such that all entries are
|
|
|
|
|
# non-negative (by adding the absolute value of the lowest ``y``) and
|
|
|
|
|
# 2. applying an exponential function to obtain non-linear
|
|
|
|
|
# targets which cannot be fitted using a simple linear model.
|
2017-12-14 06:31:03 +08:00
|
|
|
#
|
2018-01-11 06:13:03 +08:00
|
|
|
# Therefore, a logarithmic (`np.log1p`) and an exponential function
|
|
|
|
|
# (`np.expm1`) will be used to transform the targets before training a linear
|
|
|
|
|
# regression model and using it for prediction.
|
2017-12-14 06:31:03 +08:00
|
|
|
|
|
|
|
|
X, y = make_regression(n_samples=10000, noise=100, random_state=0)
|
2020-05-14 17:55:48 +08:00
|
|
|
y = np.expm1((y + abs(y.min())) / 200)
|
2018-01-11 06:13:03 +08:00
|
|
|
y_trans = np.log1p(y)
|
2017-12-14 06:31:03 +08:00
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2020-05-14 17:55:48 +08:00
|
|
|
# Below we plot the probability density functions of the target
|
2017-12-14 06:31:03 +08:00
|
|
|
# before and after applying the logarithmic functions.
|
|
|
|
|
|
|
|
|
|
f, (ax0, ax1) = plt.subplots(1, 2)
|
|
|
|
|
|
2022-03-02 18:32:06 +08:00
|
|
|
ax0.hist(y, bins=100, density=True)
|
2017-12-14 06:31:03 +08:00
|
|
|
ax0.set_xlim([0, 2000])
|
|
|
|
|
ax0.set_ylabel("Probability")
|
|
|
|
|
ax0.set_xlabel("Target")
|
|
|
|
|
ax0.set_title("Target distribution")
|
|
|
|
|
|
2022-03-02 18:32:06 +08:00
|
|
|
ax1.hist(y_trans, bins=100, density=True)
|
2017-12-14 06:31:03 +08:00
|
|
|
ax1.set_ylabel("Probability")
|
|
|
|
|
ax1.set_xlabel("Target")
|
|
|
|
|
ax1.set_title("Transformed target distribution")
|
|
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
f.suptitle("Synthetic data", y=0.06, x=0.53)
|
2017-12-14 06:31:03 +08:00
|
|
|
f.tight_layout(rect=[0.05, 0.05, 0.95, 0.95])
|
|
|
|
|
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
|
|
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2017-12-14 06:31:03 +08:00
|
|
|
# At first, a linear model will be applied on the original targets. Due to the
|
2020-05-14 17:55:48 +08:00
|
|
|
# non-linearity, the model trained will not be precise during
|
2017-12-14 06:31:03 +08:00
|
|
|
# prediction. Subsequently, a logarithmic function is used to linearize the
|
|
|
|
|
# targets, allowing better prediction even with a similar linear model as
|
|
|
|
|
# reported by the median absolute error (MAE).
|
|
|
|
|
|
|
|
|
|
f, (ax0, ax1) = plt.subplots(1, 2, sharey=True)
|
2020-05-14 17:55:48 +08:00
|
|
|
# Use linear model
|
2017-12-14 06:31:03 +08:00
|
|
|
regr = RidgeCV()
|
|
|
|
|
regr.fit(X_train, y_train)
|
|
|
|
|
y_pred = regr.predict(X_test)
|
2020-05-14 17:55:48 +08:00
|
|
|
# Plot results
|
2017-12-14 06:31:03 +08:00
|
|
|
ax0.scatter(y_test, y_pred)
|
|
|
|
|
ax0.plot([0, 2000], [0, 2000], "--k")
|
|
|
|
|
ax0.set_ylabel("Target predicted")
|
|
|
|
|
ax0.set_xlabel("True Target")
|
|
|
|
|
ax0.set_title("Ridge regression \n without target transformation")
|
|
|
|
|
ax0.text(
|
|
|
|
|
100,
|
|
|
|
|
1750,
|
|
|
|
|
r"$R^2$=%.2f, MAE=%.2f"
|
|
|
|
|
% (r2_score(y_test, y_pred), median_absolute_error(y_test, y_pred)),
|
|
|
|
|
)
|
|
|
|
|
ax0.set_xlim([0, 2000])
|
|
|
|
|
ax0.set_ylim([0, 2000])
|
2020-05-14 17:55:48 +08:00
|
|
|
# Transform targets and use same linear model
|
2017-12-14 06:31:03 +08:00
|
|
|
regr_trans = TransformedTargetRegressor(
|
2018-01-11 06:13:03 +08:00
|
|
|
regressor=RidgeCV(), func=np.log1p, inverse_func=np.expm1
|
|
|
|
|
)
|
2017-12-14 06:31:03 +08:00
|
|
|
regr_trans.fit(X_train, y_train)
|
|
|
|
|
y_pred = regr_trans.predict(X_test)
|
|
|
|
|
|
|
|
|
|
ax1.scatter(y_test, y_pred)
|
|
|
|
|
ax1.plot([0, 2000], [0, 2000], "--k")
|
|
|
|
|
ax1.set_ylabel("Target predicted")
|
|
|
|
|
ax1.set_xlabel("True Target")
|
|
|
|
|
ax1.set_title("Ridge regression \n with target transformation")
|
|
|
|
|
ax1.text(
|
|
|
|
|
100,
|
|
|
|
|
1750,
|
|
|
|
|
r"$R^2$=%.2f, MAE=%.2f"
|
|
|
|
|
% (r2_score(y_test, y_pred), median_absolute_error(y_test, y_pred)),
|
|
|
|
|
)
|
|
|
|
|
ax1.set_xlim([0, 2000])
|
|
|
|
|
ax1.set_ylim([0, 2000])
|
|
|
|
|
|
|
|
|
|
f.suptitle("Synthetic data", y=0.035)
|
|
|
|
|
f.tight_layout(rect=[0.05, 0.05, 0.95, 0.95])
|
|
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2017-12-14 06:31:03 +08:00
|
|
|
# Real-world data set
|
|
|
|
|
###############################################################################
|
2020-06-09 11:23:14 +08:00
|
|
|
#
|
2020-05-14 17:55:48 +08:00
|
|
|
# In a similar manner, the Ames housing data set is used to show the impact
|
2017-12-14 06:31:03 +08:00
|
|
|
# of transforming the targets before learning a model. In this example, the
|
2020-05-14 17:55:48 +08:00
|
|
|
# target to be predicted is the selling price of each house.
|
2017-12-14 06:31:03 +08:00
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
from sklearn.datasets import fetch_openml
|
2017-12-14 06:31:03 +08:00
|
|
|
from sklearn.preprocessing import QuantileTransformer, quantile_transform
|
|
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
ames = fetch_openml(name="house_prices", as_frame=True)
|
|
|
|
|
# Keep only numeric columns
|
|
|
|
|
X = ames.data.select_dtypes(np.number)
|
|
|
|
|
# Remove columns with NaN or Inf values
|
|
|
|
|
X = X.drop(columns=["LotFrontage", "GarageYrBlt", "MasVnrArea"])
|
|
|
|
|
y = ames.target
|
|
|
|
|
y_trans = quantile_transform(
|
2019-06-24 17:49:14 +08:00
|
|
|
y.to_frame(), n_quantiles=900, output_distribution="normal", copy=True
|
|
|
|
|
).squeeze()
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2020-05-14 17:55:48 +08:00
|
|
|
# A :class:`~sklearn.preprocessing.QuantileTransformer` is used to normalize
|
|
|
|
|
# the target distribution before applying a
|
|
|
|
|
# :class:`~sklearn.linear_model.RidgeCV` model.
|
2017-12-14 06:31:03 +08:00
|
|
|
|
|
|
|
|
f, (ax0, ax1) = plt.subplots(1, 2)
|
|
|
|
|
|
2022-03-02 18:32:06 +08:00
|
|
|
ax0.hist(y, bins=100, density=True)
|
2017-12-14 06:31:03 +08:00
|
|
|
ax0.set_ylabel("Probability")
|
|
|
|
|
ax0.set_xlabel("Target")
|
2020-05-14 17:55:48 +08:00
|
|
|
ax0.text(s="Target distribution", x=1.2e5, y=9.8e-6, fontsize=12)
|
|
|
|
|
ax0.ticklabel_format(axis="both", style="sci", scilimits=(0, 0))
|
2017-12-14 06:31:03 +08:00
|
|
|
|
2022-03-02 18:32:06 +08:00
|
|
|
ax1.hist(y_trans, bins=100, density=True)
|
2017-12-14 06:31:03 +08:00
|
|
|
ax1.set_ylabel("Probability")
|
|
|
|
|
ax1.set_xlabel("Target")
|
2020-05-14 17:55:48 +08:00
|
|
|
ax1.text(s="Transformed target distribution", x=-6.8, y=0.479, fontsize=12)
|
2017-12-14 06:31:03 +08:00
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
f.suptitle("Ames housing data: selling price", y=0.04)
|
2017-12-14 06:31:03 +08:00
|
|
|
f.tight_layout(rect=[0.05, 0.05, 0.95, 0.95])
|
|
|
|
|
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
|
|
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2017-12-14 06:31:03 +08:00
|
|
|
# The effect of the transformer is weaker than on the synthetic data. However,
|
2020-05-14 17:55:48 +08:00
|
|
|
# the transformation results in an increase in :math:`R^2` and large decrease
|
|
|
|
|
# of the MAE. The residual plot (predicted target - true target vs predicted
|
|
|
|
|
# target) without target transformation takes on a curved, 'reverse smile'
|
|
|
|
|
# shape due to residual values that vary depending on the value of predicted
|
|
|
|
|
# target. With target transformation, the shape is more linear indicating
|
|
|
|
|
# better model fit.
|
2017-12-14 06:31:03 +08:00
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
f, (ax0, ax1) = plt.subplots(2, 2, sharey="row", figsize=(6.5, 8))
|
2017-12-14 06:31:03 +08:00
|
|
|
|
|
|
|
|
regr = RidgeCV()
|
|
|
|
|
regr.fit(X_train, y_train)
|
|
|
|
|
y_pred = regr.predict(X_test)
|
|
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
ax0[0].scatter(y_pred, y_test, s=8)
|
|
|
|
|
ax0[0].plot([0, 7e5], [0, 7e5], "--k")
|
|
|
|
|
ax0[0].set_ylabel("True target")
|
|
|
|
|
ax0[0].set_xlabel("Predicted target")
|
|
|
|
|
ax0[0].text(
|
|
|
|
|
s="Ridge regression \n without target transformation",
|
|
|
|
|
x=-5e4,
|
|
|
|
|
y=8e5,
|
|
|
|
|
fontsize=12,
|
|
|
|
|
multialignment="center",
|
|
|
|
|
)
|
|
|
|
|
ax0[0].text(
|
|
|
|
|
3e4,
|
|
|
|
|
64e4,
|
|
|
|
|
r"$R^2$=%.2f, MAE=%.2f"
|
2017-12-14 06:31:03 +08:00
|
|
|
% (r2_score(y_test, y_pred), median_absolute_error(y_test, y_pred)),
|
|
|
|
|
)
|
2020-05-14 17:55:48 +08:00
|
|
|
ax0[0].set_xlim([0, 7e5])
|
|
|
|
|
ax0[0].set_ylim([0, 7e5])
|
|
|
|
|
ax0[0].ticklabel_format(axis="both", style="sci", scilimits=(0, 0))
|
|
|
|
|
|
|
|
|
|
ax1[0].scatter(y_pred, (y_pred - y_test), s=8)
|
|
|
|
|
ax1[0].set_ylabel("Residual")
|
|
|
|
|
ax1[0].set_xlabel("Predicted target")
|
|
|
|
|
ax1[0].ticklabel_format(axis="both", style="sci", scilimits=(0, 0))
|
2017-12-14 06:31:03 +08:00
|
|
|
|
|
|
|
|
regr_trans = TransformedTargetRegressor(
|
|
|
|
|
regressor=RidgeCV(),
|
2020-05-14 17:55:48 +08:00
|
|
|
transformer=QuantileTransformer(n_quantiles=900, output_distribution="normal"),
|
2019-06-24 17:49:14 +08:00
|
|
|
)
|
2017-12-14 06:31:03 +08:00
|
|
|
regr_trans.fit(X_train, y_train)
|
|
|
|
|
y_pred = regr_trans.predict(X_test)
|
|
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
ax0[1].scatter(y_pred, y_test, s=8)
|
|
|
|
|
ax0[1].plot([0, 7e5], [0, 7e5], "--k")
|
|
|
|
|
ax0[1].set_ylabel("True target")
|
|
|
|
|
ax0[1].set_xlabel("Predicted target")
|
|
|
|
|
ax0[1].text(
|
|
|
|
|
s="Ridge regression \n with target transformation",
|
|
|
|
|
x=-5e4,
|
|
|
|
|
y=8e5,
|
|
|
|
|
fontsize=12,
|
|
|
|
|
multialignment="center",
|
|
|
|
|
)
|
|
|
|
|
ax0[1].text(
|
|
|
|
|
3e4,
|
|
|
|
|
64e4,
|
|
|
|
|
r"$R^2$=%.2f, MAE=%.2f"
|
2017-12-14 06:31:03 +08:00
|
|
|
% (r2_score(y_test, y_pred), median_absolute_error(y_test, y_pred)),
|
|
|
|
|
)
|
2020-05-14 17:55:48 +08:00
|
|
|
ax0[1].set_xlim([0, 7e5])
|
|
|
|
|
ax0[1].set_ylim([0, 7e5])
|
|
|
|
|
ax0[1].ticklabel_format(axis="both", style="sci", scilimits=(0, 0))
|
2017-12-14 06:31:03 +08:00
|
|
|
|
2020-05-14 17:55:48 +08:00
|
|
|
ax1[1].scatter(y_pred, (y_pred - y_test), s=8)
|
|
|
|
|
ax1[1].set_ylabel("Residual")
|
|
|
|
|
ax1[1].set_xlabel("Predicted target")
|
|
|
|
|
ax1[1].ticklabel_format(axis="both", style="sci", scilimits=(0, 0))
|
|
|
|
|
|
|
|
|
|
f.suptitle("Ames housing data: selling price", y=0.035)
|
2017-12-14 06:31:03 +08:00
|
|
|
|
|
|
|
|
plt.show()
|