Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Reco Baseline

From Leeroopedia
Revision as of 16:10, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Online_ml_River_Reco_Baseline.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Online_Learning, Recommender_Systems, Collaborative_Filtering
Last Updated 2026-02-08 16:00 GMT

Overview

Baseline recommender model using global mean plus user and item biases.

Description

Implements a simple but effective baseline for recommendation systems using the formula: ŷ = ȳ + bu + bi, where ȳ is the global mean, bu is user bias, and bi is item bias. Learns biases through stochastic gradient descent with optional L2 regularization. Serves as a strong baseline and foundation for more complex models.

Usage

Use as a baseline for collaborative filtering or as the bias component in factorization models. Captures first-order user and item effects without learning interactions. Essential for understanding if complex models improve over simple bias terms.

Code Reference

Source Location

Signature

class Baseline(reco.base.Ranker):
    def __init__(
        self,
        optimizer: optim.base.Optimizer | None = None,
        loss: optim.losses.Loss | None = None,
        l2=0.0,
        initializer: optim.initializers.Initializer | None = None,
        clip_gradient=1e12,
        seed=None,
    ):
        ...

    def predict_one(self, user, item, x=None):
        ...

    def learn_one(self, user, item, y, x=None):
        ...

Import

from river import reco

Usage Examples

from river import optim, reco

dataset = (
    ({'user': 'Alice', 'item': 'Superman'}, 8),
    ({'user': 'Alice', 'item': 'Terminator'}, 9),
    ({'user': 'Alice', 'item': 'Star Wars'}, 8),
    ({'user': 'Alice', 'item': 'Notting Hill'}, 2),
    ({'user': 'Alice', 'item': 'Harry Potter'}, 5),
    ({'user': 'Bob', 'item': 'Superman'}, 8),
    ({'user': 'Bob', 'item': 'Terminator'}, 9),
    ({'user': 'Bob', 'item': 'Star Wars'}, 8),
    ({'user': 'Bob', 'item': 'Notting Hill'}, 2)
)

model = reco.Baseline(optimizer=optim.SGD(0.005))

for x, y in dataset:
    model.learn_one(**x, y=y)

# Predict for unseen user-item pair
pred = model.predict_one(user='Bob', item='Harry Potter')
print(f"Predicted rating: {pred:.2f}")

# Access learned parameters
print(f"Global mean: {model.global_mean.get():.2f}")
print(f"Bob's bias: {model.u_biases['Bob']:.2f}")
print(f"Harry Potter bias: {model.i_biases['Harry Potter']:.2f}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment