FixedThresholdClassifier#
- class sklearn.model_selection.FixedThresholdClassifier(estimator, *, threshold='auto', pos_label=None, response_method='auto')[源代码]#
手动设置决策阈值的二元分类器。
此分类器允许更改用于将后验概率估计(即
predict_proba的输出)或决策分数(即decision_function的输出)转换为类标签的默认决策阈值。此处,阈值未进行优化,而是设置为一个常量值。
在 用户指南 中阅读更多内容。
1.5 版本新增。
- 参数:
- estimatorestimator instance
在
predict期间,我们希望优化决策阈值的二元分类器,无论其是否已拟合。- threshold{“auto”} 或 float,默认为“auto”
在将后验概率估计(即
predict_proba的输出)或决策分数(即decision_function的输出)转换为类标签时使用的决策阈值。当设置为"auto"时,如果predict_proba被用作response_method,则阈值设置为 0.5,否则设置为 0(即decision_function的默认阈值)。- pos_labelint, float, bool or str, default=None
正类的标签。用于处理
response_method方法的输出。当pos_label=None时,如果y_true是{-1, 1}或{0, 1},则pos_label设置为 1,否则将引发错误。- response_method{“auto”, “decision_function”, “predict_proba”},默认为“auto”
分类器
estimator对应于我们想要找到阈值的决策函数的那些方法。它可以是如果设置为
"auto",它将按顺序尝试调用"predict_proba"或"decision_function"。否则,可以是
"predict_proba"或"decision_function"中的一个。如果分类器未实现该方法,将引发错误。
- 属性:
另请参阅
sklearn.model_selection.TunedThresholdClassifierCV通过交叉验证,根据某些指标对决策阈值进行后调优的分类器。
sklearn.calibration.CalibratedClassifierCV校准概率的估计器。
示例
>>> from sklearn.datasets import make_classification >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.metrics import confusion_matrix >>> from sklearn.model_selection import FixedThresholdClassifier, train_test_split >>> X, y = make_classification( ... n_samples=1_000, weights=[0.9, 0.1], class_sep=0.8, random_state=42 ... ) >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, stratify=y, random_state=42 ... ) >>> classifier = LogisticRegression(random_state=0).fit(X_train, y_train) >>> print(confusion_matrix(y_test, classifier.predict(X_test))) [[217 7] [ 19 7]] >>> classifier_other_threshold = FixedThresholdClassifier( ... classifier, threshold=0.1, response_method="predict_proba" ... ).fit(X_train, y_train) >>> print(confusion_matrix(y_test, classifier_other_threshold.predict(X_test))) [[184 40] [ 6 20]]
- decision_function(X)[源代码]#
使用已拟合的估计器计算
X中样本的决策函数。- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练向量,其中
n_samples是样本数量,n_features是特征数量。
- 返回:
- decisions形状为 (n_samples,) 的 ndarray
由已拟合的估计器计算的决策函数。
- fit(X, y, **params)[源代码]#
拟合分类器。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练数据。
- yarray-like of shape (n_samples,)
目标值。
- **paramsdict
要传递给底层分类器的
fit方法的参数。
- 返回:
- selfobject
Returns an instance of self.
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请查阅 用户指南,了解路由机制如何工作。
- 返回:
- routingMetadataRouter
封装路由信息的
MetadataRouter。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deepbool, default=True
如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- predict(X)[源代码]#
预测新样本的目标。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
样本,如
estimator.predict所接受。
- 返回:
- class_labels形状为 (n_samples,) 的 ndarray
预测的类别。
- predict_log_proba(X)[源代码]#
使用已拟合的估计器计算
X的对数类概率。- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练向量,其中
n_samples是样本数量,n_features是特征数量。
- 返回:
- log_probabilities形状为 (n_samples, n_classes) 的 ndarray
输入样本的对数类概率。
- predict_proba(X)[源代码]#
使用已拟合的估计器计算
X的类概率。- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练向量,其中
n_samples是样本数量,n_features是特征数量。
- 返回:
- probabilities形状为 (n_samples, n_classes) 的 ndarray
输入样本的类别概率。
- score(X, y, sample_weight=None)[源代码]#
返回在提供的数据和标签上的 准确率 (accuracy)。
在多标签分类中,这是子集准确率 (subset accuracy),这是一个严格的指标,因为它要求每个样本的每个标签集都被正确预测。
- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
测试样本。
- yshape 为 (n_samples,) 或 (n_samples, n_outputs) 的 array-like
X的真实标签。- sample_weightshape 为 (n_samples,) 的 array-like, default=None
样本权重。
- 返回:
- scorefloat
self.predict(X)相对于y的平均准确率。
- set_params(**params)[源代码]#
设置此估计器的参数。
此方法适用于简单的估计器以及嵌套对象(如
Pipeline)。后者具有<component>__<parameter>形式的参数,以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计器参数。
- 返回:
- selfestimator instance
估计器实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') FixedThresholdClassifier[源代码]#
配置是否应请求元数据以传递给
score方法。请注意,此方法仅在以下情况下相关:此估计器用作 元估计器 中的子估计器,并且通过
enable_metadata_routing=True启用了元数据路由(请参阅sklearn.set_config)。请查看 用户指南 以了解路由机制的工作原理。每个参数的选项如下:
True:请求元数据,如果提供则传递给score。如果未提供元数据,则忽略该请求。False:不请求元数据,元估计器不会将其传递给score。None:不请求元数据,如果用户提供元数据,元估计器将引发错误。str:应将元数据以给定别名而不是原始名称传递给元估计器。
默认值 (
sklearn.utils.metadata_routing.UNCHANGED) 保留现有请求。这允许您更改某些参数的请求而不更改其他参数。在版本 1.3 中新增。
- 参数:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
score方法中sample_weight参数的元数据路由。
- 返回:
- selfobject
更新后的对象。