.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\LinearModels\RANSAC.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_LinearModels_RANSAC.py: Robust Regression with RANSAC ================================ This script demonstrates the use of RANSAC (Random Sample Consensus) regression to fit a robust model in the presence of outliers. A quadratic relationship is used to generate inlier data, while a separate set of points acts as outliers. The performance of standard linear regression is compared with RANSAC regression to highlight the latter's robustness to noisy data. - Inliers follow the function: \( y = 2x^2 + 1 + ext{noise} \) - Outliers are randomly distributed and do not follow the quadratic trend. - A polynomial feature transformation is applied to the input data to allow for quadratic regression. .. GENERATED FROM PYTHON SOURCE LINES 13-43 .. image-sg:: /auto_examples/LinearModels/images/sphx_glr_RANSAC_001.png :alt: RANSAC :srcset: /auto_examples/LinearModels/images/sphx_glr_RANSAC_001.png :class: sphx-glr-single-img .. code-block:: Python import torch import matplotlib.pyplot as plt from DLL.MachineLearning.SupervisedLearning.LinearModels import LinearRegression, RANSACRegression from DLL.Data.Preprocessing import PolynomialFeatures num_inliers = 100 num_outliers = 20 x_inliers = torch.linspace(0, 10, num_inliers) y_inliers = 2 * x_inliers ** 2 + 1 + torch.randn(num_inliers) x_outliers = torch.rand(num_outliers) * 4 + 6 y_outliers = torch.rand(num_outliers) * 20 + 10 X = PolynomialFeatures(degree=2).transform(torch.cat((x_inliers, x_outliers)).unsqueeze(-1)) y = torch.cat((y_inliers, y_outliers)) indices = torch.randperm(len(X)) X, y = X[indices], y[indices] lr = LinearRegression() lr.fit(X, y) ransac = RANSACRegression(estimator=LinearRegression()) ransac.fit(X, y, min_samples=0.1) plt.plot(x_inliers, y_inliers, ".", label="inliers") plt.plot(x_outliers, y_outliers, ".", label="outliers") plt.plot(x_inliers, lr.predict(PolynomialFeatures(degree=2).transform(x_inliers.unsqueeze(-1))), label="Linear regression") plt.plot(x_inliers, ransac.predict(PolynomialFeatures(degree=2).transform(x_inliers.unsqueeze(-1))), label="RANSAC regression") plt.legend() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.560 seconds) .. _sphx_glr_download_auto_examples_LinearModels_RANSAC.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: RANSAC.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: RANSAC.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: RANSAC.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_