(cm, pos_indices=None, average='micro',
                                  beta=1)
Precision, Recall and F1 from the confusion matrix
    Parameters
    ----------
    cm : tf.Tensor of type tf.int32, of shape (num_classes, num_classes)
        The streaming confusion matrix.
    pos_indices : list of int, optional
        The indices of the positive classes
    beta : int, optional
        Weight of precision in harmonic mean
    average : str, optional
        'micro', 'macro' or 'weighted'
Precision, Recall and F1 from the confusion matrix
    Parameters
    ----------
    cm : tf.Tensor of type tf.int32, of shape (num_classes, num_classes)
        The streaming confusion matrix.
    pos_indices : list of int, optional
        The indices of the positive classes
    beta : int, optional
        Weight of precision in harmonic mean
    average : str, optional
        'micro', 'macro' or 'weighted'
Precision Recall and F1 from the confusion matrix Parameters ---------- cm : tf . Tensor of type tf . int32 of shape ( num_classes num_classes ) The streaming confusion matrix . pos_indices : list of int optional The indices of the positive classes beta : int optional Weight of precision in harmonic mean average : str optional micro macro or weighted
def metrics_from_confusion_matrix(cm, pos_indices=None, average='micro',
                                  beta=1):
    """Precision, Recall and F1 from the confusion matrix
    Parameters
    ----------
    cm : tf.Tensor of type tf.int32, of shape (num_classes, num_classes)
        The streaming confusion matrix.
    pos_indices : list of int, optional
        The indices of the positive classes
    beta : int, optional
        Weight of precision in harmonic mean
    average : str, optional
        'micro', 'macro' or 'weighted'
    """
    num_classes = cm.shape[0]
    if pos_indices is None:
        pos_indices = [i for i in range(num_classes)]

    if average == 'micro':
        return pr_re_fbeta(cm, pos_indices, beta)
    elif average in {'macro', 'weighted'}:
        precisions, recalls, fbetas, n_golds = [], [], [], []
        for idx in pos_indices:
            pr, re, fbeta = pr_re_fbeta(cm, [idx], beta)
            precisions.append(pr)
            recalls.append(re)
            fbetas.append(fbeta)
            cm_mask = np.zeros([num_classes, num_classes])
            cm_mask[idx, :] = 1
            n_golds.append(tf.to_float(tf.reduce_sum(cm * cm_mask)))

        if average == 'macro':
            pr = tf.reduce_mean(precisions)
            re = tf.reduce_mean(recalls)
            fbeta = tf.reduce_mean(fbetas)
            return pr, re, fbeta
        if average == 'weighted':
            n_gold = tf.reduce_sum(n_golds)
            pr_sum = sum(p * n for p, n in zip(precisions, n_golds))
            pr = safe_div(pr_sum, n_gold)
            re_sum = sum(r * n for r, n in zip(recalls, n_golds))
            re = safe_div(re_sum, n_gold)
            fbeta_sum = sum(f * n for f, n in zip(fbetas, n_golds))
            fbeta = safe_div(fbeta_sum, n_gold)
            return pr, re, fbeta

    else:
        raise NotImplementedError()
def metrics_from_confusion_matrix ( cm , pos_indices = None , average = 'micro' , beta = 1 ) : num_classes = cm . shape [ 0 ] if pos_indices is None : pos_indices = [ i for i in range ( num_classes ) ] if average == 'micro' : return pr_re_fbeta ( cm , pos_indices , beta ) elif average in { 'macro' , 'weighted' } : precisions , recalls , fbetas , n_golds = [ ] , [ ] , [ ] , [ ] for idx in pos_indices : pr , re , fbeta = pr_re_fbeta ( cm , [ idx ] , beta ) precisions . append ( pr ) recalls . append ( re ) fbetas . append ( fbeta ) cm_mask = np . zeros ( [ num_classes , num_classes ] ) cm_mask [ idx , : ] = 1 n_golds . append ( tf . to_float ( tf . reduce_sum ( cm * cm_mask ) ) ) if average == 'macro' : pr = tf . reduce_mean ( precisions ) re = tf . reduce_mean ( recalls ) fbeta = tf . reduce_mean ( fbetas ) return pr , re , fbeta if average == 'weighted' : n_gold = tf . reduce_sum ( n_golds ) pr_sum = sum ( p * n for p , n in zip ( precisions , n_golds ) ) pr = safe_div ( pr_sum , n_gold ) re_sum = sum ( r * n for r , n in zip ( recalls , n_golds ) ) re = safe_div ( re_sum , n_gold ) fbeta_sum = sum ( f * n for f , n in zip ( fbetas , n_golds ) ) fbeta = safe_div ( fbeta_sum , n_gold ) return pr , re , fbeta else : raise NotImplementedError ( )