FixedThresholdClassifier#
- class sklearn.model_selection.FixedThresholdClassifier(estimator, *, threshold='auto', pos_label=None, response_method='auto')[source]#
手动设置决策阈值的二元分类器。
该分类器允许更改用于将后验概率估计(即
predict_proba
的输出)或决策分数(即decision_function
的输出)转换为类别标签的默认决策阈值。在此,阈值未经优化,并被设置为一个常数值。
在用户指南中阅读更多信息。
版本 1.5 中新增。
- 参数:
- estimator估计器实例
我们希望优化在
predict
期间使用的决策阈值的二元分类器,无论其是否已拟合。- threshold{“auto”} 或 float,默认值=”auto”
将后验概率估计(即
predict_proba
的输出)或决策分数(即decision_function
的输出)转换为类别标签时使用的决策阈值。当为"auto"
时,如果predict_proba
用作response_method
,则阈值设置为 0.5,否则设置为 0(即decision_function
的默认阈值)。- pos_labelint, float, bool 或 str,默认值=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)[source]#
使用已拟合估计器对
X
中的样本进行决策函数计算。- 参数:
- X形状为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练向量,其中
n_samples
是样本数量,n_features
是特征数量。
- 返回:
- decisions形状为 (n_samples,) 的 ndarray
已拟合估计器计算出的决策函数。
- fit(X, y, **params)[source]#
拟合分类器。
- 参数:
- X形状为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练数据。
- y形状为 (n_samples,) 的 array-like
目标值。
- **paramsdict
传递给底层分类器
fit
方法的参数。
- 返回:
- self对象
返回一个 self 实例。
- get_metadata_routing()[source]#
获取此对象的元数据路由。
请查看用户指南,了解路由机制的工作原理。
- 返回:
- routingMetadataRouter
一个封装路由信息的
MetadataRouter
。
- get_params(deep=True)[source]#
获取此估计器的参数。
- 参数:
- deep布尔值,默认值=True
如果为 True,将返回此估计器及其包含的作为估计器的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- predict(X)[source]#
预测新样本的目标。
- 参数:
- X形状为 (n_samples, n_features) 的 {array-like, sparse matrix}
样本,由
estimator.predict
接受。
- 返回:
- class_labels形状为 (n_samples,) 的 ndarray
预测的类别。
- predict_log_proba(X)[source]#
使用已拟合估计器预测
X
的对数类别概率。- 参数:
- X形状为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练向量,其中
n_samples
是样本数量,n_features
是特征数量。
- 返回:
- log_probabilities形状为 (n_samples, n_classes) 的 ndarray
输入样本的对数类别概率。
- predict_proba(X)[source]#
使用已拟合估计器预测
X
的类别概率。- 参数:
- X形状为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练向量,其中
n_samples
是样本数量,n_features
是特征数量。
- 返回:
- probabilities形状为 (n_samples, n_classes) 的 ndarray
输入样本的类别概率。
- score(X, y, sample_weight=None)[source]#
返回在提供的数据和标签上的准确率。
在多标签分类中,这是子集准确率,这是一个严格的指标,因为它要求每个样本的每个标签集都必须被正确预测。
- 参数:
- X形状为 (n_samples, n_features) 的 array-like
测试样本。
- y形状为 (n_samples,) 或 (n_samples, n_outputs) 的 array-like
X
的真实标签。- sample_weight形状为 (n_samples,) 的 array-like,默认值=None
样本权重。
- 返回:
- score浮点数
self.predict(X)
相对于y
的平均准确率。
- set_params(**params)[source]#
设置此估计器的参数。
此方法适用于简单估计器以及嵌套对象(例如
Pipeline
)。后者具有<component>__<parameter>
形式的参数,因此可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计器参数。
- 返回:
- self估计器实例
估计器实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') FixedThresholdClassifier [source]#
请求传递给
score
方法的元数据。请注意,此方法仅在
enable_metadata_routing=True
时才相关(参见sklearn.set_config
)。请参阅用户指南,了解路由机制的工作原理。每个参数的选项如下:
True
: 请求元数据,如果提供则传递给score
。如果未提供元数据,则忽略请求。False
: 不请求元数据,并且元估计器不会将其传递给score
。None
: 不请求元数据,如果用户提供元数据,元估计器将引发错误。str
: 元数据应使用此给定别名而非原始名称传递给元估计器。
默认值(
sklearn.utils.metadata_routing.UNCHANGED
)保留现有请求。这允许您更改某些参数的请求,而不更改其他参数的请求。版本 1.3 中新增。
注意
此方法仅在估计器用作元估计器(例如在
Pipeline
中使用)的子估计器时才相关。否则,它没有效果。- 参数:
- sample_weightstr, True, False, 或 None,默认值=sklearn.utils.metadata_routing.UNCHANGED
score
中sample_weight
参数的元数据路由。
- 返回:
- self对象
更新后的对象。