LoginSignup
0
1

確率補正とその実装:プラットスケーリングを用いた例

Posted at

はじめに、私たちは分類問題で機械学習モデルを使用する際、クラスラベルだけでなく、予測確率にも注目することがあります。予測確率は特に、医療、金融、保険など、リスク評価が重要な領域でよく使用されます。しかし、モデルが出力する確率が真の確率と一致する保証はなく、カリブレーション(確率補正)が必要になることがあります。ここでは、プラットスケーリングを用いた確率補正の方法について解説します。

プラットスケーリングとは

プラットスケーリングは、モデルが出力する確率を真の確率に近づけるための方法です。具体的には、サポートベクターマシンやブースティングアルゴリズムなどのモデルの出力をシグモイド関数に通すことで、補正された確率を得ます。

scikit-learnでのプラットスケーリングの実装

Pythonのscikit-learnライブラリでは、CalibratedClassifierCVを使用して確率補正を行うことができます。以下にその実装例を示します。

from sklearn.ensemble import RandomForestClassifier
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import log_loss
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification

# create a synthetic binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# train a random forest classifier
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)

# predict probabilities
probs = rf.predict_proba(X_test)

# compute log loss
loss = log_loss(y_test, probs)
print('Log loss before calibration: ', loss)

# Now, calibrate the probabilities
calibrator = CalibratedClassifierCV(rf, method='sigmoid', cv='prefit')
calibrator.fit(X_test, y_test)

# predict calibrated probabilities
calibrated_probs = calibrator.predict_proba(X_test)

# compute log loss again
loss = log_loss(y_test, calibrated_probs)
print('Log loss after calibration: ', loss)

この例では、ランダムフォレストをトレーニングし、CalibratedClassifierCVを用いて確率を補正しています。そして、補正前と補正後のログロス(モデルの予測の信頼度を計測するための損失関数)を比較しています。

確率補正は、モデルの出力が確率として解釈可能な場合に重要です。特に、不均衡なデータセットに対する予測、または高い信頼性が求められる予測などで有用です。その一方で、モデルの予測精度(たとえば、精度、再現率、F1スコアなど)に影響を与えないことに注意が必要です。これらの評価指標は、予測の順序にのみ依存しているため、確率補正はその順序を変えません。

以上が、プラットスケーリングを用いた確率補正の基本的な考え方と実装になります。これが皆さんの機械学習モデリングの一助となれば幸いです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1