毎回リファレンス確認しながら書いてるカスタムメトリクス。
ローカルにソース転がってたので、備忘録として挙げておく。
# サンプルコード
from sklearn.metrics import roc_auc_score
from sklearn.metrics import average_precision_score
def calc_accuracy(y_pred, y_true):
metric = np.mean(y_true == y_pred)
return 'accuracy', metric, True
def calc_precision(y_pred, y_true):
metric = precision_score(y_true, y_pred)
return 'precison', metric, True
def calc_recall(y_pred, y_true):
metric = recall_score(y_true, y_pred)
return 'recall', metric, True
def calc_fscore(fscores):
metric = np.max(fscores)
return 'fscore', metric, True
def calc_auc(y_pred, y_true):
metric = roc_auc_score(y_true, y_pred)
return 'auc', metric, True
def calc_prauc(y_pred, y_true):
metric = average_precision_score(y_true, y_pred)
return 'prauc', metric, True
# CV用評価関数算出メソッド
def acquire_custom_metircs(preds ,data):
precisions, recalls, thresholds = precision_recall_curve(data.get_label(), preds, pos_label=1)
thresholds = np.append(thresholds, 1)
fscores = 2 * precisions * recalls / (precisions + recalls)
threshold_max_fscore = thresholds[np.argmax(fscores)]
y_true = data.get_label()
y_pred = np.where(preds > 0.5, 1, 0)
return [
calc_accuracy(y_pred, y_true), # Accuracy
calc_precision(y_pred, y_true), # Precision
calc_recall(y_pred, y_true), # Recall
calc_fscore(fscores), # F1 score
calc_auc(y_pred, y_true), # ROC AUC
calc_prauc(y_pred, y_true) # PR AUC
]
# CV
histroy = lgb.cv(
best,
dtrain,
folds=StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=0),
feval=acquire_custom_metircs, # CVで使う評価関数に自作関数を指定
num_boost_round=cons_val_num_boost_round,
early_stopping_rounds=cons_val_early_stopping_rounds,
verbose_eval=cons_val_verbose_eval,
show_stdv=False,
seed=0
)