Implementation:Scikit learn Scikit learn ExtendedMath
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Linear Algebra, Numerical Computing |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete utility module for optimized mathematical operations provided by scikit-learn.
Description
The extmath module provides a collection of mathematical utility functions optimized for machine learning workloads. Key functions include safe_sparse_dot for matrix multiplication with sparse support, randomized_svd for truncated SVD via randomized methods, row_norms for efficient norm computation, softmax, stable_cumsum, and weighted_mode among others.
Usage
Use these functions when you need efficient mathematical operations that handle both dense and sparse matrices, or when you need randomized linear algebra for dimensionality reduction and matrix decomposition.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/utils/extmath.py
Signature
def squared_norm(x):
...
def row_norms(X, squared=False):
...
def fast_logdet(A):
...
def density(w):
...
def safe_sparse_dot(a, b, *, dense_output=False):
...
def randomized_svd(M, n_components, *, n_oversamples=10, n_iter="auto",
power_iteration_normalizer="auto", transpose="auto",
flip_sign=True, random_state="warn", svd_lapack_driver="gesdd"):
...
def softmax(X, copy=True):
...
def stable_cumsum(arr, axis=None, rtol=1e-05, atol=1e-08):
...
def weighted_mode(a, w, *, axis=0):
...
Import
from sklearn.utils.extmath import safe_sparse_dot, randomized_svd, softmax, row_norms
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | array-like | Yes | Input array for norm computation |
| a | array-like or sparse matrix | Yes | First matrix for dot product |
| b | array-like or sparse matrix | Yes | Second matrix for dot product |
| M | array-like or sparse matrix | Yes | Matrix to decompose (for randomized_svd) |
| n_components | int | Yes | Number of SVD components to compute |
| random_state | int or RandomState | No | Random state for reproducibility |
Outputs
| Name | Type | Description |
|---|---|---|
| result | ndarray | Result of the mathematical operation |
| U, Sigma, VT | ndarrays | SVD decomposition components (for randomized_svd) |
Usage Examples
Basic Usage
import numpy as np
from sklearn.utils.extmath import safe_sparse_dot, randomized_svd, softmax
# Safe dot product (works with sparse and dense)
A = np.random.rand(100, 50)
B = np.random.rand(50, 30)
C = safe_sparse_dot(A, B)
# Randomized SVD
U, Sigma, VT = randomized_svd(A, n_components=5, random_state=42)
# Softmax
logits = np.array([[1.0, 2.0, 3.0]])
probs = softmax(logits)
print(probs.sum()) # 1.0