固定阈值分类器#
- 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"} 或浮点数,默认为 "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]] - property classes_#
- 类别标签。 
 - decision_function(X)[source]#
- 使用已拟合估计器对 - X中样本的决策函数。- 参数:
- X形状为 (n_samples, n_features) 的 {数组型、稀疏矩阵}
- 训练向量,其中 - n_samples是样本数,- n_features是特征数。
 
- 返回:
- decisions形状为 (n_samples,) 的 ndarray
- 使用已拟合估计器计算的决策函数。 
 
 
 - fit(X, y, **params)[source]#
- 拟合分类器。 - 参数:
- X形状为 (n_samples, n_features) 的 {数组型、稀疏矩阵}
- 训练数据。 
- y形状为 (n_samples,) 的数组型
- 目标值。 
- **paramsdict
- 要传递给底层分类器的 - fit方法的参数。
 
- 返回:
- selfobject
- 返回 self 的实例。 
 
 
 - get_metadata_routing()[source]#
- 获取此对象的元数据路由。 - 请查看 用户指南,了解路由机制的工作原理。 - 返回:
- routingMetadataRouter
- 封装路由信息的 - MetadataRouter。
 
 
 - get_params(deep=True)[source]#
- 获取此估计器的参数。 - 参数:
- deepbool,默认值=True
- 如果为 True,将返回此估计器和包含的子对象的参数(这些子对象是估计器)。 
 
- 返回:
- paramsdict
- 参数名称与其值的映射。 
 
 
 - predict(X)[source]#
- 预测新样本的目标。 - 参数:
- X形状为 (n_samples, n_features) 的 {数组型、稀疏矩阵}
- 样本,被 - estimator.predict接受。
 
- 返回:
- class_labels形状为 (n_samples,) 的 ndarray
- 预测的类别。 
 
 
 - predict_log_proba(X)[source]#
- 使用已拟合估计器预测 - X的对数类别概率。- 参数:
- X形状为 (n_samples, n_features) 的 {数组型、稀疏矩阵}
- 训练向量,其中 - n_samples是样本数,- n_features是特征数。
 
- 返回:
- log_probabilities形状为 (n_samples, n_classes) 的 ndarray
- 输入样本的对数类别概率。 
 
 
 - predict_proba(X)[source]#
- 使用已拟合的估计器预测 - X的类概率。- 参数:
- X形状为 (n_samples, n_features) 的 {数组型、稀疏矩阵}
- 训练向量,其中 - n_samples是样本数,- n_features是特征数。
 
- 返回:
- probabilitiesndarray of shape (n_samples, n_classes)
- 输入样本的类概率。 
 
 
 - score(X, y, sample_weight=None)[source]#
- 返回给定测试数据和标签的平均准确率。 - 在多标签分类中,这是子集准确率,这是一个严格的指标,因为您要求每个样本的每个标签集都必须被正确预测。 - 参数:
- Xarray-like of shape (n_samples, n_features)
- 测试样本。 
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
- X的真实标签。
- sample_weightarray-like of shape (n_samples,), default=None
- 样本权重。 
 
- 返回:
- scorefloat
- self.predict(X)关于- y的平均准确率。
 
 
 - set_params(**params)[source]#
- 设置此估计器的参数。 - 此方法适用于简单的估计器以及嵌套对象(例如 - Pipeline)。后者具有- <component>__<parameter>形式的参数,因此可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
- 估计器参数。 
 
- 返回:
- selfestimator instance
- 估计器实例。 
 
 
 - 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, or None, default=sklearn.utils.metadata_routing.UNCHANGED
- score中- sample_weight参数的元数据路由。
 
- 返回:
- selfobject
- 更新后的对象。 
 
 
 
 
     
 
 
