examples/classify_digits.py
def classify_digits():
    r"""Test that a nontrivial hierarchy leaf classification behaves as expected.

    We build the following class hierarchy along with data from the handwritten digits dataset:

            <ROOT>
           /      \
          A        B
         / \       |  \
        1   7      C   9
                 /   \
                3     8

    """
    class_hierarchy = {
        ROOT: ["A", "B"],
        "A": ["1", "7"],
        "B": ["C", "9"],
        "C": ["3", "8"],
    }
    base_estimator = make_pipeline(
        TruncatedSVD(n_components=24),
        svm.SVC(
            gamma=0.001,
            kernel="rbf",
            probability=True
        ),
    )
    clf = HierarchicalClassifier(
        base_estimator=base_estimator,
        class_hierarchy=class_hierarchy,
    )
    X, y = make_digits_dataset(
        targets=[1, 7, 3, 8, 9],
        as_str=False,
    )
    # cast the targets to strings so we have consistent typing of labels across hierarchy
    y = y.astype(str)

    X_train, X_test, y_train, y_test = train_test_split(
        X,
        y,
        test_size=0.2,
        random_state=RANDOM_STATE,
    )

    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    print("Classification Report:\n", classification_report(y_test, y_pred))

    # Demonstrate using our hierarchical metrics module with MLB wrapper
    with multi_labeled(y_test, y_pred, clf.graph_) as (y_test_, y_pred_, graph_):
        h_fbeta = h_fbeta_score(
            y_test_,
            y_pred_,
            graph_,
        )
        print("h_fbeta_score: ", h_fbeta)
r"""Test that a nontrivial hierarchy leaf classification behaves as expected.

    We build the following class hierarchy along with data from the handwritten digits dataset:

            <ROOT>
           /      \
          A        B
         / \       |  \
        1   7      C   9
                 /   \
                3     8

    
r"""Test that a nontrivial hierarchy leaf classification behaves as expected.
We build the following class hierarchy along with data from the handwritten digits dataset.


r " " " Test that a nontrivial hierarchy leaf classification behaves as expected . We build the following class hierarchy along with data from the handwritten digits dataset .
def classify_digits():
    class_hierarchy = {
        ROOT: ["A", "B"],
        "A": ["1", "7"],
        "B": ["C", "9"],
        "C": ["3", "8"],
    }
    base_estimator = make_pipeline(
        TruncatedSVD(n_components=24),
        svm.SVC(
            gamma=0.001,
            kernel="rbf",
            probability=True
        ),
    )
    clf = HierarchicalClassifier(
        base_estimator=base_estimator,
        class_hierarchy=class_hierarchy,
    )
    X, y = make_digits_dataset(
        targets=[1, 7, 3, 8, 9],
        as_str=False,
    )
    y = y.astype(str)
    X_train, X_test, y_train, y_test = train_test_split(
        X,
        y,
        test_size=0.2,
        random_state=RANDOM_STATE,
    )
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    print("Classification Report:\n", classification_report(y_test, y_pred))
    with multi_labeled(y_test, y_pred, clf.graph_) as (y_test_, y_pred_, graph_):
        h_fbeta = h_fbeta_score(
            y_test_,
            y_pred_,
            graph_,
        )
        print("h_fbeta_score: ", h_fbeta)
def classify_digits ( ) : class_hierarchy = { ROOT : [ "A" , "B" ] , "A" : [ "1" , "7" ] , "B" : [ "C" , "9" ] , "C" : [ "3" , "8" ] , } base_estimator = make_pipeline ( TruncatedSVD ( n_components = 24 ) , svm . SVC ( gamma = 0.001 , kernel = "rbf" , probability = True ) , ) clf = HierarchicalClassifier ( base_estimator = base_estimator , class_hierarchy = class_hierarchy , ) X , y = make_digits_dataset ( targets = [ 1 , 7 , 3 , 8 , 9 ] , as_str = False , ) y = y . astype ( str ) X_train , X_test , y_train , y_test = train_test_split ( X , y , test_size = 0.2 , random_state = RANDOM_STATE , ) clf . fit ( X_train , y_train ) y_pred = clf . predict ( X_test ) print ( "Classification Report:\n" , classification_report ( y_test , y_pred ) ) with multi_labeled ( y_test , y_pred , clf . graph_ ) as ( y_test_ , y_pred_ , graph_ ) : h_fbeta = h_fbeta_score ( y_test_ , y_pred_ , graph_ , ) print ( "h_fbeta_score: " , h_fbeta )
r"""Test that a nontrivial hierarchy leaf classification behaves as expected.
r " " " Test that a nontrivial hierarchy leaf classification behaves as expected .
r"""Test that a nontrivial hierarchy leaf classification behaves as expected.

    We build the following class hierarchy along with data from the handwritten digits dataset:

            <ROOT>
           /      \
          A        B
         / \       |  \
        1   7      C   9
                 /   \
                3     8

    """ # cast the targets to strings so we have consistent typing of labels across hierarchy # Demonstrate using our hierarchical metrics module with MLB wrapper