.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "sklearn_ensae_course/plot_bias_variance.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_bias_variance.py: Biais vs Variance ================= Fits the data with polynoms with increasing degrees. .. GENERATED FROM PYTHON SOURCE LINES 8-54 .. image-sg:: /sklearn_ensae_course/images/sphx_glr_plot_bias_variance_001.png :alt: d = 1 (under-fit; high bias), d = 2, d = 6 (over-fit; high variance) :srcset: /sklearn_ensae_course/images/sphx_glr_plot_bias_variance_001.png :class: sphx-glr-single-img .. code-block:: default import numpy as np import matplotlib.pyplot as plt def test_func(x, err=0.5): return np.random.normal(10 - 1. / (x + 0.1), err) def compute_error(x, y, p): yfit = np.polyval(p, x) return np.sqrt(np.mean((y - yfit) ** 2)) def plot_bias_variance(N=8, random_seed=42, err=0.5): x = 10 ** np.linspace(-2, 0, N) y = test_func(x) xfit = np.linspace(-0.2, 1.2, 1000) titles = ['d = 1 (under-fit; high bias)', 'd = 2', 'd = 6 (over-fit; high variance)'] degrees = [1, 2, 6] fig = plt.figure(figsize=(9, 3.5)) fig.subplots_adjust(left=0.06, right=0.98, bottom=0.15, top=0.85, wspace=0.05) for i, d in enumerate(degrees): ax = fig.add_subplot(131 + i, xticks=[], yticks=[]) ax.scatter(x, y, marker='x', c='k', s=50) p = np.polyfit(x, y, d) yfit = np.polyval(p, xfit) ax.plot(xfit, yfit, '-b') ax.set_xlim(-0.2, 1.2) ax.set_ylim(0, 12) ax.set_xlabel('house size') if i == 0: ax.set_ylabel('price') ax.set_title(titles[i]) plot_bias_variance() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.519 seconds) .. _sphx_glr_download_sklearn_ensae_course_plot_bias_variance.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_bias_variance.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_bias_variance.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_