Classic Matrix Factorization

LKPY provides classical matrix factorization implementations.

Common Support

The mf_common module contains common support code for matrix factorization algorithms.

class lenskit.algorithms.mf_common.MFModel(users, items, umat, imat)

Common model for matrix factorization.

user_index

Users in the model (length=:math:m).

Type:pandas.Index
item_index

Items in the model (length=:math:n).

Type:pandas.Index
user_features

The \(m \times k\) user-feature matrix.

Type:numpy.ndarray
item_features

The \(n \times k\) item-feature matrix.

Type:numpy.ndarray
lookup_items(items)

Look up the indices for a set of items.

Parameters:items (array-like) – the item IDs to look up.
Returns:the item indices. Unknown items will have negative indices.
Return type:numpy.ndarray
lookup_user(user)

Look up the index for a user.

Parameters:user – the user ID to look up
Returns:the user index.
Return type:int
n_features

The number of features.

n_items

The number of items.

n_users

The number of users.

score(user, items)

Score a set of items for a user. User and item parameters must be indices into the matrices.

Parameters:
  • user (int) – the user index
  • items (array-like of int) – the item indices
  • raw (bool) – if True, do return raw scores without biases added back.
Returns:

the scores for the items.

Return type:

numpy.ndarray

class lenskit.algorithms.mf_common.BiasMFModel(users, items, bias, umat, imat)

Common model for biased matrix factorization.

user_index

Users in the model (length=:math:m).

Type:pandas.Index
item_index

Items in the model (length=:math:n).

Type:pandas.Index
global_bias

The global bias term.

Type:double
user_bias

The user bias terms.

Type:numpy.ndarray
item_bias

The item bias terms.

Type:numpy.ndarray
user_features

The \(m \times k\) user-feature matrix.

Type:numpy.ndarray
item_features

The \(n \times k\) item-feature matrix.

Type:numpy.ndarray
score(user, items, raw=False)

Score a set of items for a user. User and item parameters must be indices into the matrices.

Parameters:
  • user (int) – the user index
  • items (array-like of int) – the item indices
  • raw (bool) – if True, do return raw scores without biases added back.
Returns:

the scores for the items.

Return type:

numpy.ndarray

Alternating Least Squares

LensKit provides alternating least squares implementations of matrix factorization suitable for explicit feedback data. These implementations are parallelized with Numba, and perform best with the MKL from Conda.

FunkSVD

FunkSVD is an SVD-like matrix factorization that uses stochastic gradient descent, configured much like coordinate descent, to train the user-feature and item-feature matrices.

class lenskit.algorithms.funksvd.FunkSVD(features, iterations=100, lrate=0.001, reg=0.015, damping=5, range=None)

Algorithm class implementing FunkSVD matrix factorization.

Parameters:
  • features (int) – the number of features to train
  • iterations (int) – the number of iterations to train each feature
  • lrate (double) – the learning rate
  • reg (double) – the regularization factor
  • damping (double) – damping factor for the underlying mean
  • range (tuple) – the (min, max) rating values to clamp ratings, or None to leave predictions unclamped.
load_model(path)

Save a trained model to a file.

Parameters:path (str) – the path to file from which to load the model.
Returns:the re-loaded model (of an implementation-defined type).
predict(model, user, items, ratings=None)

Compute predictions for a user and items.

Parameters:
  • model – the trained model to use. Either None or the ratings matrix if the algorithm has no concept of training.
  • user – the user ID
  • items (array-like) – the items to predict
  • ratings (pandas.Series) – the user’s ratings (indexed by item id); if provided, they may be used to override or augment the model’s notion of a user’s preferences.
Returns:

scores for the items, indexed by item id.

Return type:

pandas.Series

save_model(model, path)

Save a trained model to a file or directory. The default implementation pickles the model.

Algorithms are allowed to use any format for saving their models, including directories.

Parameters:
  • model – the trained model.
  • path (str) – the path at which to save the model.
train(ratings, bias=None)

Train a FunkSVD model.

Parameters:
  • ratings – the ratings data frame.
  • bias (bias.BiasModel) – a pre-trained bias model to use.
Returns:

The trained biased MF model.