2014-01-18 05:07:12 +08:00
|
|
|
"""
|
|
|
|
|
====================
|
|
|
|
|
Theil-Sen Regression
|
|
|
|
|
====================
|
|
|
|
|
|
|
|
|
|
Computes a Theil-Sen Regression on a synthetic dataset.
|
|
|
|
|
|
|
|
|
|
See :ref:`theil_sen_regression` for more information on the regressor.
|
|
|
|
|
|
|
|
|
|
Compared to the OLS (ordinary least squares) estimator, the Theil-Sen
|
|
|
|
|
estimator is robust against outliers. It has a breakdown point of about 29.3%
|
|
|
|
|
in case of a simple linear regression which means that it can tolerate
|
2014-03-05 02:59:49 +08:00
|
|
|
arbitrary corrupted data (outliers) of up to 29.3% in the two-dimensional
|
|
|
|
|
case.
|
2014-01-18 05:07:12 +08:00
|
|
|
|
|
|
|
|
The estimation of the model is done by calculating the slopes and intercepts
|
2014-03-05 02:59:49 +08:00
|
|
|
of a subpopulation of all possible combinations of p subsample points. If an
|
2014-03-23 20:17:53 +08:00
|
|
|
intercept is fitted, p must be greater than or equal to n_features + 1. The
|
|
|
|
|
final slope and intercept is then defined as the spatial median of these
|
|
|
|
|
slopes and intercepts.
|
2014-03-22 23:18:45 +08:00
|
|
|
|
|
|
|
|
In certain cases Theil-Sen performs better than :ref:`RANSAC
|
2014-03-25 01:55:58 +08:00
|
|
|
<ransac_regression>` which is also a robust method. This is illustrated in the
|
|
|
|
|
second example below where outliers with respect to the x-axis perturb RANSAC.
|
|
|
|
|
Tuning the ``residual_threshold`` parameter of RANSAC remedies this but in
|
|
|
|
|
general a priori knowledge about the data and the nature of the outliers is
|
|
|
|
|
needed.
|
|
|
|
|
Due to the computational complexity of Theil-Sen it is recommended to use it
|
|
|
|
|
only for small problems in terms of number of samples and features. For larger
|
|
|
|
|
problems the ``max_subpopulation`` parameter restricts the magnitude of all
|
|
|
|
|
possible combinations of p subsample points to a randomly chosen subset and
|
|
|
|
|
therefore also limits the runtime. Therefore, Theil-Sen is applicable to larger
|
|
|
|
|
problems with the drawback of losing some of its mathematical properties since
|
|
|
|
|
it then works on a random subset.
|
2014-01-18 05:07:12 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Florian Wilhelm -- <florian.wilhelm@gmail.com>
|
|
|
|
|
# License: BSD 3 clause
|
|
|
|
|
|
2014-03-24 07:55:51 +08:00
|
|
|
import time
|
2014-01-18 05:07:12 +08:00
|
|
|
import numpy as np
|
2014-09-25 01:52:54 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2014-09-25 03:46:39 +08:00
|
|
|
from sklearn.linear_model import LinearRegression, TheilSenRegressor
|
|
|
|
|
from sklearn.linear_model import RANSACRegressor
|
2014-01-18 05:07:12 +08:00
|
|
|
|
|
|
|
|
print(__doc__)
|
|
|
|
|
|
2014-03-24 07:55:51 +08:00
|
|
|
estimators = [('OLS', LinearRegression()),
|
2014-10-17 20:54:49 +08:00
|
|
|
('Theil-Sen', TheilSenRegressor(random_state=42)),
|
2014-03-24 07:55:51 +08:00
|
|
|
('RANSAC', RANSACRegressor(random_state=42)), ]
|
2015-10-22 20:12:06 +08:00
|
|
|
colors = {'OLS': 'turquoise', 'Theil-Sen': 'gold', 'RANSAC': 'lightgreen'}
|
|
|
|
|
lw = 2
|
2014-03-24 07:55:51 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2014-03-24 07:55:51 +08:00
|
|
|
# Outliers only in the y direction
|
2014-03-22 23:18:45 +08:00
|
|
|
|
2014-01-18 05:07:12 +08:00
|
|
|
np.random.seed(0)
|
2014-03-24 07:55:51 +08:00
|
|
|
n_samples = 200
|
2014-01-18 05:07:12 +08:00
|
|
|
# Linear model y = 3*x + N(2, 0.1**2)
|
|
|
|
|
x = np.random.randn(n_samples)
|
2014-03-24 07:55:51 +08:00
|
|
|
w = 3.
|
|
|
|
|
c = 2.
|
2014-01-18 05:07:12 +08:00
|
|
|
noise = 0.1 * np.random.randn(n_samples)
|
|
|
|
|
y = w * x + c + noise
|
2014-03-24 07:55:51 +08:00
|
|
|
# 10% outliers
|
|
|
|
|
y[-20:] += -20 * x[-20:]
|
2014-01-18 05:07:12 +08:00
|
|
|
X = x[:, np.newaxis]
|
|
|
|
|
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.scatter(x, y, color='indigo', marker='x', s=40)
|
2014-01-18 05:07:12 +08:00
|
|
|
line_x = np.array([-3, 3])
|
2014-03-24 07:55:51 +08:00
|
|
|
for name, estimator in estimators:
|
|
|
|
|
t0 = time.time()
|
|
|
|
|
estimator.fit(X, y)
|
|
|
|
|
elapsed_time = time.time() - t0
|
|
|
|
|
y_pred = estimator.predict(line_x.reshape(2, 1))
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.plot(line_x, y_pred, color=colors[name], linewidth=lw,
|
2014-09-25 03:46:39 +08:00
|
|
|
label='%s (fit time: %.2fs)' % (name, elapsed_time))
|
2014-03-24 07:55:51 +08:00
|
|
|
|
2014-09-25 01:52:54 +08:00
|
|
|
plt.axis('tight')
|
|
|
|
|
plt.legend(loc='upper left')
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.title("Corrupt y")
|
2014-03-23 20:17:53 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2014-03-24 07:55:51 +08:00
|
|
|
# Outliers in the X direction
|
|
|
|
|
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
# Linear model y = 3*x + N(2, 0.1**2)
|
|
|
|
|
x = np.random.randn(n_samples)
|
|
|
|
|
noise = 0.1 * np.random.randn(n_samples)
|
|
|
|
|
y = 3 * x + 2 + noise
|
|
|
|
|
# 10% outliers
|
|
|
|
|
x[-20:] = 9.9
|
2014-09-08 02:11:56 +08:00
|
|
|
y[-20:] += 22
|
2014-03-22 23:18:45 +08:00
|
|
|
X = x[:, np.newaxis]
|
2014-03-24 07:55:51 +08:00
|
|
|
|
2014-09-25 01:52:54 +08:00
|
|
|
plt.figure()
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.scatter(x, y, color='indigo', marker='x', s=40)
|
2014-03-24 07:55:51 +08:00
|
|
|
|
|
|
|
|
line_x = np.array([-3, 10])
|
|
|
|
|
for name, estimator in estimators:
|
|
|
|
|
t0 = time.time()
|
|
|
|
|
estimator.fit(X, y)
|
|
|
|
|
elapsed_time = time.time() - t0
|
|
|
|
|
y_pred = estimator.predict(line_x.reshape(2, 1))
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.plot(line_x, y_pred, color=colors[name], linewidth=lw,
|
2014-09-25 03:46:39 +08:00
|
|
|
label='%s (fit time: %.2fs)' % (name, elapsed_time))
|
2014-03-24 07:55:51 +08:00
|
|
|
|
2014-09-25 01:52:54 +08:00
|
|
|
plt.axis('tight')
|
|
|
|
|
plt.legend(loc='upper left')
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.title("Corrupt x")
|
2014-09-25 01:52:54 +08:00
|
|
|
plt.show()
|