def test__recommend__good__train(): """test the recommend() method on the training users""" model = RankFM(factors=2) model.fit(intx_train_pd_int) recs = model.recommend(train_users, n_items=3) klass = isinstance(recs, pd.DataFrame) shape = recs.shape == (3, 3) index = np.array_equal(recs.index.values, train_users) items = recs.isin(intx_train_pd_int['item_id'].values).all().all() assert klass and shape and index and items
def test__recommend__good__valid__nan(): """test the recommend() method on a disjoint set of validation users""" model = RankFM(factors=2) model.fit(intx_train_pd_int) recs = model.recommend(valid_users, n_items=3, cold_start='nan') klass = isinstance(recs, pd.DataFrame) shape = recs.shape == (4, 3) index = np.array_equal(sorted(recs.index.values), sorted(valid_users)) items = recs.dropna().isin(intx_train_pd_int['item_id'].values).all().all() new_users = list(set(valid_users) - set(train_users)) nmiss = recs.loc[new_users].isnull().all().all() assert klass and shape and index and items and nmiss
def test__recommend__good__valid__drop(): """test the recommend() method on a disjoint set of validation users""" model = RankFM(factors=2) model.fit(intx_train_pd_int) recs = model.recommend(valid_users, n_items=3, cold_start='drop') klass = isinstance(recs, pd.DataFrame) shape = recs.shape == (2, 3) index = np.isin(recs.index.values, valid_users).all() items = recs.dropna().isin(intx_train_pd_int['item_id'].values).all().all() same_users = list(set(valid_users) & set(train_users)) match_users = np.array_equal(sorted(same_users), sorted(recs.index.values)) assert klass and shape and index and items and match_users
def test__recommend__good__train__filter(): """test the recommend() method on the training users but filter previous items""" model = RankFM(factors=2) model.fit(intx_train_pd_int) recs = model.recommend(train_users, n_items=3, filter_previous=True) klass = isinstance(recs, pd.DataFrame) shape = recs.shape == (3, 3) index = np.array_equal(recs.index.values, train_users) items = recs.isin(intx_train_pd_int['item_id'].values).all().all() recs_long = recs.stack().reset_index().drop('level_1', axis=1) recs_long.columns = ['user_id', 'item_id'] intersect = pd.merge(intx_train_pd_int, recs_long, on=['user_id', 'item_id'], how='inner').empty assert klass and shape and index and items and intersect