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 Utils Math

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


Knowledge Sources
Domains Online_Learning, Mathematics, Linear_Algebra
Last Updated 2026-02-08 16:00 GMT

Overview

Mathematical utility functions for vector/matrix operations on dict-based sparse representations.

Description

Provides a collection of mathematical operations optimized for River's dict-based sparse data format. Includes dot products, matrix multiplication, outer products, norms, distance metrics, sigmoid, softmax, and matrix inversion updates (Sherman-Morrison, Woodbury). All operations handle sparse representations efficiently by only processing non-zero elements.

Usage

Use internally within River algorithms or when implementing custom models. Essential for linear models, neural networks, and any algorithm requiring vector/matrix arithmetic on sparse data.

Code Reference

Source Location

Signature

def dot(x: dict, y: dict) -> float:
    ...

def chain_dot(*xs):
    ...

def matmul2d(A, B):
    ...

def outer(u: dict, v: dict) -> dict:
    ...

def norm(x: dict, order=None) -> float:
    ...

def minkowski_distance(a: dict, b: dict, p: int):
    ...

def sigmoid(x: float):
    ...

def softmax(y_pred: dict):
    ...

def clamp(x: float, minimum=0.0, maximum=1.0):
    ...

def sherman_morrison(A: np.ndarray, u: np.ndarray, v: np.ndarray):
    ...

def woodbury_matrix(A: np.ndarray, U: np.ndarray, V: np.ndarray):
    ...

Import

from river import utils

Usage Examples

from river import utils

# Dot product
x = {'x0': 1, 'x1': 2}
y = {'x1': 21, 'x2': 3}
print(f"Dot product: {utils.math.dot(x, y)}")  # 42

# Chain dot product
x = {'x0': 1, 'x1': 2, 'x2': 1}
y = {'x1': 21, 'x2': 3}
z = {'x1': 2, 'x2': 1 / 3}
print(f"Chain dot: {utils.math.chain_dot(x, y, z)}")  # 85.0

# Outer product
u = dict(enumerate((1, 2, 3)))
v = dict(enumerate((2, 4, 8)))
outer_prod = utils.math.outer(u, v)
print(f"Outer product: {outer_prod}")

# Sigmoid
print(f"Sigmoid(0): {utils.math.sigmoid(0)}")  # 0.5
print(f"Sigmoid(30): {utils.math.sigmoid(30)}")  # 1

# Softmax
scores = {'A': 2.0, 'B': 1.0, 'C': 0.1}
probas = utils.math.softmax(scores)
print(f"Softmax: {probas}")

# Norm
vec = {'x': 3, 'y': 4}
print(f"L2 norm: {utils.math.norm(vec)}")  # 5.0

# Clamp
print(f"Clamp(1.5): {utils.math.clamp(1.5, 0, 1)}")  # 1.0
print(f"Clamp(-0.5): {utils.math.clamp(-0.5, 0, 1)}")  # 0.0

Related Pages

Page Connections

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