SelfTrainingClassifier#

class sklearn.semi_supervised.SelfTrainingClassifier(estimator=None, threshold=0.75, criterion='threshold', k_best=10, max_iter=10, verbose=False)[source]#

自训练分类器。

元估计器(metaestimator)允许给定的有监督分类器充当半监督分类器,使其能够从无标签数据中学习。它通过迭代地预测无标签数据的伪标签并将其添加到训练集中来实现这一点。

分类器将持续迭代,直到达到 max_iter 或在上次迭代中没有伪标签被添加到训练集中。

用户指南中阅读更多内容。

参数:
estimator估计器对象

一个实现 fitpredict_proba 的估计器对象。调用 fit 方法将拟合所传递估计器的克隆,该克隆将存储在 estimator_ 属性中。

版本 1.6 新增: estimator 被添加以替换 base_estimator

thresholdfloat, default=0.75

用于 criterion='threshold' 的决策阈值。应在 [0, 1) 范围内。使用 'threshold' 准则时,应使用校准良好的分类器

criterion{‘threshold’, ‘k_best’}, default=’threshold’

用于选择要添加到训练集中的标签的选择准则。如果为 'threshold',则将预测概率高于 threshold 的伪标签添加到数据集中。如果为 'k_best',则将预测概率最高的 k_best 个伪标签添加到数据集中。当使用“threshold”准则时,应使用校准良好的分类器

k_bestint, default=10

每次迭代中添加的样本数量。仅当 criterion='k_best' 时使用。

max_iterint or None, default=10

允许的最大迭代次数。应大于或等于 0。如果为 None,分类器将继续预测标签,直到没有新的伪标签被添加,或者所有无标签样本都被标记。

verbosebool, default=False

启用详细输出。

属性:
estimator_estimator object

拟合后的估计器。

classes_ndarray or list of ndarray of shape (n_classes,)

每个输出的类别标签。(取自训练后的 estimator_)。

transduction_ndarray of shape (n_samples,)

用于分类器最终拟合的标签,包括在拟合过程中添加的伪标签。

labeled_iter_ndarray of shape (n_samples,)

每个样本被标记的迭代次数。当样本的迭代次数为 0 时,该样本在原始数据集中已标记。当样本的迭代次数为 -1 时,该样本在任何迭代中均未标记。

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

fit 期间看到的特征名称。仅当 X 具有全部为字符串的特征名称时才定义。

1.0 版本新增。

n_iter_int

自训练的轮数,即基本估计器在重新标记的训练集变体上拟合的次数。

termination_condition_{‘max_iter’, ‘no_change’, ‘all_labeled’}

拟合停止的原因。

  • 'max_iter': n_iter_ 达到 max_iter

  • 'no_change': 没有新的标签被预测。

  • 'all_labeled': 在达到 max_iter 之前,所有无标签样本均被标记。

另请参阅

LabelPropagation

标签传播分类器。

LabelSpreading

用于半监督学习的标签传播模型。

References

David Yarowsky. 1995. Unsupervised word sense disambiguation rivaling supervised methods. In Proceedings of the 33rd annual meeting on Association for Computational Linguistics (ACL ‘95). Association for Computational Linguistics, Stroudsburg, PA, USA, 189-196.

示例

>>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import SelfTrainingClassifier
>>> from sklearn.svm import SVC
>>> rng = np.random.RandomState(42)
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = rng.rand(iris.target.shape[0]) < 0.3
>>> iris.target[random_unlabeled_points] = -1
>>> svc = SVC(probability=True, gamma="auto")
>>> self_training_model = SelfTrainingClassifier(svc)
>>> self_training_model.fit(iris.data, iris.target)
SelfTrainingClassifier(...)
decision_function(X, **params)[source]#

调用 estimator 的决策函数。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

表示数据点的数组。

**paramsstr -> object 字典

要传递给底层估计器的 decision_function 方法的参数。

版本 1.6 中新增: 仅当 enable_metadata_routing=True 时可用,可通过使用 sklearn.set_config(enable_metadata_routing=True) 进行设置。有关详细信息,请参阅元数据路由用户指南

返回:
yndarray of shape (n_samples, n_features)

estimator 的决策函数结果。

fit(X, y, **params)[source]#

使用 X, y 作为训练数据拟合自训练分类器。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

表示数据点的数组。

y{array-like, sparse matrix} of shape (n_samples,)

表示标签的数组。无标签样本应具有标签 -1。

**paramsdict

Parameters to pass to the underlying estimators.

版本 1.6 中新增: 仅当 enable_metadata_routing=True 时可用,可通过使用 sklearn.set_config(enable_metadata_routing=True) 进行设置。有关详细信息,请参阅元数据路由用户指南

返回:
selfobject

拟合的估计器。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查阅 用户指南,了解路由机制如何工作。

版本 1.6 中新增。

返回:
routingMetadataRouter

封装路由信息的 MetadataRouter

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。

返回:
paramsdict

参数名称映射到其值。

predict(X, **params)[source]#

预测 X 的类别。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

表示数据点的数组。

**paramsstr -> object 字典

要传递给底层估计器的 predict 方法的参数。

版本 1.6 中新增: 仅当 enable_metadata_routing=True 时可用,可通过使用 sklearn.set_config(enable_metadata_routing=True) 进行设置。有关详细信息,请参阅元数据路由用户指南

返回:
yndarray of shape (n_samples,)

带有预测标签的数组。

predict_log_proba(X, **params)[source]#

预测每个可能结果的对数概率。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

表示数据点的数组。

**paramsstr -> object 字典

要传递给底层估计器的 predict_log_proba 方法的参数。

版本 1.6 中新增: 仅当 enable_metadata_routing=True 时可用,可通过使用 sklearn.set_config(enable_metadata_routing=True) 进行设置。有关详细信息,请参阅元数据路由用户指南

返回:
yndarray of shape (n_samples, n_features)

带有对数预测概率的数组。

predict_proba(X, **params)[source]#

预测每个可能结果的概率。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

表示数据点的数组。

**paramsstr -> object 字典

要传递给底层估计器的 predict_proba 方法的参数。

版本 1.6 中新增: 仅当 enable_metadata_routing=True 时可用,可通过使用 sklearn.set_config(enable_metadata_routing=True) 进行设置。有关详细信息,请参阅元数据路由用户指南

返回:
yndarray of shape (n_samples, n_features)

带有预测概率的数组。

score(X, y, **params)[source]#

调用 estimator 上的 score 方法。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

表示数据点的数组。

yarray-like of shape (n_samples,)

表示标签的数组。

**paramsstr -> object 字典

要传递给底层估计器的 score 方法的参数。

版本 1.6 中新增: 仅当 enable_metadata_routing=True 时可用,可通过使用 sklearn.set_config(enable_metadata_routing=True) 进行设置。有关详细信息,请参阅元数据路由用户指南

返回:
scorefloat

调用 estimator 上的 score 方法的结果。

set_params(**params)[source]#

设置此估计器的参数。

此方法适用于简单的估计器以及嵌套对象(如 Pipeline)。后者具有 <component>__<parameter> 形式的参数,以便可以更新嵌套对象的每个组件。

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。