鲁棒线性估计器拟合对比试验

这里,用3阶多项式对正弦函数进行拟合,正弦函数数值接近于零。

鲁棒的拟合在多种不同情形下做了展示:

  • 没有测量误差, 只有模型误差(即,使用多项式模型拟合正弦函数模型带来的误差)
  • 在 X 中有测量误差
  • 在 y 中有测量误差

无污染新数据的中位绝对偏差(median absolute deviation)用来判断预测的质量。

我们可以看到的是:

  • RANSAC 在y方向上有强离群点的时候表现不错
  • TheilSen 对弱离群点的效果不错, 不仅在 X 方向还有 y 方向, 但是它有一个崩溃点 在这个崩溃点之上,它的表现弱于OLS。
  • HuberRegressor 的得分可能无法与 TheilSen 和 RANSAC 直接对比,因为它不尝试 完全过滤掉离群点而是去削弱它们对模型拟合的影响。
  • ../../images/sphx_glr_plot_robust_fit_001.png
  • ../../images/sphx_glr_plot_robust_fit_002.png
  • ../../images/sphx_glr_plot_robust_fit_003.png
  • ../../images/sphx_glr_plot_robust_fit_004.png
  • ../../images/sphx_glr_plot_robust_fit_005.png
from matplotlib import pyplot as plt
import numpy as np

from sklearn.linear_model import (
    LinearRegression, TheilSenRegressor, RANSACRegressor, HuberRegressor)
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline

np.random.seed(42)

X = np.random.normal(size=400)
y = np.sin(X)
# 确保 X 是一个 2D 数组
X = X[:, np.newaxis]
# 或 X= X.reshape(-1,1)

X_test = np.random.normal(size=200)
y_test = np.sin(X_test)
X_test = X_test[:, np.newaxis]

y_errors = y.copy()
y_errors[::3] = 3

X_errors = X.copy()
X_errors[::3] = 3

y_errors_large = y.copy()
y_errors_large[::3] = 10

X_errors_large = X.copy()
X_errors_large[::3] = 10

estimators = [('OLS', LinearRegression()),
              ('Theil-Sen', TheilSenRegressor(random_state=42)),
              ('RANSAC', RANSACRegressor(random_state=42)),
              ('HuberRegressor', HuberRegressor())]
colors = {'OLS': 'turquoise', 'Theil-Sen': 'gold', 'RANSAC': 'lightgreen', 'HuberRegressor': 'black'}
linestyle = {'OLS': '-', 'Theil-Sen': '-.', 'RANSAC': '--', 'HuberRegressor': '--'}
lw = 3

x_plot = np.linspace(X.min(), X.max())
for title, this_X, this_y in [
        ('Modeling Errors Only', X, y),
        ('Corrupt X, Small Deviants', X_errors, y),
        ('Corrupt y, Small Deviants', X, y_errors),
        ('Corrupt X, Large Deviants', X_errors_large, y),
        ('Corrupt y, Large Deviants', X, y_errors_large)]:
    plt.figure(figsize=(5, 4))
    plt.plot(this_X[:, 0], this_y, 'b+')

    for name, estimator in estimators:
        model = make_pipeline(PolynomialFeatures(3), estimator)
        model.fit(this_X, this_y)
        mse = mean_squared_error(model.predict(X_test), y_test)
        y_plot = model.predict(x_plot[:, np.newaxis])
        plt.plot(x_plot, y_plot, color=colors[name], linestyle=linestyle[name],
                 linewidth=lw, label='%s: error = %.3f' % (name, mse))

    legend_title = 'Error of Mean\nAbsolute Deviation\nto Non-corrupt Data'
    legend = plt.legend(loc='upper right', frameon=False, title=legend_title,
                        prop=dict(size='x-small'))
    plt.xlim(-4, 10.2)
    plt.ylim(-2, 10.2)
    plt.title(title)
plt.show()

Total running time of the script: ( 0 minutes 3.492 seconds)

Gallery generated by Sphinx-Gallery