Beispiel #1
0
def test_recall_array():
    prec = lm.recall(np.array([1, 3]), np.array([1, 3]))
    assert prec == approx(1.0)

    prec = lm.recall(np.array([1, 2, 3, 4]), np.array([1, 3, 5, 7]))
    assert prec == approx(0.5)

    prec = lm.recall(np.array([1, 2, 3, 4]), np.arange(4, 9, 1, 'u4'))
    assert prec == approx(0.2)
Beispiel #2
0
def test_recall_series_index():
    prec = lm.recall(pd.Series([1, 3]), pd.Index([1, 3]))
    assert prec == approx(1.0)

    prec = lm.recall(pd.Series([1, 2, 3, 4]), pd.Index([1, 3, 5, 7]))
    assert prec == approx(0.5)

    prec = lm.recall(pd.Series([1, 2, 3, 4]), pd.Index(range(4, 9)))
    assert prec == approx(0.2)
Beispiel #3
0
def test_recall_bulk_k(demo_recs):
    "bulk and normal match"
    train, test, recs = demo_recs
    assert test['user'].value_counts().max() > 5

    rla = topn.RecListAnalysis()
    rla.add_metric(recall, name='rk', k=5)
    rla.add_metric(recall)
    # metric without the bulk capabilities
    rla.add_metric(lambda *a, **k: recall(*a, **k), name='ind_rk', k=5)
    rla.add_metric(lambda *a: recall(*a), name='ind_r')
    res = rla.compute(recs, test)

    print(res)
    _log.info('recall mismatches:\n%s', res[res.recall != res.ind_r])

    assert res.recall.values == approx(res.ind_r.values)
    assert res.rk.values == approx(res.ind_rk.values)
Beispiel #4
0
def test_pr_bulk_match(demo_recs, drop_rating):
    "bulk and normal match"
    train, test, recs = demo_recs
    if drop_rating:
        test = test[['user', 'item']]

    rla = topn.RecListAnalysis()
    rla.add_metric(precision)
    rla.add_metric(recall)
    # metric without the bulk capabilities
    rla.add_metric(lambda *a: precision(*a), name='ind_p')
    rla.add_metric(lambda *a: recall(*a), name='ind_r')
    res = rla.compute(recs, test)

    print(res)
    _log.info('precision mismatches:\n%s', res[res.precision != res.ind_p])
    _log.info('recall mismatches:\n%s', res[res.recall != res.ind_r])

    assert res.precision.values == approx(res.ind_p.values)
    assert res.recall.values == approx(res.ind_r.values)
Beispiel #5
0
def test_recall_simple_cases():
    prec = lm.recall([1, 3], [1, 3])
    assert prec == approx(1.0)

    prec = lm.recall([1], [1, 3])
    assert prec == approx(0.5)

    prec = lm.recall([1, 2, 3, 4], [1, 3])
    assert prec == approx(1.0)

    prec = lm.recall([1, 2, 3, 4], [1, 3, 5])
    assert prec == approx(2.0 / 3)

    prec = lm.recall([1, 2, 3, 4], range(5, 10))
    assert prec == approx(0.0)

    prec = lm.recall([1, 2, 3, 4], range(4, 9))
    assert prec == approx(0.2)
Beispiel #6
0
def _test_recall(items, rel, **kwargs):
    recs = pd.DataFrame({'item': items})
    truth = pd.DataFrame({'item': rel}).set_index('item')
    return recall(recs, truth, **kwargs)
Beispiel #7
0
def test_recall_empty_zero():
    prec = lm.recall([], [1, 3])
    assert prec == approx(0)
Beispiel #8
0
def test_recall_series_set():
    prec = lm.recall(pd.Series([1, 2, 3, 4]), [1, 3, 5, 7])
    assert prec == approx(0.5)

    prec = lm.recall(pd.Series([1, 2, 3, 4]), range(4, 9))
    assert prec == approx(0.2)
Beispiel #9
0
def test_recall_norel_na():
    prec = lm.recall([1, 3], [])
    assert np.isnan(prec)