def test_load_form_df(): """Ensure reading dataset from pandas dataframe is OK.""" # DF creation. ratings_dict = { 'itemID': [1, 1, 1, 2, 2], 'userID': [9, 32, 2, 45, '10000'], 'rating': [3, 2, 4, 3, 1] } df = pd.DataFrame(ratings_dict) reader = Reader(rating_scale=(1, 5)) data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], reader) # Assert split and folds can be used without problems with pytest.warns(UserWarning): data.split(2) assert sum(1 for _ in data.folds()) == 2 # assert users and items are correctly mapped trainset = data.build_full_trainset() assert trainset.knows_user(trainset.to_inner_uid(9)) assert trainset.knows_user(trainset.to_inner_uid('10000')) assert trainset.knows_item(trainset.to_inner_iid(2)) # assert r(9, 1) = 3 and r(2, 1) = 4 uid9 = trainset.to_inner_uid(9) uid2 = trainset.to_inner_uid(2) iid1 = trainset.to_inner_iid(1) assert trainset.ur[uid9] == [(iid1, 3)] assert trainset.ur[uid2] == [(iid1, 4)] # assert at least rating file or dataframe must be specified with pytest.raises(ValueError): data = Dataset.load_from_df(None, None) # mess up the column ordering and assert that users are not correctly # mapped data = Dataset.load_from_df(df[['rating', 'itemID', 'userID']], reader) trainset = data.build_full_trainset() with pytest.raises(ValueError): trainset.to_inner_uid('10000')
def test_build_anti_testset(): ratings_dict = { 'itemID': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'userID': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'rating': [1, 2, 3, 4, 5, 6, 7, 8, 9] } df = pd.DataFrame(ratings_dict) reader = Reader(rating_scale=(1, 5)) data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], reader) with pytest.warns(UserWarning): data.split(2) trainset, __testset = next(data.folds()) # fill with some specific value for fillvalue in (0, 42., -1): anti = trainset.build_anti_testset(fill=fillvalue) for (u, i, r) in anti: assert r == fillvalue # fill with global_mean anti = trainset.build_anti_testset(fill=None) for (u, i, r) in anti: assert r == trainset.global_mean expect = trainset.n_users * trainset.n_items assert trainset.n_ratings + len(anti) == expect