mlprimitives.adapters.lightfm module

class mlprimitives.adapters.lightfm.LightFM(epochs=1, num_threads=1, *args, **kwargs)[source]

Bases: lightfm.lightfm.LightFM

fit(X, y)[source]

Fit the model.

For details on how to use feature matrices, see the documentation on the lightfm.LightFM class.

Parameters
  • interactions (np.float32 coo_matrix of shape [n_users, n_items]) – the matrix containing user-item interactions. Will be converted to numpy.float32 dtype if it is not of that type.

  • user_features (np.float32 csr_matrix of shape [n_users, n_user_features], optional) – Each row contains that user’s weights over features.

  • item_features (np.float32 csr_matrix of shape [n_items, n_item_features], optional) – Each row contains that item’s weights over features.

  • sample_weight (np.float32 coo_matrix of shape [n_users, n_items], optional) – matrix with entries expressing weights of individual interactions from the interactions matrix. Its row and col arrays must be the same as those of the interactions matrix. For memory efficiency its possible to use the same arrays for both weights and interaction matrices. Defaults to weight 1.0 for all interactions. Not implemented for the k-OS loss.

  • epochs (int, optional) – number of epochs to run

  • num_threads (int, optional) – Number of parallel computation threads to use. Should not be higher than the number of physical cores.

  • verbose (bool, optional) – whether to print progress messages. If tqdm is installed, a progress bar will be displayed instead.

Returns

the fitted model

Return type

LightFM instance

get_columns(X)[source]
predict(X)[source]

Compute the recommendation score for user-item pairs.

For details on how to use feature matrices, see the documentation on the lightfm.LightFM class.

Parameters
  • user_ids (integer or np.int32 array of shape [n_pairs,]) – single user id or an array containing the user ids for the user-item pairs for which a prediction is to be computed. Note that these are LightFM’s internal id’s, i.e. the index of the user in the interaction matrix used for fitting the model.

  • item_ids (np.int32 array of shape [n_pairs,]) – an array containing the item ids for the user-item pairs for which a prediction is to be computed. Note that these are LightFM’s internal id’s, i.e. the index of the item in the interaction matrix used for fitting the model.

  • user_features (np.float32 csr_matrix of shape [n_users, n_user_features], optional) – Each row contains that user’s weights over features.

  • item_features (np.float32 csr_matrix of shape [n_items, n_item_features], optional) – Each row contains that item’s weights over features.

  • num_threads (int, optional) – Number of parallel computation threads to use. Should not be higher than the number of physical cores.

Returns

Numpy array containing the recommendation scores for pairs defined by the inputs.

Return type

np.float32 array of shape [n_pairs,]

Notes

As indicated above, this method returns an array of scores corresponding to the score assigned by the model to _pairs of inputs_. Importantly, this means the i-th element of the output array corresponds to the score for the i-th user-item pair in the input arrays.

Concretely, you should expect the lfm.predict([0, 1], [8, 9]) to return an array of np.float32 that may look something like [0.42 0.31], where 0.42 is the score assigned to the user-item pair (0, 8) and 0.31 the score assigned to pair (1, 9) respectively.

In other words, if you wish to generate the score for a few items (e.g. [7, 8, 9]) for two users (e.g. [0, 1]), a proper way to call this method would be to use lfm.predict([0, 0, 0, 1, 1, 1], [7, 8, 9, 7, 8, 9]), and _not_ lfm.predict([0, 1], [7, 8, 9]) as you may initially expect (this will throw an exception!).