.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ensemble/plot_forest_importances.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. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_ensemble_plot_forest_importances.py: ========================================== أهمية الميزات باستخدام غابة من الأشجار ========================================== هذا المثال يوضح استخدام غابة من الأشجار لتقييم أهمية الميزات في مهمة تصنيف اصطناعية. تمثل الأعمدة الزرقاء أهمية الميزات للغابة، إلى جانب تباينها بين الأشجار الذي يمثله خطأ الأعمدة. كما هو متوقع، يوضح الرسم البياني أن 3 ميزات مفيدة، في حين أن الميزات المتبقية ليست كذلك. .. GENERATED FROM PYTHON SOURCE LINES 10-15 .. code-block:: Python # المؤلفون: مطوري سكايت-ليرن # معرف الترخيص: BSD-3-Clause import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 16-22 توليد البيانات وتناسب النموذج --------------------------------- نقوم بتوليد مجموعة بيانات اصطناعية تحتوي على 3 ميزات مفيدة فقط. لن نقوم بخلط المجموعة بشكل صريح لضمان أن الميزات المفيدة ستتوافق مع الأعمدة الثلاثة الأولى من X. بالإضافة إلى ذلك، سنقوم بتقسيم مجموعة البيانات الخاصة بنا إلى مجموعات فرعية للتدريب والاختبار. .. GENERATED FROM PYTHON SOURCE LINES 22-37 .. code-block:: Python from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split X, y = make_classification( n_samples=1000, n_features=10, n_informative=3, n_redundant=0, n_repeated=0, n_classes=2, random_state=0, shuffle=False, ) X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42) .. GENERATED FROM PYTHON SOURCE LINES 38-39 سيتم تناسب مصنف غابة عشوائية لحساب أهمية الميزات. .. GENERATED FROM PYTHON SOURCE LINES 39-45 .. code-block:: Python from sklearn.ensemble import RandomForestClassifier feature_names = [f"feature {i}" for i in range(X.shape[1])] forest = RandomForestClassifier(random_state=0) forest.fit(X_train, y_train) .. raw:: html
RandomForestClassifier(random_state=0)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 46-56 أهمية الميزة بناءً على الانخفاض المتوسط في عدم النقاء ----------------------------------------------------- يتم توفير أهمية الميزات بواسطة الخاصية المناسبة `feature_importances_` ويتم حسابها كمتوسط وانحراف معياري تراكم انخفاض عدم النقاء داخل كل شجرة. .. تحذير:: يمكن أن تكون أهمية الميزات القائمة على عدم النقاء مضللة للميزات ذات **الارتفاع cardinality** (العديد من القيم الفريدة). راجع :ref:`permutation_importance` كبديل أدناه. .. GENERATED FROM PYTHON SOURCE LINES 56-66 .. code-block:: Python import time import numpy as np start_time = time.time() importances = forest.feature_importances_ std = np.std([tree.feature_importances_ for tree in forest.estimators_], axis=0) elapsed_time = time.time() - start_time print(f"Elapsed time to compute the importances: {elapsed_time:.3f} seconds") .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time to compute the importances: 0.009 seconds .. GENERATED FROM PYTHON SOURCE LINES 67-68 دعنا نرسم أهمية الانخفاض في عدم النقاء. .. GENERATED FROM PYTHON SOURCE LINES 68-78 .. code-block:: Python import pandas as pd forest_importances = pd.Series(importances, index=feature_names) fig, ax = plt.subplots() forest_importances.plot.bar(yerr=std, ax=ax) ax.set_title("Feature importances using MDI") ax.set_ylabel("Mean decrease in impurity") fig.tight_layout() .. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_forest_importances_001.png :alt: Feature importances using MDI :srcset: /auto_examples/ensemble/images/sphx_glr_plot_forest_importances_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-85 نلاحظ أنه، كما هو متوقع، يتم العثور على الميزات الثلاثة الأولى كأهمية. أهمية الميزة بناءً على تبديل الميزة ----------------------------------------------- تتجاوز أهمية الميزة بالتبديل قيود أهمية الميزة القائمة على عدم النقاء: فهي لا تحتوي على تحيز نحو الميزات ذات cardinality العالي ويمكن حسابها على مجموعة اختبار مستبعدة. .. GENERATED FROM PYTHON SOURCE LINES 85-96 .. code-block:: Python from sklearn.inspection import permutation_importance start_time = time.time() result = permutation_importance( forest, X_test, y_test, n_repeats=10, random_state=42, n_jobs=2 ) elapsed_time = time.time() - start_time print(f"Elapsed time to compute the importances: {elapsed_time:.3f} seconds") forest_importances = pd.Series(result.importances_mean, index=feature_names) .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time to compute the importances: 0.799 seconds .. GENERATED FROM PYTHON SOURCE LINES 97-101 الحساب لأهمية التبديل الكامل أكثر تكلفة. يتم خلط الميزات n مرات ويتم إعادة تناسب النموذج لتقدير أهمية ذلك. يرجى الاطلاع على :ref:`permutation_importance` لمزيد من التفاصيل. يمكننا الآن رسم ترتيب الأهمية. .. GENERATED FROM PYTHON SOURCE LINES 101-108 .. code-block:: Python fig, ax = plt.subplots() forest_importances.plot.bar(yerr=result.importances_std, ax=ax) ax.set_title("Feature importances using permutation on full model") ax.set_ylabel("Mean accuracy decrease") fig.tight_layout() plt.show() .. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_forest_importances_002.png :alt: Feature importances using permutation on full model :srcset: /auto_examples/ensemble/images/sphx_glr_plot_forest_importances_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 109-111 يتم اكتشاف نفس الميزات كأهمية باستخدام كلتا الطريقتين. على الرغم من تختلف الأهمية النسبية. كما هو موضح في الرسوم البيانية، MDI أقل احتمالا من أهمية التبديل لإغفال ميزة تمامًا. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.326 seconds) .. _sphx_glr_download_auto_examples_ensemble_plot_forest_importances.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/ensemble/plot_forest_importances.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/ensemble/plot_forest_importances.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_forest_importances.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_forest_importances.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_forest_importances.zip ` .. include:: plot_forest_importances.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_