.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/feature_selection/plot_rfe_with_cross_validation.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_feature_selection_plot_rfe_with_cross_validation.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_feature_selection_plot_rfe_with_cross_validation.py: =================================================== 带交å‰éªŒè¯çš„é€’å½’ç‰¹å¾æ¶ˆé™¤ =================================================== ä¸€ä¸ªé€’å½’ç‰¹å¾æ¶ˆé™¤ï¼ˆRFE)的示例,通过交å‰éªŒè¯è‡ªåŠ¨è°ƒæ•´æ‰€é€‰ç‰¹å¾çš„æ•°é‡ã€‚ .. GENERATED FROM PYTHON SOURCE LINES 11-15 æ•°æ®ç”Ÿæˆ --------------- 我们使用3个信æ¯ç‰¹å¾æž„建一个分类任务。引入2个é¢å¤–的冗余(å³ç›¸å…³ï¼‰ç‰¹å¾ä¼šå¯¼è‡´æ‰€é€‰ç‰¹å¾å› 交å‰éªŒè¯æŠ˜å è€Œå¼‚ã€‚å…¶ä½™ç‰¹å¾æ˜¯éšæœºæŠ½å–çš„ï¼Œå› æ¤æ˜¯æ— ä¿¡æ¯çš„。 .. GENERATED FROM PYTHON SOURCE LINES 15-30 .. code-block:: Python from sklearn.datasets import make_classification X, y = make_classification( n_samples=500, n_features=15, n_informative=3, n_redundant=2, n_repeated=0, n_classes=8, n_clusters_per_class=1, class_sep=0.8, random_state=0, ) .. GENERATED FROM PYTHON SOURCE LINES 31-35 模型è®ç»ƒä¸Žé€‰æ‹© ---------------------------- 我们创建了RFE对象并计算了交å‰éªŒè¯å¾—分。评分ç–略“准确性â€ä¼˜åŒ–了æ£ç¡®åˆ†ç±»æ ·æœ¬çš„æ¯”例。 .. GENERATED FROM PYTHON SOURCE LINES 35-56 .. code-block:: Python from sklearn.feature_selection import RFECV from sklearn.linear_model import LogisticRegression from sklearn.model_selection import StratifiedKFold min_features_to_select = 1 # Minimum number of features to consider clf = LogisticRegression() cv = StratifiedKFold(5) rfecv = RFECV( estimator=clf, step=1, cv=cv, scoring="accuracy", min_features_to_select=min_features_to_select, n_jobs=2, ) rfecv.fit(X, y) print(f"Optimal number of features: {rfecv.n_features_}") .. rst-class:: sphx-glr-script-out .. code-block:: none Optimal number of features: 3 .. GENERATED FROM PYTHON SOURCE LINES 57-60 åœ¨å½“å‰æƒ…况下,具有3个特å¾çš„æ¨¡åž‹ï¼ˆå¯¹åº”äºŽçœŸå®žç”Ÿæˆæ¨¡åž‹ï¼‰è¢«è®¤ä¸ºæ˜¯æœ€ä¼˜çš„。 ç»˜åˆ¶ç‰¹å¾æ•°é‡ä¸Žäº¤å‰éªŒè¯å¾—分的关系图 .. GENERATED FROM PYTHON SOURCE LINES 60-76 .. code-block:: Python import matplotlib.pyplot as plt import pandas as pd cv_results = pd.DataFrame(rfecv.cv_results_) plt.figure() plt.xlabel("Number of features selected") plt.ylabel("Mean test accuracy") plt.errorbar( x=cv_results["n_features"], y=cv_results["mean_test_score"], yerr=cv_results["std_test_score"], ) plt.title("Recursive Feature Elimination \nwith correlated features") plt.show() .. image-sg:: /auto_examples/feature_selection/images/sphx_glr_plot_rfe_with_cross_validation_001.png :alt: Recursive Feature Elimination with correlated features :srcset: /auto_examples/feature_selection/images/sphx_glr_plot_rfe_with_cross_validation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-78 从上图å¯ä»¥è¿›ä¸€æ¥æ³¨æ„到,对于选择的3到5个特å¾ï¼Œå¾—åˆ†å‡ºçŽ°äº†ä¸€ä¸ªå¹³å°æœŸï¼ˆå¹³å‡å€¼ç›¸ä¼¼ä¸”误差æ¡é‡å )。这是引入相关特å¾çš„结果。实际上,由RFE选择的最优模型å¯èƒ½ä½äºŽè¿™ä¸ªèŒƒå›´å†…,具体å–决于交å‰éªŒè¯æŠ€æœ¯ã€‚选择超过5ä¸ªç‰¹å¾æ—¶ï¼Œæµ‹è¯•准确率下é™ï¼Œä¹Ÿå°±æ˜¯è¯´ï¼Œä¿ç•™æ— ä¿¡æ¯ç‰¹å¾ä¼šå¯¼è‡´è¿‡æ‹Ÿåˆï¼Œå› æ¤å¯¹æ¨¡åž‹çš„统计性能有害。 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.329 seconds) .. _sphx_glr_download_auto_examples_feature_selection_plot_rfe_with_cross_validation.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/feature_selection/plot_rfe_with_cross_validation.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rfe_with_cross_validation.ipynb <plot_rfe_with_cross_validation.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rfe_with_cross_validation.py <plot_rfe_with_cross_validation.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_rfe_with_cross_validation.zip <plot_rfe_with_cross_validation.zip>` .. include:: plot_rfe_with_cross_validation.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_