Пример #1
0
def test_classification_matrix_mismatch_in_class_names():
    targets = ["A"] * 2 + ["B"] * 3
    predictions = ["C"] * 2 + ["D"] * 3
    matrix = ClassificationMatrix(targets, predictions)
    assert matrix.tp("A") == 0
    assert matrix.tn("A") == 0
    assert matrix.fp("A") == 2
    assert matrix.fn("A") == 0

    assert matrix.tp("B") == 0
    assert matrix.tn("B") == 0
    assert matrix.fp("B") == 3
    assert matrix.fn("B") == 0

    assert matrix.tp("C") == 0
    assert matrix.tn("C") == 0
    assert matrix.fp("C") == 0
    assert matrix.fn("C") == 2

    assert matrix.tp("D") == 0
    assert matrix.tn("D") == 0
    assert matrix.fp("D") == 0
    assert matrix.fn("D") == 3

    assert matrix.accuracy() == 0

    assert matrix.precision("A") == 0
    assert matrix.recall("A") == 0
    assert matrix.sensitivity("A") == 0
    assert matrix.specificity("A") == 0
    assert matrix.f1("A") == 0
    assert matrix.matthews_corrcoef("A") == np.inf

    assert matrix.precision("C") == 0
    assert matrix.recall("C") == 0
    assert matrix.sensitivity("C") == 0
    assert matrix.specificity("C") == 0
    assert matrix.f1("C") == 0
    assert matrix.matthews_corrcoef("C") == np.inf
Пример #2
0
def test_classification_matrix_matthews_corrcoef():
    targets = np.random.randint(2, size=20)
    # Shuffle the middle part of the targets
    section = targets[5:15]
    np.random.shuffle(section)
    predictions = np.r_[targets[:5], section, targets[15:]]
    #
    matrix = ClassificationMatrix(targets, predictions)
    tp = matrix.tp()
    fp = matrix.fp()
    tn = matrix.tn()
    fn = matrix.fn()
    if (tp + fp) * (tp + fn) * (tn + fp) * (tn + fn) == 0:
        assert matrix.matthews_corrcoef() == np.inf
    else:
        assert matrix.matthews_corrcoef() == (
            (tp * tn - fp * fn) / ((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
        )

    # Check the zero denominator
    matrix = ClassificationMatrix([1, 1, 1, 0], [1, 1, 1, 1])
    assert matrix.matthews_corrcoef() == np.inf