.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/linear_model/plot_theilsen.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_linear_model_plot_theilsen.py: ==================== انحدار ثيل-سين ==================== يحسب انحدار ثيل-سين على مجموعة بيانات اصطناعية. راجع :ref:`theil_sen_regression` لمزيد من المعلومات حول المنحدر. بالمقارنة مع مقدر المربعات الصغرى العادي (OLS)، فإن مقدر ثيل-سين قوي ضد القيم الشاذة. لديه نقطة انهيار حوالي 29.3% في حالة الانحدار الخطي البسيط، مما يعني أنه يمكنه تحمل البيانات الفاسدة (القيم الشاذة) بنسبة تصل إلى 29.3% في الحالة ثنائية الأبعاد. يتم تقدير النموذج عن طريق حساب المنحدرات والتقاطعات للسكان الفرعيين لجميع التركيبات الممكنة من النقاط الفرعية p. إذا كان تم تركيب التقاطع، يجب أن يكون p أكبر من أو يساوي n_features + 1. ثم يتم تعريف المنحدر والتقاطع النهائيين كمتوسط مكاني لهذه المنحدرات والتقاطعات. في بعض الحالات، يؤدي ثيل-سين أداءً أفضل من :ref:`RANSAC `، وهي طريقة قوية أيضًا. يتم توضيح ذلك في المثال الثاني أدناه حيث تؤثر القيم الشاذة فيما يتعلق بمحور x على RANSAC. يصلح ضبط معلمة "residual_threshold" في RANSAC هذا الأمر، ولكن بشكل عام، هناك حاجة إلى معرفة مسبقة حول البيانات وطبيعة القيم الشاذة. بسبب التعقيد الحسابي لثيل-سين، يوصى باستخدامه فقط للمشاكل الصغيرة من حيث عدد العينات والميزات. بالنسبة للمشاكل الأكبر تقيد معلمة "max_subpopulation" حجم جميع التركيبات الممكنة من النقاط الفرعية p إلى مجموعة فرعية يتم اختيارها عشوائيًا وبالتالي تحد أيضًا من وقت التشغيل. لذلك، يمكن تطبيق ثيل-سين على مشاكل أكبر مع عيب فقدان بعض خصائصه الرياضية حيث أنه يعمل على مجموعة فرعية عشوائية. .. GENERATED FROM PYTHON SOURCE LINES 32-51 .. code-block:: Python # المؤلفون: مطوري سكايلرن # معرف الترخيص: BSD-3-Clause import time import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LinearRegression, RANSACRegressor, TheilSenRegressor estimators = [ ("OLS", LinearRegression()), ("Theil-Sen", TheilSenRegressor(random_state=42)), ("RANSAC", RANSACRegressor(random_state=42)), ] colors = {"OLS": "turquoise", "Theil-Sen": "gold", "RANSAC": "lightgreen"} lw = 2 .. GENERATED FROM PYTHON SOURCE LINES 52-54 القيم الشاذة فقط في اتجاه y -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 54-86 .. code-block:: Python np.random.seed(0) n_samples = 200 # النموذج الخطي y = 3*x + N(2, 0.1**2) x = np.random.randn(n_samples) w = 3.0 c = 2.0 noise = 0.1 * np.random.randn(n_samples) y = w * x + c + noise # 10% قيم شاذة y[-20:] += -20 * x[-20:] X = x[:, np.newaxis] plt.scatter(x, y, color="indigo", marker="x", s=40) line_x = np.array([-3, 3]) for name, estimator in estimators: t0 = time.time() estimator.fit(X, y) elapsed_time = time.time() - t0 y_pred = estimator.predict(line_x.reshape(2, 1)) plt.plot( line_x, y_pred, color=colors[name], linewidth=lw, label="%s (fit time: %.2fs)" % (name, elapsed_time), ) plt.axis("tight") plt.legend(loc="upper left") _ = plt.title("Corrupt y") .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_theilsen_001.png :alt: Corrupt y :srcset: /auto_examples/linear_model/images/sphx_glr_plot_theilsen_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-89 القيم الشاذة في اتجاه X --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 89-120 .. code-block:: Python np.random.seed(0) # النموذج الخطي y = 3*x + N(2, 0.1**2) x = np.random.randn(n_samples) noise = 0.1 * np.random.randn(n_samples) y = 3 * x + 2 + noise # 10% قيم شاذة x[-20:] = 9.9 y[-20:] += 22 X = x[:, np.newaxis] plt.figure() plt.scatter(x, y, color="indigo", marker="x", s=40) line_x = np.array([-3, 10]) for name, estimator in estimators: t0 = time.time() estimator.fit(X, y) elapsed_time = time.time() - t0 y_pred = estimator.predict(line_x.reshape(2, 1)) plt.plot( line_x, y_pred, color=colors[name], linewidth=lw, label="%s (fit time: %.2fs)" % (name, elapsed_time), ) plt.axis("tight") plt.legend(loc="upper left") plt.title("Corrupt x") plt.show() .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_theilsen_002.png :alt: Corrupt x :srcset: /auto_examples/linear_model/images/sphx_glr_plot_theilsen_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.717 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_theilsen.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/linear_model/plot_theilsen.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/linear_model/plot_theilsen.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_theilsen.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_theilsen.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_theilsen.zip ` .. include:: plot_theilsen.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_