.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "sklearn_ensae_course/plot_linear_regression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_sklearn_ensae_course_plot_linear_regression.py: Simple Linear Regression ======================== See `LinearRegression `_. .. GENERATED FROM PYTHON SOURCE LINES 7-43 .. image-sg:: /sklearn_ensae_course/images/sphx_glr_plot_linear_regression_001.png :alt: plot linear regression :srcset: /sklearn_ensae_course/images/sphx_glr_plot_linear_regression_001.png :class: sphx-glr-single-img .. code-block:: default import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression def plot_linear_regression(): a = 0.5 b = 1.0 # x from 0 to 10 x = 30 * np.random.random(20) # y = a*x + b with noise y = a * x + b + np.random.normal(size=x.shape) # create a linear regression classifier clf = LinearRegression() clf.fit(x[:, None], y) # predict y from the data x_new = np.linspace(0, 30, 100) y_new = clf.predict(x_new[:, None]) # plot the results ax = plt.axes() ax.scatter(x, y) ax.plot(x_new, y_new) ax.set_xlabel('x') ax.set_ylabel('y') ax.axis('tight') plot_linear_regression() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.326 seconds) .. _sphx_glr_download_sklearn_ensae_course_plot_linear_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_linear_regression.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_linear_regression.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_