Package: clayrs
ClayRSClayRSis a python framework for (mainly) content-based recommender systems which allows you to perform several operations, starting from a raw representation of users and items to building and evaluating a recommender system. It also supports graph-based recommendation with feature selection algorithms and graph manipulation methods.The framework has three main modules, which you can also use individually:Given a raw source, theContent Analyzer:Creates and serializes contents,Using the chosen configurationTheRecSysmodule allows to:Instantiate a recommender systemUsing items and users serialized by the Content AnalyzerMake scorepredictionorrecommenditems for the active user(s)TheEvalModelhas the task of evaluating a recommender system, using several state-of-the-art metricsCode examples for all three modules will follow in theUsagesectionInstallationClayRSrequires Python3.7or later, while package dependencies are inrequirements.txtand are all installable
viapip, asClayRSitself.To install it execute the following command:pip install clayrsUsageContent AnalyzerThe first thing to do is to import the Content Analyzer moduleWe will access its methods and classes via dot notationimportclayrs.content_analyzerascaThen, let's point to the source containing raw information to processraw_source=ca.JSONFile('items_info.json')We can now start building the configuration for the itemsNote that same operations that can be specified foritems, could be also specified forusers, via theca.UserAnalyzerConfigclass# Configuration of item representationmovies_ca_config=ca.ItemAnalyzerConfig(source=raw_source,id='movielens_id',# id which uniquely identifies each itemoutput_directory='movies_codified/'# where items complexly represented will be stored)Let's represent theplotfield of each content with a TfIdf representationSince thepreprocessingparameter has been specified, then each field is first preprocessed with the specified
operationsmovies_ca_config.add_single_config('plot',ca.FieldConfig(ca.SkLearnTfIdf(),preprocessing=ca.NLTK(stopwords_removal=True,lemmatization=True),id='tfidf')# Custom id)To finalize the Content Analyzer part, let's instantiate theContentAnalyzerclass by passing the built configuration
and by calling itsfit()methodThe items will be created with the specified representations and serializedca.ContentAnalyzer(movies_ca_config).fit()RecSysSimilarly above, we must first import the RecSys moduleimportclayrs.recsysasrsThen we load the rating frame from a TSV fileIn this case in our file the first three columns are user_id, item_id, score in this orderIf your file has a different structure you must specify how to map the column via parameters, check documentation
for moreratings=ca.Ratings(ca.CSVFile('ratings.tsv',separator='\t'))Let's split with the KFold technique the loaded rating frame into train set and test setsincen_splits=2, train_list will contain twotrain_setsand test_list will contain twotest_setstrain_list,test_list=rs.KFoldPartitioning(n_splits=2).split_all(ratings)In order to recommend items to users, we must choose an algorithm to useIn this case we are using theCentroidVectoralgorithm which will work by using the first representation
specified for theplotfieldYou can freely choose which representation to use among all representation codified for the fields in the Content
Analyzer phasecentroid_vec=rs.CentroidVector({'plot':'tfidf'},similarity=rs.CosineSimilarity())Let's now compute the top-10 ranking for each user of the train setBy default the candidate items are those in the test set of the user, but you can change this behaviour with themethodologyparameterSince we used the kfold technique, we iterate over the train sets and test setsresult_list=[]fortrain_set,test_setinzip(train_list,test_list):cbrs=rs.ContentBasedRS(centroid_vec,train_set,'movies_codified/')rank=cbrs.fit_rank(test_set,n_recs=10)result_list.append(rank)EvalModelSimilarly to the Content Analyzer and RecSys module, we must first import the evaluation moduleimportclayrs.evaluationasevaThe Evaluation module needs the following parameters:A list of computed rank/predictions (in case multiple splits must be evaluated)A list of truths (in case multiple splits must be evaluated)List of metrics to computeObviously the list of computed rank/predictions and list of truths must have the same length,
and the rank/prediction in positionwill be compared
with the truth at positionem=eva.EvalModel(pred_list=result_list,truth_list=test_list,metric_list=[eva.NDCG(),eva.Precision(),eva.RecallAtK(k=5)])Then simply call thefit()method of the instantiated objectIt will return two pandas DataFrame: the first one contains the metrics aggregated for the system,
while the second contains the metrics computed for each user (where possible)sys_result,users_result=em.fit()Note that the EvalModel is able to compute evaluation of recommendations generated by other tools/frameworks, check
documentation for more