.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\LinearModels\PolynomialFeatures.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_PolynomialFeatures.py: Polynomial Surface Regression with Total Least Squares ====================================================== This script demonstrates polynomial regression on a 2D dataset using total least squares (TLS). It generates a grid of input points, applies polynomial feature expansion, and fits a linear regression model. Predictions are visualized in 3D, comparing model output (blue) against actual values (red). .. GENERATED FROM PYTHON SOURCE LINES 9-33 .. image-sg:: /auto_examples/LinearModels/images/sphx_glr_PolynomialFeatures_001.png :alt: PolynomialFeatures :srcset: /auto_examples/LinearModels/images/sphx_glr_PolynomialFeatures_001.png :class: sphx-glr-single-img .. code-block:: Python import torch import matplotlib.pyplot as plt from DLL.MachineLearning.SupervisedLearning.LinearModels import LinearRegression from DLL.Data.Preprocessing import PolynomialFeatures, data_split model = LinearRegression() n = 30 X, Y = torch.meshgrid(torch.linspace(-1, 1, n, dtype=torch.float32), torch.linspace(-1, 1, n, dtype=torch.float32), indexing="xy") x = torch.stack((X.flatten(), Y.flatten()), dim=1) y = X.flatten() ** 2 + Y.flatten() ** 2 + 0.1 * torch.randn(size=Y.flatten().size()) - 5 x_train, y_train, x_val, y_val, x_test, y_test = data_split(x, y, train_split=0.6, validation_split=0.2) features = PolynomialFeatures(degree=2, include_bias=True) # Both polynomial features and linear regression must not include a bias x_train = features.transform(x_train) model.fit(x_train, y_train, method="tls", include_bias=False) # Both polynomial features and linear regression must not include a bias z = model.predict(features.transform(x_test)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') surf = ax.scatter(x_test[:, 0], x_test[:, 1], z, color="blue") surf = ax.scatter(x_test[:, 0], x_test[:, 1], y_test, color="red") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.088 seconds) .. _sphx_glr_download_auto_examples_LinearModels_PolynomialFeatures.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: PolynomialFeatures.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: PolynomialFeatures.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: PolynomialFeatures.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_