コード例 #1
0
ファイル: test_rating.py プロジェクト: wubinzzu/cornac
    def test_rmse(self):
        rmse = RMSE()

        self.assertEqual(rmse.type, 'rating')
        self.assertEqual(rmse.name, 'RMSE')

        self.assertEqual(0, rmse.compute(np.asarray([0]), np.asarray([0])))
        self.assertEqual(1, rmse.compute(np.asarray([0, 1]), np.asarray([1, 0])))
        self.assertEqual(2, rmse.compute(np.asarray([0, 1]), np.asarray([2, 3]), np.asarray([1, 3])))
コード例 #2
0
def test_rmse():
    rmse = RMSE()

    assert rmse.type == 'rating'
    assert rmse.name == 'RMSE'

    assert 0 == rmse.compute(np.asarray([0]), np.asarray([0]))
    assert 1 == rmse.compute(np.asarray([0, 1]), np.asarray([1, 0]))
    assert 2 == rmse.compute(np.asarray([0, 1]), np.asarray([2, 3]),
                             np.asarray([1, 3]))
コード例 #3
0
def test_with_ratio_split():
    data_file = './tests/data.txt'
    data = Reader.read_uir_triplets(data_file)
    exp = Experiment(eval_method=RatioSplit(data, verbose=True),
                     models=[PMF(1, 0)],
                     metrics=[MAE(), RMSE(),
                              Recall(1), FMeasure(1)],
                     verbose=True)
    exp.run()

    assert (1, 4) == exp.avg_results.shape

    assert 1 == len(exp.user_results)
    assert 4 == len(exp.user_results['PMF'])
    assert 2 == len(exp.user_results['PMF']['MAE'])
    assert 2 == len(exp.user_results['PMF']['RMSE'])
    assert 2 == len(exp.user_results['PMF']['Recall@1'])
    assert 2 == len(exp.user_results['PMF']['F1@1'])

    try:
        Experiment(None, None, None)
    except ValueError:
        assert True

    try:
        Experiment(None, [PMF(1, 0)], None)
    except ValueError:
        assert True
コード例 #4
0
 def test_with_cross_validation(self):
     exp = Experiment(eval_method=CrossValidation(self.data),
                      models=[PMF(1, 0)],
                      metrics=[MAE(), RMSE(),
                               Recall(1),
                               FMeasure(1)],
                      verbose=True)
     exp.run()
コード例 #5
0
def test_with_cross_validation():
    data_file = './tests/data.txt'
    data = reader.read_uir(data_file)
    exp = Experiment(eval_method=CrossValidation(data),
                     models=[PMF(1, 0)],
                     metrics=[MAE(), RMSE(),
                              Recall(1), FMeasure(1)],
                     verbose=True)
    exp.run()
コード例 #6
0
 def test_grid_search(self):
     model = MF(max_iter=1, verbose=True)
     metric = RMSE()
     gs_mf = GridSearch(
         model=model,
         space=[
             Discrete("k", [1, 2, 3]),
             Discrete("learning_rate", [0.1, 0.01])
         ],
         metric=metric,
         eval_method=self.eval_method,
     )
     Experiment(
         eval_method=self.eval_method,
         models=[gs_mf],
         metrics=[metric],
         user_based=False,
     ).run()
コード例 #7
0
    def test_with_ratio_split(self):
        exp = Experiment(eval_method=RatioSplit(self.data, verbose=True),
                         models=[PMF(1, 0)],
                         metrics=[MAE(), RMSE(),
                                  Recall(1),
                                  FMeasure(1)],
                         verbose=True)
        exp.run()

        try:
            Experiment(None, None, None)
        except ValueError:
            assert True

        try:
            Experiment(None, [PMF(1, 0)], None)
        except ValueError:
            assert True
コード例 #8
0
    def test_with_ratio_split(self):
        Experiment(eval_method=RatioSplit(
            self.data + [(self.data[0][0], self.data[1][1], 5.0)],
            exclude_unknowns=True,
            seed=123,
            verbose=True),
                   models=[PMF(1, 0)],
                   metrics=[MAE(), RMSE()],
                   verbose=True).run()

        try:
            Experiment(None, None, None)
        except ValueError:
            assert True

        try:
            Experiment(None, [PMF(1, 0)], None)
        except ValueError:
            assert True
コード例 #9
0
ファイル: first_example.py プロジェクト: zshwuhan/cornac
rs = RatioSplit(data=ml_100k, test_size=0.2, rating_threshold=4.0, seed=123)

# initialize models, here we are comparing: Biased MF, PMF, and BPR
models = [
    MF(k=10,
       max_iter=25,
       learning_rate=0.01,
       lambda_reg=0.02,
       use_bias=True,
       seed=123),
    PMF(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001, seed=123),
    BPR(k=10, max_iter=200, learning_rate=0.001, lambda_reg=0.01, seed=123),
]

# define metrics to evaluate the models
metrics = [
    MAE(),
    RMSE(),
    Precision(k=10),
    Recall(k=10),
    NDCG(k=10),
    AUC(),
    MAP()
]

# put it together in an experiment, voilà!
cornac.Experiment(eval_method=rs,
                  models=models,
                  metrics=metrics,
                  user_based=True).run()
コード例 #10
0
ファイル: mter_example.py プロジェクト: Jamim/cornac
eval_method = RatioSplit(data,
                         test_size=0.2,
                         rating_threshold=1.0,
                         sentiment=md,
                         exclude_unknowns=True,
                         verbose=True,
                         seed=123)

mter = MTER(n_user_factors=15,
            n_item_factors=15,
            n_aspect_factors=12,
            n_opinion_factors=12,
            n_bpr_samples=1000,
            n_element_samples=200,
            lambda_reg=0.1,
            lambda_bpr=5,
            n_epochs=2000,
            lr=0.1,
            verbose=True,
            seed=123)

exp = Experiment(
    eval_method=eval_method,
    models=[mter],
    metrics=[RMSE(), NDCG(k=10),
             NDCG(k=20),
             NDCG(k=50),
             NDCG(k=100)])

exp.run()
コード例 #11
0
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)
コード例 #12
0
from cornac.metrics import MAE, RMSE, Precision, Recall, NDCG, AUC, MAP

from cornac.eval_methods import PropensityStratifiedEvaluation
from cornac.experiment import Experiment

# Load the MovieLens 1M dataset
ml_dataset = cornac.datasets.movielens.load_feedback(variant="1M")

# Instantiate an instance of PropensityStratifiedEvaluation method
stra_eval_method = PropensityStratifiedEvaluation(
    data=ml_dataset,
    n_strata=2,  # number of strata
    rating_threshold=4.0,
    verbose=True)

# define the examined models
models = [
    WMF(k=10, seed=123),
    BPR(k=10, seed=123),
]

# define the metrics
metrics = [MAE(), RMSE(), Precision(k=10), Recall(k=10), NDCG(), AUC(), MAP()]

# run an experiment
exp_stra = Experiment(eval_method=stra_eval_method,
                      models=models,
                      metrics=metrics)

exp_stra.run()