mojolearn.AlternatingLeastSquares#

class mojolearn.AlternatingLeastSquares(factors=100, regularization=0.01, alpha=1.0, iterations=15, use_cg=False, calculate_training_loss=False, random_state=None, numeric_mode=None, cg_steps=3)#

Implicit-feedback matrix factorization by alternating least squares.

The method of Hu, Koren and Volinsky (2008), following the implicit library: confidence is alpha * user_items, and each iteration solves for every user’s factors and then every item’s. Unlike implicit, use_cg defaults to False (the exact solve), and user_items is held dense (0 means no interaction; a scipy.sparse matrix is densified).

Parameters:
  • factors (int, default 100) – Number of latent factors.

  • regularization (float, default 0.01) – L2 penalty on the factors.

  • alpha (float, default 1.0) – Confidence scale applied to user_items.

  • iterations (int, default 15) – Number of alternating sweeps.

  • use_cg (bool, default False) – Use implicit’s conjugate-gradient solver (cg_steps steps per row per half-sweep) instead of the exact Cholesky solve.

  • calculate_training_loss (bool, default False) – Record training_loss_ after every iteration (implicit’s loss).

  • random_state (int or None, default None) – Seed of the starting factors; None means 0.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

  • cg_steps (int, default 3) – Conjugate-gradient steps per row with use_cg=True; must be at least 1.

Variables:
  • user_factors_ (user_factors,)

  • item_factors_ (item_factors,)

  • training_loss_ (list of float) – Only with calculate_training_loss=True.

Examples

>>> import numpy as np, mojolearn
>>> R = (np.random.default_rng(0).random((200, 50)) < 0.05).astype(np.float32)
>>> als = mojolearn.AlternatingLeastSquares(factors=8, iterations=5, random_state=0).fit(R)
>>> ids, scores = als.recommend(0, R, N=5)

Methods

fit(user_items[, show_progress])

Fit user and item factors.

fit_transform(X[, y])

Fit to X, then return transform(X).

get_params([deep])

Return this estimator's constructor parameters.

recommend(userid, user_items[, N, ...])

Return the top N items for one user.

set_params(**params)

Set constructor parameters by keyword.

similar_items(itemid[, N])

Return the N items most similar to one item.

fit(user_items, show_progress=False)#

Fit user and item factors.

Parameters:
  • user_items (array-like of shape (n_users, n_items)) – Interaction strengths; 0 means no interaction. A scipy.sparse matrix is densified.

  • show_progress (bool, default False) – Accepted for implicit compatibility; nothing is shown.

Returns:

self

Raises:

ValueError – If use_cg=True and cg_steps < 1.

recommend(userid, user_items, N=10, filter_already_liked_items=True)#

Return the top N items for one user.

Items are ranked by the dot product of user and item factors, ties going to the lower item id.

Parameters:
  • userid (int) – Row of the user.

  • user_items (array-like of shape (n_users, n_items) or (1, n_items), or None) – Interactions used to filter items the user already has: row userid, or the single row given.

  • N (int, default 10) – Number of items to return; must be non-negative.

  • filter_already_liked_items (bool, default True) – Skip items with a nonzero entry in the user’s row.

Returns:

  • ids (ndarray of shape (<= N,), int32)

  • scores (ndarray of shape (<= N,), float32)

Raises:

ValueError – If N is negative or user_items has no matching row.

similar_items(itemid, N=10)#

Return the N items most similar to one item.

Similarity is the cosine of the item factors. The item itself is included, as implicit returns it.

Parameters:
  • itemid (int)

  • N (int, default 10) – Must be non-negative.

Returns:

  • ids (ndarray of shape (<= N,), int32)

  • scores (ndarray of shape (<= N,), float32)

fit_transform(X, y=None)#

Fit to X, then return transform(X).

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (ignored)

Returns:

ndarray of shape (n_samples, n_components), float32

get_params(deep=True)#

Return this estimator’s constructor parameters.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility; it has no effect.

Returns:

dict – Parameter name to current value.

set_params(**params)#

Set constructor parameters by keyword.

Raises:

ValueError – If a keyword is not a parameter of this estimator.

Returns:

self