Implementation:Online ml River Facto FM
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Factorization_Machines, Recommender_Systems |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Factorization Machines (FM) are online learning models for regression and classification that efficiently capture all pairwise feature interactions using latent factors.
Description
Factorization Machines model the target as a linear combination of features plus all pairwise interactions between features. Each feature is associated with a latent vector of k factors. The interaction between two features is computed as the dot product of their latent vectors. This approach reduces the number of parameters from quadratic to linear in the number of features while still capturing all pairwise interactions. The model automatically one-hot encodes string features as categorical variables. FM uses stochastic gradient descent with customizable optimizers for both weights and latent factors, supporting L1 and L2 regularization on both.
Usage
Use Factorization Machines for sparse high-dimensional problems where feature interactions are important, such as recommendation systems, click-through rate prediction, and collaborative filtering. FM is particularly effective when many features are categorical or when dealing with implicit feedback data. The model gracefully handles missing values and can work with very sparse feature vectors.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/facto/fm.py
Signature
class FMRegressor(
n_factors=10,
weight_optimizer: optim.base.Optimizer | None = None,
latent_optimizer: optim.base.Optimizer | None = None,
loss: optim.losses.RegressionLoss | None = None,
sample_normalization=False,
l1_weight=0.0,
l2_weight=0.0,
l1_latent=0.0,
l2_latent=0.0,
intercept=0.0,
intercept_lr: optim.base.Scheduler | float = 0.01,
weight_initializer: optim.initializers.Initializer | None = None,
latent_initializer: optim.initializers.Initializer | None = None,
clip_gradient=1e12,
seed: int | None = None,
)
class FMClassifier(
n_factors=10,
weight_optimizer: optim.base.Optimizer | None = None,
latent_optimizer: optim.base.Optimizer | None = None,
loss: optim.losses.BinaryLoss | None = None,
sample_normalization=False,
l1_weight=0.0,
l2_weight=0.0,
l1_latent=0.0,
l2_latent=0.0,
intercept=0.0,
intercept_lr: optim.base.Scheduler | float = 0.01,
weight_initializer: optim.initializers.Initializer | None = None,
latent_initializer: optim.initializers.Initializer | None = None,
clip_gradient=1e12,
seed: int | None = None,
)
Import
from river import facto
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| x | dict | Feature dictionary with feature names as keys |
| y | float/bool | Target value for regression or classification |
| Method | Return Type | Description |
|---|---|---|
| predict_one(x) | float/bool | Predicted value or class label |
| predict_proba_one(x) | dict | Probability distribution (classifier only) |
| learn_one(x, y) | None | Updates the model incrementally |
Usage Examples
from river import facto
dataset = (
({'user': 'Alice', 'item': 'Superman'}, 8),
({'user': 'Alice', 'item': 'Terminator'}, 9),
({'user': 'Alice', 'item': 'Star Wars'}, 8),
({'user': 'Alice', 'item': 'Notting Hill'}, 2),
({'user': 'Bob', 'item': 'Superman'}, 8),
({'user': 'Bob', 'item': 'Terminator'}, 9),
)
model = facto.FMRegressor(
n_factors=10,
intercept=5,
seed=42,
)
for x, y in dataset:
model.learn_one(x, y)
prediction = model.predict_one({'Bob': 1, 'Harry Potter': 1})
print(f"Prediction: {prediction}") # 5.236504