-
Notifications
You must be signed in to change notification settings - Fork 1k
/
test_validation.py
49 lines (40 loc) · 1.51 KB
/
test_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Module for testing the validation module.
"""
import os
from surprise import Dataset, model_selection as ms, NormalPredictor, Reader
def test_cross_validate(toy_data):
# First test with a specified CV iterator.
current_dir = os.path.dirname(os.path.realpath(__file__))
folds_files = [(current_dir + "/custom_train", current_dir + "/custom_test")]
reader = Reader(
line_format="user item rating", sep=" ", skip_lines=3, rating_scale=(1, 5)
)
data = Dataset.load_from_folds(folds_files=folds_files, reader=reader)
algo = NormalPredictor()
pkf = ms.PredefinedKFold()
ret = ms.cross_validate(algo, data, measures=["rmse", "mae"], cv=pkf, verbose=1)
# Basically just test that keys (dont) exist as they should
assert len(ret["test_rmse"]) == 1
assert len(ret["test_mae"]) == 1
assert len(ret["fit_time"]) == 1
assert len(ret["test_time"]) == 1
assert "test_fcp" not in ret
assert "train_rmse" not in ret
assert "train_mae" not in ret
# Test that 5 fold CV is used when cv=None
# Also check that train_* key exist when return_train_measures is True.
ret = ms.cross_validate(
algo,
toy_data,
measures=["rmse", "mae"],
cv=None,
return_train_measures=True,
verbose=True,
)
assert len(ret["test_rmse"]) == 5
assert len(ret["test_mae"]) == 5
assert len(ret["fit_time"]) == 5
assert len(ret["test_time"]) == 5
assert len(ret["train_rmse"]) == 5
assert len(ret["train_mae"]) == 5