ClassifierMixin#

class sklearn.base.ClassifierMixin[源代码]#

Scikit-learn中所有分类器的Mixin类。

此mixin定义了以下功能:

  • 将估计器类型设置为 "classifier" 通过 estimator_type 标签;

  • score 默认为 accuracy_score .

  • 强制执行该 fit 需要 y 被传递通过 requires_y 标签,这是通过设置分类器类型标签来完成的。

阅读更多的 User Guide .

示例

>>> import numpy as np
>>> from sklearn.base import BaseEstimator, ClassifierMixin
>>> # Mixin classes should always be on the left-hand side for a correct MRO
>>> class MyEstimator(ClassifierMixin, BaseEstimator):
...     def __init__(self, *, param=1):
...         self.param = param
...     def fit(self, X, y=None):
...         self.is_fitted_ = True
...         return self
...     def predict(self, X):
...         return np.full(shape=X.shape[0], fill_value=self.param)
>>> estimator = MyEstimator(param=1)
>>> X = np.array([[1, 2], [2, 3], [3, 4]])
>>> y = np.array([1, 0, 1])
>>> estimator.fit(X, y).predict(X)
array([1, 1, 1])
>>> estimator.score(X, y)
0.66...
score(X, y, sample_weight=None)[源代码]#

返回给定测试数据和标签的平均准确度。

在多标签分类中,这是子集准确度,这是一个苛刻的指标,因为您需要为每个样本正确预测每个标签集。

参数:
X形状类似阵列(n_samples,n_features)

测试样本。

y形状的类似阵列(n_samples,)或(n_samples,n_outputs)

真正的标签 X .

sample_weight形状类似数组(n_samples,),默认=无

样本重量。

返回:
score浮子

平均准确度 self.predict(X) w.r.t. y .