Module Expr Str ImportFrom alias identifier Import alias identifier ImportFrom alias alias alias alias ImportFrom alias ImportFrom alias ImportFrom alias Expr Call AttributeLoad AttributeLoad NameLoad attr attr Num Assign NameStore Call AttributeLoad AttributeLoad NameLoad attr attr keyword Num Assign NameStore Call AttributeLoad NameLoad attr NameLoad Assign NameStore SubscriptLoad NameLoad ExtSlice Slice Index AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad AttributeLoad NameLoad attr attr keyword Num Assign NameStore Call AttributeLoad NameLoad attr NameLoad Assign NameStore SubscriptLoad NameLoad ExtSlice Slice Index AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr Assign SubscriptStore NameLoad Slice Num Num Assign NameStore Call AttributeLoad NameLoad attr Assign SubscriptStore NameLoad Slice Num Num Assign NameStore Call AttributeLoad NameLoad attr Assign SubscriptStore NameLoad Slice Num Num Assign NameStore Call AttributeLoad NameLoad attr Assign SubscriptStore NameLoad Slice Num Num Assign NameStore ListLoad TupleLoad Str Call NameLoad TupleLoad Str Call NameLoad keyword Num TupleLoad Str Call NameLoad keyword Num TupleLoad Str Call NameLoad Assign NameStore Dict Str Str Str Str Str Str Str Str Assign NameStore Dict Str Str Str Str Str Str Str Str Assign NameStore Num Assign NameStore Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr For TupleStore NameStore NameStore NameStore ListLoad TupleLoad Str NameLoad NameLoad TupleLoad Str NameLoad NameLoad TupleLoad Str NameLoad NameLoad TupleLoad Str NameLoad NameLoad TupleLoad Str NameLoad NameLoad body Expr Call AttributeLoad NameLoad attr keyword TupleLoad Num Num Expr Call AttributeLoad NameLoad attr SubscriptLoad NameLoad ExtSlice Slice Index Num NameLoad Str For TupleStore NameStore NameStore NameLoad body Assign NameStore Call NameLoad Call NameLoad Num NameLoad Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Assign NameStore Call NameLoad Call AttributeLoad NameLoad attr NameLoad NameLoad Assign NameStore Call AttributeLoad NameLoad attr SubscriptLoad NameLoad ExtSlice Slice Index AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr NameLoad NameLoad keyword SubscriptLoad NameLoad Index NameLoad keyword SubscriptLoad NameLoad Index NameLoad keyword NameLoad keyword BinOpMod Str TupleLoad NameLoad NameLoad Assign NameStore Str Assign NameStore Call AttributeLoad NameLoad attr keyword Str keyword NameLoad keyword NameLoad keyword Call NameLoad keyword Str Expr Call AttributeLoad NameLoad attr Num Num Expr Call AttributeLoad NameLoad attr Num Num Expr Call AttributeLoad NameLoad attr NameLoad Expr Call AttributeLoad NameLoad attr
N/A N/A 
Robust linear estimator fitting
===============================

Here a sine function is fit with a polynomial of order 3, for values
close to zero.

Robust fitting is demoed in different situations:

- No measurement errors, only modelling errors (fitting a sine with a
  polynomial)

- Measurement errors in X

- Measurement errors in y

The median absolute deviation to non corrupt new data is used to judge
the quality of the prediction.

What we can see that:

- RANSAC is good for strong outliers in the y direction

- TheilSen is good for small outliers, both in direction X and y, but has
  a break point above which it performs worse than OLS.

- The scores of HuberRegressor may not be compared directly to both TheilSen
  and RANSAC because it does not attempt to completely filter the outliers
  but lessen their effect.

 matplotlib pyplot plt N/A numpy np sklearn.linear_model LinearRegression TheilSenRegressor RANSACRegressor HuberRegressor sklearn.metrics mean_squared_error sklearn.preprocessing PolynomialFeatures sklearn.pipeline make_pipeline N/A N/A N/A N/A np random seed 42 N/A X N/A N/A N/A np random normal size 400 N/A y N/A N/A np sin X N/A X N/A X N/A N/A N/A N/A np newaxis N/A X_test N/A N/A N/A np random normal size 200 N/A y_test N/A N/A np sin X_test N/A X_test N/A X_test N/A N/A N/A N/A np newaxis N/A y_errors N/A N/A y copy N/A N/A y_errors N/A 3 3 N/A X_errors N/A N/A X copy N/A N/A X_errors N/A 3 3 N/A y_errors_large N/A N/A y copy N/A N/A y_errors_large N/A 3 10 N/A X_errors_large N/A N/A X copy N/A N/A X_errors_large N/A 3 10 N/A estimators N/A N/A OLS N/A LinearRegression N/A Theil-Sen N/A TheilSenRegressor random_state 42 N/A RANSAC N/A RANSACRegressor random_state 42 N/A HuberRegressor N/A HuberRegressor N/A colors N/A OLS Theil-Sen RANSAC HuberRegressor turquoise gold lightgreen black N/A linestyle N/A OLS Theil-Sen RANSAC HuberRegressor - -. -- -- N/A lw 3 N/A x_plot N/A N/A np linspace N/A N/A X min N/A N/A X max N/A N/A title this_X this_y N/A N/A Modeling Errors Only X y N/A Corrupt X, Small Deviants X_errors y N/A Corrupt y, Small Deviants X y_errors N/A Corrupt X, Large Deviants X_errors_large y N/A Corrupt y, Large Deviants X y_errors_large N/A N/A N/A N/A plt figure figsize N/A 5 4 N/A N/A N/A plt plot N/A this_X N/A N/A N/A 0 this_y b+ N/A N/A name estimator estimators N/A N/A model N/A make_pipeline N/A PolynomialFeatures 3 estimator N/A N/A N/A model fit this_X this_y N/A mse N/A mean_squared_error N/A N/A model predict X_test y_test N/A y_plot N/A N/A model predict N/A x_plot N/A N/A N/A N/A np newaxis N/A N/A N/A plt plot x_plot y_plot color N/A colors N/A name linestyle N/A linestyle N/A name linewidth lw label N/A %s: error = %.3f N/A name mse N/A legend_title Error of Mean
Absolute Deviation
to Non-corrupt Data N/A legend N/A N/A plt legend loc upper right frameon False title legend_title prop N/A dict size x-small N/A N/A N/A plt xlim -4 10.2 N/A N/A N/A plt ylim -2 10.2 N/A N/A N/A plt title title N/A N/A N/A plt show