__sklearn_is_fitted__ 作为开发人员API#

__sklearn_is_fitted__ 方法是scikit-learn中使用的一种惯例,用于检查估计器对象是否已被匹配。此方法通常在自定义估计器类中实现,这些类是在scikit-learn的基本类之上构建的,例如 BaseEstimator 或其子集。

开发人员应该使用 check_is_fitted 在所有方法的开头,除了 fit. If they need to customize or speed-up the check, they can implement the `_ _sklearn_is_fitted__'方法如下所示。

在此示例中,自定义估计器展示了 __sklearn_is_fitted__ method and the check_is_fitted utility function as developer APIs. The _ _sklearn_is_fitted__`方法通过验证  `_is_fitted 属性

实现简单分类器的自定义估计器示例#

此代码片段定义了一个名为 CustomEstimator that extends both the BaseEstimator and ClassifierMixin classes from scikit-learn and showcases the usage of the _ _sklearn_is_fit_'方法和  `check_is_fitted 实用功能。

# Authors: The scikit-learn developers
# SPDX-License-Identifier: 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):
        """
        Fit the estimator to the training data.
        """
        self.classes_ = sorted(set(y))
        # Custom attribute to track if the estimator is fitted
        self._is_fitted = True
        return self

    def predict(self, X):
        """
        Perform Predictions

        If the estimator is not fitted, then raise NotFittedError
        """
        check_is_fitted(self)
        # Perform prediction logic
        predictions = [self.classes_[0]] * len(X)
        return predictions

    def score(self, X, y):
        """
        Calculate Score

        If the estimator is not fitted, then raise NotFittedError
        """
        check_is_fitted(self)
        # Perform scoring logic
        return 0.5

    def __sklearn_is_fitted__(self):
        """
        Check fitted status and return a Boolean value.
        """
        return hasattr(self, "_is_fitted") and self._is_fitted

相关实例

归纳集群

Inductive Clustering

元数据路由

Metadata Routing

离散数据结构上的高斯过程

Gaussian processes on discrete data structures

Iris数据集上半监督分类器与支持机的决策边界

Decision boundary of semi-supervised classifiers versus SVM on the Iris dataset

Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io> _