File size: 1,209 Bytes
fc0f7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import numpy as np

from ._metrics_engine import metric_by_group


def selection_rate(y_true, y_pred, *, pos_label=1, sample_weight=None):
    """Calculate the fraction of predicted labels matching the 'good' outcome.

    The argument `pos_label` specifies the 'good' outcome.
    """
    selected = (np.squeeze(np.asarray(y_pred)) == pos_label)
    s_w = np.ones(len(selected))
    if sample_weight is not None:
        s_w = np.squeeze(np.asarray(sample_weight))

    return np.dot(selected, s_w) / s_w.sum()


def group_selection_rate(y_true, y_pred, group_membership,
                         *, pos_label=1, sample_weight=None):
    """Wrap :func:`selection_rate` as a group metric.

    The arguments are the same, with the addition of the
    `group_membership` array.
    """
    def internal_sel_wrapper(y_true, y_pred, sample_weight=None):
        return selection_rate(y_true, y_pred, pos_label=pos_label, sample_weight=sample_weight)

    return metric_by_group(internal_sel_wrapper,
                           y_true, y_pred, group_membership,
                           sample_weight=sample_weight)