.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/developing_estimators/sklearn_is_fitted.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_developing_estimators_sklearn_is_fitted.py: ======================================== `__sklearn_is_fitted__` كـ API للمطورين ======================================== طريقة `__sklearn_is_fitted__` هي اتفاقية مستخدمة في scikit-learn للتحقق مما إذا كان كائن المُقدر (estimator) قد تم تكييفه (fitted) أم لا. هذه الطريقة يتم تنفيذها عادةً في فئات المُقدرات المخصصة التي يتم بناؤها على فئات القاعدة في scikit-learn مثل `BaseEstimator` أو فئاتها الفرعية. يجب على المطورين استخدام :func:`~sklearn.utils.validation.check_is_fitted` في بداية جميع الطرق باستثناء `fit`. إذا كانوا بحاجة إلى تخصيص أو تسريع عملية التحقق، يمكنهم تنفيذ طريقة `__sklearn_is_fitted__` كما هو موضح أدناه. في هذا المثال، يُظهر المُقدر المخصص استخدام طريقة `__sklearn_is_fitted__` ووظيفة فائدة `check_is_fitted` كـ APIs للمطورين. طريقة `__sklearn_is_fitted__` تتحقق من حالة التكييف (fitted) من خلال التحقق من وجود الخاصية `_is_fitted`. # %% # مثال على مُقدر مخصص ينفذ مُصنف بسيط # ------------------------------------------------------------ # هذا الجزء من الكود يُعرّف فئة مُقدر مخصص تسمى `CustomEstimator` # والتي تمدد كلاً من `BaseEstimator` و `ClassifierMixin` من # scikit-learn وتُظهر استخدام طريقة `__sklearn_is_fitted__` # ووظيفة فائدة `check_is_fitted`. .. GENERATED FROM PYTHON SOURCE LINES 20-66 .. code-block:: Python # المؤلفون: مطورو scikit-learn # معرف الترخيص: BSD-3-Clause from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.utils.validation import check_is_fitted class CustomEstimator(BaseEstimator, ClassifierMixin): def __init__(self, parameter=1): self.parameter = parameter def fit(self, X, y): """ تكييف المُقدر مع بيانات التدريب. """ self.classes_ = sorted(set(y)) # خاصية مخصصة لتتبع ما إذا كان المُقدر مُكيّفاً self._is_fitted = True return self def predict(self, X): """ إجراء التوقعات إذا لم يكن المُقدر مُكيّفاً، فيتم إثارة خطأ NotFittedError """ check_is_fitted(self) # منطق التوقع predictions = [self.classes_[0]] * len(X) return predictions def score(self, X, y): """ حساب النتيجة إذا لم يكن المُقدر مُكيّفاً، فيتم إثارة خطأ NotFittedError """ check_is_fitted(self) # منطق حساب النتيجة return 0.5 def __sklearn_is_fitted__(self): """ التحقق من حالة التكييف وإرجاع قيمة منطقية. """ return hasattr(self, "_is_fitted") and self._is_fitted .. _sphx_glr_download_auto_examples_developing_estimators_sklearn_is_fitted.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/developing_estimators/sklearn_is_fitted.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/developing_estimators/sklearn_is_fitted.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: sklearn_is_fitted.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: sklearn_is_fitted.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: sklearn_is_fitted.zip ` .. include:: sklearn_is_fitted.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_