Exemple #1
0
"""
Baseline only
"""

from surprise import BaselineOnly
from rs import Recommender, get_dump_path

uids = [1, 2, 3]
param_grid = {'bsl_options': [{'method': 'als', 'n_epochs': 30},
                              {'method': 'sgd', 'learning_rate': 0.0007}]}

recommender = Recommender(algorithm=BaselineOnly,
                          param_grid=param_grid,
                          dump_model=True,
                          dump_file_name=get_dump_path('baseline_only'))

recommender.recommend(uids=uids, verbose=True)
"""
Slope One
"""

from surprise import SlopeOne
from rs import Recommender, get_dump_path

uids = [1, 2, 3]

recommender = Recommender(algorithm=SlopeOne,
                          dump_model=True,
                          dump_file_name=get_dump_path('slope_one'))

recommender.recommend(uids=uids, verbose=True)
"""
Neighborhood-based collaborative filtering (kNN-with-means)
"""

from surprise import KNNWithMeans
from rs import Recommender, get_dump_path

uids = [1, 2, 3]
param_grid = {
    'k': [20, 40],
    'sim_options': [{
        'name': 'msd'
    }, {
        'name': 'cosine'
    }, {
        'name': 'pearson'
    }, {
        'name': 'pearson_baseline'
    }, {
        'name': 'pearson_baseline',
        'shrinkage': 150
    }]
}

recommender = Recommender(algorithm=KNNWithMeans,
                          param_grid=param_grid,
                          dump_model=True,
                          dump_file_name=get_dump_path('knn_with_means'))

recommender.recommend(uids=uids, verbose=True)
from surprise import SVD
from rs import Recommender, load_data_from_file, parse_config, get_dump_path


data_path = parse_config(section='Path', key='data')
votes = load_data_from_file(data_path + '/votes.csv', (-1, 1))
clips = load_data_from_file(data_path + '/clips.csv', (0, 1))

print('■ Voting data')
param_grid = {'n_epochs': [20, 30], 'n_factors': [20, 50]}
recommender = Recommender(algorithm=SVD,
                          param_grid=param_grid,
                          data=votes,
                          rating_threshold=0.5,
                          dump_model=True,
                          dump_file_name=get_dump_path('viblo_votes'))

recommender.recommend(uids=[2, 9, 21], verbose=True)

print()
print('■ Clipping data')
param_grid = {'n_epochs': [20, 30], 'n_factors': [20, 50]}
recommender = Recommender(algorithm=SVD,
                          param_grid=param_grid,
                          data=clips,
                          rating_threshold=1,
                          dump_model=True,
                          dump_file_name=get_dump_path('viblo_clips'))

recommender.recommend(uids=[2, 12825, 13072], verbose=True)
"""
Matrix factorization - Non-negative Matrix Factorization
"""

from surprise import NMF
from rs import Recommender, get_dump_path

uids = [1, 2, 3]
param_grid = {'n_epochs': [50, 100], 'n_factors': [15, 20]}

recommender = Recommender(algorithm=NMF,
                          param_grid=param_grid,
                          dump_model=True,
                          dump_file_name=get_dump_path('nmf'))

recommender.recommend(uids=uids, verbose=True)
Exemple #6
0
"""
Neighborhood-based collaborative filtering (kNN-basic)
"""

from surprise import KNNBasic
from rs import Recommender, get_dump_path

uids = [1, 2, 3]
param_grid = {'k': [20, 40],
              'sim_options': [{'name': 'msd'},
                              {'name': 'cosine'},
                              {'name': 'pearson'},
                              {'name': 'pearson_baseline'},
                              {'name': 'pearson_baseline', 'shrinkage': 150}]}

recommender = Recommender(algorithm=KNNBasic,
                          param_grid=param_grid,
                          dump_model=True,
                          dump_file_name=get_dump_path('knn_basic'))

recommender.recommend(uids=uids, verbose=True)
"""
Neighborhood-based collaborative filtering (kNN-baseline)
"""

from surprise import KNNBaseline
from rs import Recommender, get_dump_path

uids = [1, 2, 3]
param_grid = {
    'k': [20, 40],
    'bsl_options': [{
        'method': 'als'
    }, {
        'method': 'sgd',
        'learning_rate': 0.0007
    }],
    'sim_options': [{
        'name': 'cosine'
    }, {
        'name': 'pearson_baseline'
    }]
}

recommender = Recommender(algorithm=KNNBaseline,
                          param_grid=param_grid,
                          dump_model=True,
                          dump_file_name=get_dump_path('knn_baseline'))

recommender.recommend(uids=uids, verbose=True)
"""
Co-Clustering
"""

from surprise import CoClustering
from rs import Recommender, get_dump_path

uids = [1, 2, 3]
param_grid = {'n_epochs': [20, 60], 'n_cltr_u': [3, 5], 'n_cltr_i': [3, 5]}

recommender = Recommender(algorithm=CoClustering,
                          param_grid=param_grid,
                          dump_model=True,
                          dump_file_name=get_dump_path('co_clustering'))

recommender.recommend(uids=uids, verbose=True)
"""
Matrix factorization - SVD++ using Alternating Least Squares
"""

from surprise import SVDpp
from rs import Recommender, get_dump_path

uids = [1, 2, 3]
param_grid = {'n_epochs': [10, 20], 'reg_all': [0.01, 0.02]}

recommender = Recommender(algorithm=SVDpp,
                          param_grid=param_grid,
                          dump_model=True,
                          n_folds=3,
                          dump_file_name=get_dump_path('svdpp'))

recommender.recommend(uids=uids, verbose=True)