.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/model_selection/plot_underfitting_overfitting.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end <sphx_glr_download_auto_examples_model_selection_plot_underfitting_overfitting.py>` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_model_selection_plot_underfitting_overfitting.py: ============================ æ¬ æ‹Ÿåˆä¸Žè¿‡æ‹Ÿåˆ ============================ æœ¬ç¤ºä¾‹æ¼”ç¤ºäº†æ¬ æ‹Ÿåˆå’Œè¿‡æ‹Ÿåˆçš„问题,以åŠå¦‚何使用带有多项å¼ç‰¹å¾çš„线性回归æ¥é€¼è¿‘éžçº¿æ€§å‡½æ•°ã€‚图䏿˜¾ç¤ºäº†æˆ‘们想è¦é€¼è¿‘的函数,它是余弦函数的一部分。æ¤å¤–ï¼Œè¿˜æ˜¾ç¤ºäº†çœŸå®žå‡½æ•°çš„æ ·æœ¬å’Œä¸åŒæ¨¡åž‹çš„逼近结果。这些模型具有ä¸åŒæ¬¡æ•°çš„多项å¼ç‰¹å¾ã€‚我们å¯ä»¥çœ‹åˆ°ï¼Œçº¿æ€§å‡½æ•°ï¼ˆæ¬¡æ•°ä¸º1的多项å¼ï¼‰ä¸è¶³ä»¥æ‹Ÿåˆè®ç»ƒæ ·æœ¬ã€‚这被称为 **æ¬ æ‹Ÿåˆ** 。次数为4的多项å¼å‡ 乎完美地逼近了真实函数。然而,对于更高次数的多项å¼ï¼Œæ¨¡åž‹ä¼šå¯¹è®ç»ƒæ•°æ® **过拟åˆ** ,å³å®ƒå¦ä¹ 了è®ç»ƒæ•°æ®çš„噪声。 我们通过使用交å‰éªŒè¯å®šé‡è¯„ä¼° **过拟åˆ** / **æ¬ æ‹Ÿåˆ** 。我们计算验è¯é›†ä¸Šçš„凿–¹è¯¯å·®ï¼ˆMSE),误差越高,模型从è®ç»ƒæ•°æ®ä¸æ£ç¡®æ³›åŒ–çš„å¯èƒ½æ€§å°±è¶Šå°ã€‚ .. GENERATED FROM PYTHON SOURCE LINES 10-67 .. image-sg:: /auto_examples/model_selection/images/sphx_glr_plot_underfitting_overfitting_001.png :alt: Degree 1 MSE = 4.08e-01(+/- 4.25e-01), Degree 4 MSE = 4.32e-02(+/- 7.08e-02), Degree 15 MSE = 1.83e+08(+/- 5.48e+08) :srcset: /auto_examples/model_selection/images/sphx_glr_plot_underfitting_overfitting_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LinearRegression from sklearn.model_selection import cross_val_score from sklearn.pipeline import Pipeline from sklearn.preprocessing import PolynomialFeatures def true_fun(X): return np.cos(1.5 * np.pi * X) np.random.seed(0) n_samples = 30 degrees = [1, 4, 15] X = np.sort(np.random.rand(n_samples)) y = true_fun(X) + np.random.randn(n_samples) * 0.1 plt.figure(figsize=(14, 5)) for i in range(len(degrees)): ax = plt.subplot(1, len(degrees), i + 1) plt.setp(ax, xticks=(), yticks=()) polynomial_features = PolynomialFeatures(degree=degrees[i], include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline( [ ("polynomial_features", polynomial_features), ("linear_regression", linear_regression), ] ) pipeline.fit(X[:, np.newaxis], y) # 使用交å‰éªŒè¯è¯„估模型 scores = cross_val_score( pipeline, X[:, np.newaxis], y, scoring="neg_mean_squared_error", cv=10 ) X_test = np.linspace(0, 1, 100) plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model") plt.plot(X_test, true_fun(X_test), label="True function") plt.scatter(X, y, edgecolor="b", s=20, label="Samples") plt.xlabel("x") plt.ylabel("y") plt.xlim((0, 1)) plt.ylim((-2, 2)) plt.legend(loc="best") plt.title( "Degree {}\nMSE = {:.2e}(+/- {:.2e})".format( degrees[i], -scores.mean(), scores.std() ) ) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.102 seconds) .. _sphx_glr_download_auto_examples_model_selection_plot_underfitting_overfitting.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/model_selection/plot_underfitting_overfitting.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_underfitting_overfitting.ipynb <plot_underfitting_overfitting.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_underfitting_overfitting.py <plot_underfitting_overfitting.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_underfitting_overfitting.zip <plot_underfitting_overfitting.zip>` .. include:: plot_underfitting_overfitting.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_