Accuracy rewards a model for guessing the majority class. If 95 percent of rows are negative, a model that always says negative scores 95 percent and catches nothing. Balanced accuracy averages the recall of each class, so every class counts equally no matter how many rows it has.
Implement balanced_accuracy(y_true, y_pred). For each class present in y_true, compute the fraction of its rows that were predicted correctly, then return the mean of those fractions.
y_true and y_pred have the same length, otherwise raise ValueError.ValueError.y_pred are ignored.Open one at a time. Each gives away a little more than the last.
Loop over np.unique(y_true). For each class, select the rows where y_true == c first.
Recall for class c is np.mean(y_pred[mask] == c), where mask selects those rows.
No results yet
Run your code to check it against the examples, or submit to run every test.