Ejemplo n.º 1
0
def instantiate(opts, implicit):
    nnbrs, smin = opts
    if implicit:
        return ItemItem(nnbrs, min_sim=smin, save_nbrs=10000,
                        aggregate='sum', center=False)
    else:
        return ItemItem(nnbrs, min_sim=smin, save_nbrs=10000)
Ejemplo n.º 2
0
def test_store_iknn(store_cls):
    algo = ItemItem(10)
    algo = Recommender.adapt(algo)
    algo.fit(lktu.ml_test.ratings)

    with store_cls() as store:
        k = store.put_model(algo)
        client = store.client()

        a2 = client.get_model(k)
        assert a2 is not algo
        del a2
Ejemplo n.º 3
0
def test_adv_fill_users():
    rla = topn.RecListAnalysis()
    rla.add_metric(topn.precision)
    rla.add_metric(topn.recall)

    a_uu = UserUser(30, min_nbrs=10)
    a_uu = Recommender.adapt(a_uu)
    a_ii = ItemItem(20, min_nbrs=4)
    a_ii = Recommender.adapt(a_ii)

    splits = xf.sample_users(ml_test.ratings, 2, 50, xf.SampleN(5))
    all_recs = {}
    all_test = {}
    for i, (train, test) in enumerate(splits):
        a_uu.fit(train)
        rec_users = test['user'].sample(50).unique()
        all_recs[(i + 1, 'UU')] = batch.recommend(a_uu, rec_users, 25)

        a_ii.fit(train)
        rec_users = test['user'].sample(50).unique()
        all_recs[(i + 1, 'II')] = batch.recommend(a_ii, rec_users, 25)
        all_test[i + 1] = test

    recs = pd.concat(all_recs, names=['part', 'algo'])
    recs.reset_index(['part', 'algo'], inplace=True)
    recs.reset_index(drop=True, inplace=True)

    test = pd.concat(all_test, names=['part'])
    test.reset_index(['part'], inplace=True)
    test.reset_index(drop=True, inplace=True)

    scores = rla.compute(recs, test, include_missing=True)
    inames = scores.index.names
    scores.sort_index(inplace=True)
    assert len(scores) == 50 * 4
    assert all(scores['ntruth'] == 5)
    assert scores['recall'].isna().sum() > 0
    _log.info('scores:\n%s', scores)

    ucounts = scores.reset_index().groupby('algo')['user'].agg(
        ['count', 'nunique'])
    assert all(ucounts['count'] == 100)
    assert all(ucounts['nunique'] == 100)

    mscores = rla.compute(recs, test)
    mscores = mscores.reset_index().set_index(inames)
    mscores.sort_index(inplace=True)
    assert len(mscores) < len(scores)
    _log.info('mscores:\n%s', mscores)

    recall = scores.loc[scores['recall'].notna(), 'recall'].copy()
    recall, mrecall = recall.align(mscores['recall'])
    assert all(recall == mrecall)
Ejemplo n.º 4
0
 def __init__(self,
              nnbrs,
              min_nbrs=1,
              min_sim=1e-06,
              save_nbrs=None,
              center=True,
              aggregate='weighted-average'):
     algo = ItemItem(nnbrs, min_nbrs, min_sim, save_nbrs, center, aggregate)
     fallback = Bias()
     Fallback.__init__(self, [algo, fallback])
Ejemplo n.º 5
0
def default(implicit):
    if implicit:
        return ItemItem(20, save_nbrs=10000,
                        aggregate='sum', center=False)
    else:
        return ItemItem(20, save_nbrs=10000)
 def __str__(self):
     return ItemItem.__str__(self)