.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "packages/scikit-learn/auto_examples/plot_linear_regression.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_packages_scikit-learn_auto_examples_plot_linear_regression.py: A simple linear regression =========================== .. GENERATED FROM PYTHON SOURCE LINES 6-38 .. image-sg:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_linear_regression_001.png :alt: plot linear regression :srcset: /packages/scikit-learn/auto_examples/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 # 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() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.048 seconds) .. _sphx_glr_download_packages_scikit-learn_auto_examples_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 `_