注意
跳至結尾以下載完整的範例程式碼。或透過 JupyterLite 或 Binder 在您的瀏覽器中執行此範例
使用 FrozenEstimator
的範例#
此範例展示了 FrozenEstimator
的一些使用案例。
FrozenEstimator
是一個實用類別,允許凍結已擬合的估計器。例如,當我們想將已擬合的估計器傳遞給元估計器(例如 FixedThresholdClassifier
)而無需讓元估計器重新擬合估計器時,這很有用。
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
為預先擬合的分類器設定決策閾值#
scikit-learn 中已擬合的分類器使用任意決策閾值來決定給定樣本屬於哪個類別。決策閾值要么是 decision_function 返回值上的 0.0
,要么是 predict_proba 返回的機率上的 0.5
。
但是,可能有人想要設定自訂決策閾值。我們可以透過使用 FixedThresholdClassifier
並使用 FrozenEstimator
包裝分類器來做到這一點。
from sklearn.datasets import make_classification
from sklearn.frozen import FrozenEstimator
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import FixedThresholdClassifier, train_test_split
X, y = make_classification(n_samples=1000, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
classifier = LogisticRegression().fit(X_train, y_train)
print(
"Probability estimates for three data points:\n"
f"{classifier.predict_proba(X_test[-3:]).round(3)}"
)
print(
"Predicted class for the same three data points:\n"
f"{classifier.predict(X_test[-3:])}"
)
Probability estimates for three data points:
[[0.18 0.82]
[0.29 0.71]
[0. 1. ]]
Predicted class for the same three data points:
[1 1 1]
現在想像一下,您想要在機率估計上設定不同的決策閾值。我們可以透過使用 FrozenEstimator
包裝分類器並將其傳遞給 FixedThresholdClassifier
來做到這一點。
threshold_classifier = FixedThresholdClassifier(
estimator=FrozenEstimator(classifier), threshold=0.9
)
請注意,在上面的程式碼片段中,在 FixedThresholdClassifier
上呼叫 fit
不會重新擬合底層分類器。
現在,讓我們看看預測如何相對於機率閾值而變化。
print(
"Probability estimates for three data points with FixedThresholdClassifier:\n"
f"{threshold_classifier.predict_proba(X_test[-3:]).round(3)}"
)
print(
"Predicted class for the same three data points with FixedThresholdClassifier:\n"
f"{threshold_classifier.predict(X_test[-3:])}"
)
Probability estimates for three data points with FixedThresholdClassifier:
[[0.18 0.82]
[0.29 0.71]
[0. 1. ]]
Predicted class for the same three data points with FixedThresholdClassifier:
[0 0 1]
我們看到機率估計保持不變,但由於使用了不同的決策閾值,因此預測的類別有所不同。
請參閱 針對成本敏感學習的事後調整決策閾值,以了解成本敏感學習和決策閾值調整。
校準預先擬合的分類器#
您可以使用 FrozenEstimator
使用 CalibratedClassifierCV
校準預先擬合的分類器。
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import brier_score_loss
calibrated_classifier = CalibratedClassifierCV(
estimator=FrozenEstimator(classifier)
).fit(X_train, y_train)
prob_pos_clf = classifier.predict_proba(X_test)[:, 1]
clf_score = brier_score_loss(y_test, prob_pos_clf)
print(f"No calibration: {clf_score:.3f}")
prob_pos_calibrated = calibrated_classifier.predict_proba(X_test)[:, 1]
calibrated_score = brier_score_loss(y_test, prob_pos_calibrated)
print(f"With calibration: {calibrated_score:.3f}")
No calibration: 0.033
With calibration: 0.032
腳本的總執行時間: (0 分鐘 0.017 秒)
相關範例