# -*- coding: utf-8 -*-

"""
Example to train and evaluate a model with given data

@author: Quoc-Tuan Truong <tuantq.vnu@gmail.com>
"""

from cornac.data import Reader
from cornac.eval_methods import BaseMethod
from cornac.models import MF
from cornac.metrics import MAE, RMSE
from cornac.utils import cache

# Download MovieLens 100K provided training and test splits
reader = Reader()
train_data = reader.read(cache(url='http://files.grouplens.org/datasets/movielens/ml-100k/u1.base'))
test_data = reader.read(cache(url='http://files.grouplens.org/datasets/movielens/ml-100k/u1.test'))

eval_method = BaseMethod.from_splits(train_data=train_data, test_data=test_data,
                                     exclude_unknowns=False, verbose=True)

mf = MF(k=10, max_iter=25, learning_rate=0.01, lambda_reg=0.02,
        use_bias=True, early_stop=True, verbose=True)

# Evaluation
result = eval_method.evaluate(model=mf, metrics=[MAE(), RMSE()], user_based=True)
print(result)

[((379, 387), 'cornac.data.Reader', 'Reader', ([], {}), '()\n', (385, 387), False, 'from cornac.data import Reader\n'), ((604, 712), 'cornac.eval_methods.BaseMethod.from_splits', 'BaseMethod.from_splits', ([], {'train_data': 'train_data', 'test_data': 'test_data', 'exclude_unknowns': '(False)', 'verbose': '(True)'}), '(train_data=train_data, test_data=test_data,\n    exclude_unknowns=False, verbose=True)\n', (626, 712), False, 'from cornac.eval_methods import BaseMethod\n'), ((752, 860), 'cornac.models.MF', 'MF', ([], {'k': '(10)', 'max_iter': '(25)', 'learning_rate': '(0.01)', 'lambda_reg': '(0.02)', 'use_bias': '(True)', 'early_stop': '(True)', 'verbose': '(True)'}), '(k=10, max_iter=25, learning_rate=0.01, lambda_reg=0.02, use_bias=True,\n    early_stop=True, verbose=True)\n', (754, 860), False, 'from cornac.models import MF\n'), ((413, 487), 'cornac.utils.cache', 'cache', ([], {'url': '"""http://files.grouplens.org/datasets/movielens/ml-100k/u1.base"""'}), "(url='http://files.grouplens.org/datasets/movielens/ml-100k/u1.base')\n", (418, 487), False, 'from cornac.utils import cache\n'), ((513, 587), 'cornac.utils.cache', 'cache', ([], {'url': '"""http://files.grouplens.org/datasets/movielens/ml-100k/u1.test"""'}), "(url='http://files.grouplens.org/datasets/movielens/ml-100k/u1.test')\n", (518, 587), False, 'from cornac.utils import cache\n'), ((928, 933), 'cornac.metrics.MAE', 'MAE', ([], {}), '()\n', (931, 933), False, 'from cornac.metrics import MAE, RMSE\n'), ((935, 941), 'cornac.metrics.RMSE', 'RMSE', ([], {}), '()\n', (939, 941), False, 'from cornac.metrics import MAE, RMSE\n')]