surprise_base_algo.py

class ucas_dm.prediction_algorithms.surprise_base_algo.SurpriseBaseAlgo[source]

Bases: ucas_dm.prediction_algorithms.base_algo.BaseAlgo

Do not use this class directly. This is the base class for all other sub-class which use the algorithms from Python recommend package–’Surprise’. Inherit from this base class will obtain some basic features.

_init_surprise_model()[source]

Sub-class should implement this method which return a prediction algorithm from package ‘Surprise’.

Returns:A surprise-based recommend model
classmethod load(fname)[source]

Load an object previously saved from a file

Parameters:fname – file path
Returns:object loaded from file
predict(u_id, i_id)[source]

Predict the rate of user ‘u_id’ give to the item ‘i_id’

Parameters:
  • u_id – user id
  • i_id – item id
Returns:

rate value

save(fname, *args)[source]

Save an object to a file.

Parameters:
  • fname – file path
  • ignore – a set of attributes that should’t be saved by super class, but subclass may have to handle these special attributes.
to_dict()[source]

Convert algorithm model to a dict which contains algorithm’s type and it’s main hyper-parameters.

Returns:A dict contains type and hyper-parameters.
top_k_recommend(u_id, k)[source]

Calculate the top-K recommend items

Parameters:
  • u_id – users’ identity (user’s id)
  • k – the number of the items that the recommender should return
Returns:

(v,id) v is a list contains predict rate or distance, id is a list contains top-k highest rated or nearest items

train(train_set)[source]

Do some train-set-dependent work here: for example calculate sims between users or items

Parameters:train_set – A pandas.DataFrame contains two attributes: user_id and item_id,which represents the user view record during a period of time.
Returns:return a model that is ready to give recommend