def test_pandas_confusion_cm_stats_animals():
    y_true = ['rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit', 'rabbit', 'cat', 'dog', 'rabbit']
    y_pred = ['cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'cat', 'rabbit', 'rabbit']
    print("y_true: %s" % y_true)
    print("y_pred: %s" % y_pred)
    cm = ConfusionMatrix(y_true, y_pred)
    assert isinstance(cm.stats(), OrderedDict)
    assert cm.population == len(y_true)  # 12
    cm.print_stats()
    cm_stats = cm.stats()  # noqa
    assert cm.binarize("cat").TP == cm.get("cat")  # cm.get("cat", "cat")
    assert cm.binarize("cat").TP == 3
    assert cm.binarize("dog").TP == cm.get("dog")  # 1
    assert cm.binarize("rabbit").TP == cm.get("rabbit")  # 3
def test_pandas_confusion_cm_binarize():
    y_true = ['rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit', 'rabbit', 'cat', 'dog', 'rabbit']
    y_pred = ['cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'cat', 'rabbit', 'rabbit']

    cm = ConfusionMatrix(y_true, y_pred)
    print("Confusion matrix:\n%s" % cm)

    select = ['cat', 'dog']
    print("Binarize with %s" % select)
    binary_cm = cm.binarize(select)

    print("Binary confusion matrix:\n%s" % binary_cm)

    assert cm.sum() == binary_cm.sum()
def main(save, show):
    basepath = os.path.dirname(__file__)

    # y_true = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
    # y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    # y_true = [2, 0, 2, 2, 0, 1]
    # y_pred = [0, 0, 2, 2, 0, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    y_true = ['rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit', 'rabbit', 'cat', 'dog', 'rabbit']
    y_pred = ['cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'cat', 'rabbit', 'rabbit']
    cm = ConfusionMatrix(y_true, y_pred)

    # y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    # y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    # >>> cm(y_true, y_pred, labels=["ant", "bird", "cat"])
    # array([[2, 0, 0],
    #       [0, 0, 1],
    #       [1, 0, 2]])
    # cm = ConfusionMatrix(y_true, y_pred)

    print("Confusion matrix:\n%s" % cm)
    df = cm.to_dataframe()
    print(df)
    print(df.dtypes)

    cm.plot()
    filename = 'cm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.plot(normalized=True)
    filename = 'cm_norm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.print_stats()
    print(cm.classification_report)

    print("sklearn confusion_matrix:\n%s" % confusion_matrix(y_true, y_pred))
    print(classification_report(y_true, y_pred))

    # stat = 'precision'
    # print(cm._avg_stat(stat))
    # print(cm.ACC)

    # import seaborn as sns
    # cm.plot(normalized=True, backend=Backend.Seaborn)
    # sns.plt.show()

    print("Binarize a confusion matrix")
    y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    cm = ConfusionMatrix(y_true, y_pred)
    print(cm)
    binary_cm = cm.binarize(['ant', 'cat'])
    # A bird is not a "land_animal"
    print(binary_cm)
def main(save, show):
    basepath = os.path.dirname(__file__)

    # y_true = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
    # y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    # y_true = [2, 0, 2, 2, 0, 1]
    # y_pred = [0, 0, 2, 2, 0, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    y_true = [
        'rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit',
        'rabbit', 'cat', 'dog', 'rabbit'
    ]
    y_pred = [
        'cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit',
        'cat', 'rabbit', 'rabbit'
    ]
    cm = ConfusionMatrix(y_true, y_pred)

    # y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    # y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    # >>> cm(y_true, y_pred, labels=["ant", "bird", "cat"])
    # array([[2, 0, 0],
    #       [0, 0, 1],
    #       [1, 0, 2]])
    # cm = ConfusionMatrix(y_true, y_pred)

    print("Confusion matrix:\n%s" % cm)
    df = cm.to_dataframe()
    print(df)
    print(df.dtypes)

    cm.plot()
    filename = 'cm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.plot(normalized=True)
    filename = 'cm_norm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.print_stats()
    print(cm.classification_report)

    print("sklearn confusion_matrix:\n%s" % confusion_matrix(y_true, y_pred))
    print(classification_report(y_true, y_pred))

    # stat = 'precision'
    # print(cm._avg_stat(stat))
    # print(cm.ACC)

    # import seaborn as sns
    # cm.plot(normalized=True, backend=Backend.Seaborn)
    # sns.plt.show()

    print("Binarize a confusion matrix")
    y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    cm = ConfusionMatrix(y_true, y_pred)
    print(cm)
    binary_cm = cm.binarize(['ant', 'cat'])
    # A bird is not a "land_animal"
    print(binary_cm)