Package: acf
AcfA lightweight recommender engine for implicit feedback datasetsThe package implements an algorithm described inCollaborative Filtering for Implicit Feedback Datasetspaper.
The algorithm is based on the following ideas:using collaborative filtering with latent factorstransforming feedback observations into binary preferences
with associated confidence levelsusing alternating least sqaures to compute the matrix factorizationInstallThe package requires Python3.7or newer, the only dependencies arenumpyandpandas. To install it, runpipinstallacfUsageThe following example shows how to train a model and compute predictions.importacfimportpandasaspd# assuming the data are in the following format:# | user_id | item_id | feedback |# |---------|---------|----------|# | 2491    | 129     | 2        |interactions=pd.read_csv('interactions.csv')engine=acf.Engine(reg_lambda=1,alpha=35,n_factors=2,random_state=0)engine.fit(interactions,user_column='user_id',item_column='item_id',feedback_column='feedback',n_iter=20,n_jobs=4)# get the best 20 recommendationsprediction=engine.predict(user=2491,top_n=20)# to print training loss value at every iterationprint(engine.loss)Model EvaluationFor performance evaluation, the package offersmetrics.mean_rankfunction that implements "mean rank" metric as defined by equation
8 in thepaper.The metric is a weighted mean of percentile-ranked recommendations
(rank_ui = 0says that itemiis the first to be recommended for
useruand itemjwithrank_uj = 1is the last to be recommended)
where the weights are the actual feedback values fromRuser-item matrix.interactions_test=pd.read_csv('intercations_test.csv')print(acf.metrics.mean_rank(interactions=interactions_test,user_column='user_id',item_column='item_id'feedback_column='feedback',engine=engine))Model PersistenceTrained model can be serialized and stored usingjobliborpickle.To store a model:withopen('engine.joblib','wb')asf:joblib.dump(engine,f)To load a model:withopen('engine.joblib','rb')asf:engine=joblib.load(f)Public APIacf.Engineacf.core.computation.Engine(reg_lambda=0.1,alpha=40,n_factors=10,random_state=None):Class exposing the recommender.reg_lambda: regularization strengthalpha: gain parameter in feedback-confidence transformationc_ui = 1 + alpha * r_uin_factors: number of latent factorsrandom_state: initial RNG stateProperties:user_factors: user factor matrixitem_factors: item factor matrixloss: training loss historyMethods:Engine.fit(interactions,user_column,item_column,feedback_column,n_iter=20,n_jobs=1)Trains the model.interactions: dataframe containing user-item feedbacksuser_column: name of the column containing user idsitem_column: name of the column containing item idsfeedback_column: name of the column containing feedback valuesn_iter: number of alternating least squares iterationn_jobs: number of parallel jobsEngine.predict(user,top_n=None)Predicts the recommendation.user: user identification for whom the prediction is computedtop_n: if notNone, only the besr n items are included in the resultReturns:predicted recommendation score for each item aspandas.Seriesacf.metrics.mean_rankacf.core.metrics.mean_rank(interactions,user_column,item_column,feedback_column,engine)Computes mean rank evaluation.interactions: dataframe containing user-item feedbacksuser_column: name of the column containing user idsitem_column: name of the column containing item idsfeedback_column: name of the column containing feedback valuesengine: trainedacf.EngineinstanceReturns:computed valueTestsTests can be executed bypytestaspython-mpytestacf/tests