.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ensemble/plot_isolation_forest.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_isolation_forest.py: ======================= مثال IsolationForest ======================= مثال يستخدم :class:`~sklearn.ensemble.IsolationForest` للكشف عن الشذوذ. :ref:`isolation_forest` هي مجموعة من "أشجار العزل" التي "تعزل" الملاحظات عن طريق التقسيم العشوائي التكراري، والذي يمكن تمثيله ببنية شجرة. يكون عدد التقسيمات المطلوبة لعزل عينة أقل بالنسبة للقيم المتطرفة وأعلى بالنسبة للقيم الداخلية. في هذا المثال، نعرض طريقتين لتصور حدود القرار لـ Isolation Forest المدربة على مجموعة بيانات تجريبية. .. GENERATED FROM PYTHON SOURCE LINES 17-21 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 22-33 توليد البيانات --------------- نقوم بإنشاء مجموعتين (كل منهما تحتوي على `n_samples`) عن طريق أخذ عينات عشوائية من التوزيع الطبيعي القياسي كما هو مسترجع بواسطة :func:`numpy.random.randn`. إحداهما كروية والأخرى مشوهة قليلاً. من أجل الاتساق مع تدوين :class:`~sklearn.ensemble.IsolationForest`، يتم تعيين تصنيف أرضي `1` للقيم الداخلية (أي المجموعات الغاوسية) بينما يتم تعيين التصنيف `-1` للقيم المتطرفة (التي تم إنشاؤها باستخدام :func:`numpy.random.uniform`). .. GENERATED FROM PYTHON SOURCE LINES 33-52 .. code-block:: Python import numpy as np from sklearn.model_selection import train_test_split n_samples, n_outliers = 120, 40 rng = np.random.RandomState(0) covariance = np.array([[0.5, -0.1], [0.7, 0.4]]) cluster_1 = 0.4 * rng.randn(n_samples, 2) @ covariance + np.array([2, 2]) # عام cluster_2 = 0.3 * rng.randn(n_samples, 2) + np.array([-2, -2]) # كروي outliers = rng.uniform(low=-4, high=4, size=(n_outliers, 2)) X = np.concatenate([cluster_1, cluster_2, outliers]) y = np.concatenate( [np.ones((2 * n_samples), dtype=int), -np.ones((n_outliers), dtype=int)] ) X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42) .. GENERATED FROM PYTHON SOURCE LINES 53-54 يمكننا تصور المجموعات الناتجة: .. GENERATED FROM PYTHON SOURCE LINES 54-64 .. code-block:: Python import matplotlib.pyplot as plt scatter = plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k") handles, labels = scatter.legend_elements() plt.axis("square") plt.legend(handles=handles, labels=["القيم المتطرفة", "القيم الداخلية"], title="التصنيف الحقيقي") plt.title("القيم الداخلية الغاوسية مع \nالقيم المتطرفة الموزعة بشكل موحد") plt.show() .. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_isolation_forest_001.png :alt: القيم الداخلية الغاوسية مع القيم المتطرفة الموزعة بشكل موحد :srcset: /auto_examples/ensemble/images/sphx_glr_plot_isolation_forest_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-67 تدريب النموذج --------------------- .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. code-block:: Python from sklearn.ensemble import IsolationForest clf = IsolationForest(max_samples=100, random_state=0) clf.fit(X_train) .. raw:: html
IsolationForest(max_samples=100, 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 74-81 رسم حدود القرار المنفصلة ------------------------------- نستخدم الفئة :class:`~sklearn.inspection.DecisionBoundaryDisplay` لتصور حدود القرار المنفصلة. يمثل لون الخلفية ما إذا كانت عينة في تلك المنطقة معينة متوقع أن تكون قيمة متطرفة أم لا. يعرض مخطط التشتت التصنيفات الحقيقية. .. GENERATED FROM PYTHON SOURCE LINES 81-98 .. code-block:: Python import matplotlib.pyplot as plt from sklearn.inspection import DecisionBoundaryDisplay disp = DecisionBoundaryDisplay.from_estimator( clf, X, response_method="predict", alpha=0.5, ) disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k") disp.ax_.set_title("حدود القرار الثنائية \nلـ IsolationForest") plt.axis("square") plt.legend(handles=handles, labels=["القيم المتطرفة", "القيم الداخلية"], title="التصنيف الحقيقي") plt.show() .. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_isolation_forest_002.png :alt: حدود القرار الثنائية لـ IsolationForest :srcset: /auto_examples/ensemble/images/sphx_glr_plot_isolation_forest_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 99-112 رسم حدود قرار طول المسار ---------------------------------- عن طريق تعيين `response_method="decision_function"`، تمثل خلفية :class:`~sklearn.inspection.DecisionBoundaryDisplay` مقياس طبيعية الملاحظة. يتم إعطاء هذه النتيجة بواسطة متوسط ​​طول المسار على غابة من الأشجار العشوائية، والذي يتم إعطاؤه بواسطة عمق الورقة (أو بشكل مكافئ عدد التقسيمات) المطلوبة لعزل عينة معينة. عندما تنتج غابة من الأشجار العشوائية بشكل جماعي أطوال مسار قصيرة لعزل بعض العينات المعينة، فمن المحتمل جدًا أن تكون شذوذًا ويكون مقياس الطبيعية قريبًا من `0`. وبشكل مشابه، تتوافق المسارات الكبيرة مع القيم القريبة من `1` ومن المرجح أن تكون قيمًا داخلية. .. GENERATED FROM PYTHON SOURCE LINES 112-127 .. code-block:: Python disp = DecisionBoundaryDisplay.from_estimator( clf, X, response_method="decision_function", alpha=0.5, ) disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k") disp.ax_.set_title("حدود قرار طول المسار \nلـ IsolationForest") plt.axis("square") plt.legend(handles=handles, labels=["القيم المتطرفة", "القيم الداخلية"], title="التصنيف الحقيقي") plt.colorbar(disp.ax_.collections[1]) plt.show() .. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_isolation_forest_003.png :alt: حدود قرار طول المسار لـ IsolationForest :srcset: /auto_examples/ensemble/images/sphx_glr_plot_isolation_forest_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.534 seconds) .. _sphx_glr_download_auto_examples_ensemble_plot_isolation_forest.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_isolation_forest.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_isolation_forest.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_isolation_forest.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_isolation_forest.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_isolation_forest.zip ` .. include:: plot_isolation_forest.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_