.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_packages_scikit-learn_auto_examples_plot_linear_regression.py: A simple linear regression =========================== .. image:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_linear_regression_001.png :class: sphx-glr-single-img .. code-block:: python import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # x from 0 to 30 x = 30 * np.random.random((20, 1)) # y = a*x + b with noise y = 0.5 * x + 1.0 + np.random.normal(size=x.shape) # create a linear regression model model = LinearRegression() model.fit(x, y) # predict y from the data x_new = np.linspace(0, 30, 100) y_new = model.predict(x_new[:, np.newaxis]) # plot the results plt.figure(figsize=(4, 3)) ax = plt.axes() ax.scatter(x, y) ax.plot(x_new, y_new) ax.set_xlabel('x') ax.set_ylabel('y') ax.axis('tight') plt.show() **Total running time of the script:** ( 0 minutes 0.017 seconds) .. _sphx_glr_download_packages_scikit-learn_auto_examples_plot_linear_regression.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_linear_regression.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_linear_regression.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_