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)[source]#
Bases:
_BaseImplicit-feedback matrix factorization by alternating least squares.
The method of Hu, Koren and Volinsky (2008), following the
implicitlibrary: confidence isalpha * user_items, and each iteration solves for every user’s factors and then every item’s. Unlikeimplicit,use_cgdefaults to False (the exact solve), anduser_itemsis held dense (0 means no interaction; ascipy.sparsematrix 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_stepssteps 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)."identical"gives the same bits on NVIDIA and AMD GPUs;"fast"applies on Apple GPUs.cg_steps (int, default 3) – Conjugate-gradient steps per row with
use_cg=True; must be at least 1.
- Variables:
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)
- __init__(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)[source]#
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)[source]#
Fit user and item factors.
- Parameters:
user_items (array-like of shape (n_users, n_items)) – Interaction strengths; 0 means no interaction. A
scipy.sparsematrix is densified.show_progress (bool, default False) – Accepted for
implicitcompatibility; nothing is shown.
- Returns:
self
- Raises:
ValueError – If
use_cg=Trueandcg_steps < 1.
- recommend(userid, user_items, N=10, filter_already_liked_items=True)[source]#
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
Nis negative oruser_itemshas no matching row.
- similar_items(itemid, N=10)[source]#
Return the N items most similar to one item.
Similarity is the cosine of the item factors. The item itself is included, as
implicitreturns 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)