sklearn_hierarchical_classification/metrics.py
def h_precision_score(y_true, y_pred, class_hierarchy, root=ROOT):
    """
    Calculate the micro-averaged hierarchical precision ("hR") metric based on
    given set of true class labels and predicated class labels, and the
    class hierarchy graph.

    Note that the format expected here for `y_true` and `y_pred` is a
    binary multi-label matrix format, e.g. as can be generated by scikit-learn's
    MultiLabelBinarizer.

    For motivation and definition details, see:

        Functional Annotation of Genes Using Hierarchical Text
        Categorization, Kiritchenko et al 2008

        http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.68.5824&rep=rep1&type=pdf

    Parameters
    ----------
    y_true : array-like, shape = [n_samples, n_classes].
        Ground truth multi-class targets.

    y_pred : array-like, shape = [n_samples, n_classes].
        Predicted multi-class targets.

    class_hierarchy : the class hierarchy graph, given as a `networkx.DiGraph` instance
        Node ids must be integer and correspond to the indices into the y_true / y_pred matrices.

    Returns
    -------
    hP : float
        The computed (micro-averaged) hierarchical precision score.

    """
    y_true_ = fill_ancestors(y_true, graph=class_hierarchy, root=root)
    y_pred_ = fill_ancestors(y_pred, graph=class_hierarchy, root=root)

    ix = np.where((y_true_ != 0) & (y_pred_ != 0))

    true_positives = len(ix[0])
    all_results = np.count_nonzero(y_pred_)

    return true_positives / all_results

    Calculate the micro-averaged hierarchical precision ("hR") metric based on
    given set of true class labels and predicated class labels, and the
    class hierarchy graph.

    Note that the format expected here for `y_true` and `y_pred` is a
    binary multi-label matrix format, e.g. as can be generated by scikit-learn's
    MultiLabelBinarizer.

    For motivation and definition details, see:

        Functional Annotation of Genes Using Hierarchical Text
        Categorization, Kiritchenko et al 2008

        http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.68.5824&rep=rep1&type=pdf

    Parameters
    ----------
    y_true : array-like, shape = [n_samples, n_classes].
        Ground truth multi-class targets.

    y_pred : array-like, shape = [n_samples, n_classes].
        Predicted multi-class targets.

    class_hierarchy : the class hierarchy graph, given as a `networkx.DiGraph` instance
        Node ids must be integer and correspond to the indices into the y_true / y_pred matrices.

    Returns
    -------
    hP : float
        The computed (micro-averaged) hierarchical precision score.

    
Calculate the micro-averaged hierarchical precision ("hR") metric based on
given set of true class labels and predicated class labels, and the
class hierarchy graph.

Note that the format expected here for `y_true` and `y_pred` is a
binary multi-label matrix format, e.g. as can be generated by scikit-learn's
MultiLabelBinarizer.

For motivation and definition details, see.

Functional Annotation of Genes Using Hierarchical Text
Categorization, Kiritchenko et al 2008



Parameters





class_hierarchy : the class hierarchy graph, given as a `networkx.DiGraph` instance
Node ids must be integer and correspond to the indices into the y_true / y_pred matrices.

Returns

hP : float
The computed (micro-averaged) hierarchical precision score.
Calculate the micro - averaged hierarchical precision ( " hR " ) metric based on given set of true class labels and predicated class labels and the class hierarchy graph . Note that the format expected here for ` y_true ` and ` y_pred ` is a binary multi - label matrix format e . g . as can be generated by scikit - learn ' s MultiLabelBinarizer . For motivation and definition details see . Functional Annotation of Genes Using Hierarchical Text Categorization Kiritchenko et al 2008 Parameters class_hierarchy : the class hierarchy graph given as a ` networkx . DiGraph ` instance Node ids must be integer and correspond to the indices into the y_true / y_pred matrices . Returns hP : float The computed ( micro - averaged ) hierarchical precision score .
def h_precision_score(y_true, y_pred, class_hierarchy, root=ROOT):
    y_true_ = fill_ancestors(y_true, graph=class_hierarchy, root=root)
    y_pred_ = fill_ancestors(y_pred, graph=class_hierarchy, root=root)
    ix = np.where((y_true_ != 0) & (y_pred_ != 0))
    true_positives = len(ix[0])
    all_results = np.count_nonzero(y_pred_)
    return true_positives / all_results
def h_precision_score ( y_true , y_pred , class_hierarchy , root = ROOT ) : y_true_ = fill_ancestors ( y_true , graph = class_hierarchy , root = root ) y_pred_ = fill_ancestors ( y_pred , graph = class_hierarchy , root = root ) ix = np . where ( ( y_true_ != 0 ) & ( y_pred_ != 0 ) ) true_positives = len ( ix [ 0 ] ) all_results = np . count_nonzero ( y_pred_ ) return true_positives / all_results
Calculate the micro-averaged hierarchical precision ("hR") metric based on
given set of true class labels and predicated class labels, and the
class hierarchy graph.
Calculate the micro - averaged hierarchical precision ( " hR " ) metric based on given set of true class labels and predicated class labels and the class hierarchy graph .
"""
    Calculate the micro-averaged hierarchical precision ("hR") metric based on
    given set of true class labels and predicated class labels, and the
    class hierarchy graph.

    Note that the format expected here for `y_true` and `y_pred` is a
    binary multi-label matrix format, e.g. as can be generated by scikit-learn's
    MultiLabelBinarizer.

    For motivation and definition details, see:

        Functional Annotation of Genes Using Hierarchical Text
        Categorization, Kiritchenko et al 2008

        http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.68.5824&rep=rep1&type=pdf

    Parameters
    ----------
    y_true : array-like, shape = [n_samples, n_classes].
        Ground truth multi-class targets.

    y_pred : array-like, shape = [n_samples, n_classes].
        Predicted multi-class targets.

    class_hierarchy : the class hierarchy graph, given as a `networkx.DiGraph` instance
        Node ids must be integer and correspond to the indices into the y_true / y_pred matrices.

    Returns
    -------
    hP : float
        The computed (micro-averaged) hierarchical precision score.

    """
y_true
y_pred
class_hierarchy
root