Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

New metrics are added for imbalanced data #480

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
New metrics are added for imbalanced data
Mathews Correlation Coefficient, Balanced Accuracy, Brier Score and log-loss metrics, which are used in case of imbalanced data, are added.
  • Loading branch information
mcavs authored Feb 20, 2022
commit 9947f549f869a05e00eb16d27a4ec2e1779e6480
31 changes: 26 additions & 5 deletions R/model_performance.R
Original file line number Diff line number Diff line change
@@ -97,11 +97,15 @@ model_performance <- function(explainer, ..., cutoff = 0.5) {
fn = sum((observed == 1) * (predicted < cutoff))

measures <- list(
recall = model_performance_recall(tp, fp, tn, fn),
precision = model_performance_precision(tp, fp, tn, fn),
f1 = model_performance_f1(tp, fp, tn, fn),
accuracy = model_performance_accuracy(tp, fp, tn, fn),
auc = model_performance_auc(predicted, observed)
recall = model_performance_recall(tp, fp, tn, fn),
precision = model_performance_precision(tp, fp, tn, fn),
f1 = model_performance_f1(tp, fp, tn, fn),
accuracy = model_performance_accuracy(tp, fp, tn, fn),
auc = model_performance_auc(predicted, observed)
mcc = model_performance_mcc(tp, fp, tn, fn),
brier_score = model_performance_brier(predicted, observed),
log_loss = model_performance_logloss(predicted, observed),
balanced_accuracy = model_performance_bacc(tp, fp, tn, fn)
)
} else if (type == "multiclass") {
measures <- list(
@@ -167,6 +171,23 @@ model_performance_accuracy <- function(tp, fp, tn, fn) {
(tp + tn)/(tp + fp + tn + fn)
}

model_performance_bacc <- function(tp, fp, tn, fn) {
((tp / (tp + fn)) + (tn / (tn + fp))) / 2
}

model_performance_logloss <- function(predicted, observed) {
-mean(observed * log(predicted) + (1 - observed) * log(1 - predicted))
}

model_performance_brier <- function(predicted, observed) {
mean((predicted - observed) ^ 2)
}

model_performance_mcc <- function(tp, fp, tn, fn) {
((tp * tn) - (fp * fn)) / sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
}


model_performance_macro_f1 <- function(predicted, observed) {
predicted_vectorized <- turn_probs_into_vector(predicted)
confusion_matrixes <- calculate_confusion_matrixes(predicted_vectorized, observed)