キャンセル
次の結果を表示 
次の代わりに検索 
もしかして: 
DataRobot コミュニティ

RocCurveクラスでMCC最大化閾値を取得する方法

Kazuki Hosoya
青色LED

RocCurveクラスでMCC最大化閾値を取得する方法

PythonライブラリのRocCurveクラスでF1最大化の閾値ではなく、MCC最大化の閾値を取得する方法を知りたいです。F1最大化は下記メソッドがありますが、get_best_mcc_threshold()メソッドはないようなので。

 

F1最大化取得方法

roc = model.get_roc_curve('validation')
threshold = roc.get_best_f1_threshold()

 

メソッド確認

dir(roc)

 

['__annotations__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_client', '_converter', '_fields', '_filter_data', '_repack_insights_response', '_safe_data', '_server_data', '_validate_threshold', 'estimate_threshold', 'from_data', 'from_location', 'from_server_data', 'get_best_f1_threshold', 'negative_class_predictions', 'positive_class_predictions', 'roc_points', 'source', 'source_model_id']

 

 

Pythonライブラリバージョンは3.2.1を使ってます。

0 件の賞賛
2件の返信2
mitsuo
データサイエンティスト
データサイエンティスト

ご質問いただきありがとうございます。以下のコードでいかがでしょうか。

import pandas as pd
import datarobot as dr
from datarobot.enums import ACCURACY_METRIC
from sklearn.datasets import load_breast_cancer

# 乳がんデータセットをロード
breast_cancer = load_breast_cancer()
X = breast_cancer.data
y = breast_cancer.target

# 特徴量をDataFrameに変換
breast_cancer_df = pd.DataFrame(X, columns=breast_cancer.feature_names)

# 目的変数をDataFrameに追加
breast_cancer_df['target'] = y

# DataRobotプロジェクトを作成
project = dr.Project.create(sourcedata=breast_cancer_df, project_name="Breast Cancer Classification")

# モデルの分析とトレーニングを開始
project.analyze_and_model(
    target="target",
    metric=ACCURACY_METRIC.AUC,
    worker_count=-1,
)

# AutoPilotの完了を待つ
project.wait_for_autopilot()

# ベストモデルを取得
best_model = project.get_models()[0]

# ROC曲線を取得
roc = best_model.get_roc_curve('validation').roc_points

# MCC を最大化するエントリを探す
max_mcc = max(roc, key=lambda x: x['matthews_correlation_coefficient'])

# その際のthresholdを出力
max_mcc["threshold"]

 

ありがとうございます!実現できました!