Implementation:Scikit learn Scikit learn BenchRandomProjections
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Benchmarking |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for benchmarking random projection transformers provided by scikit-learn.
Description
This benchmark script evaluates the performance of GaussianRandomProjection and SparseRandomProjection transformers. It measures fit and transform times across varying data sizes and dimensionalities. The script also validates the Johnson-Lindenstrauss lemma by comparing minimum dimension estimates with empirical results from both dense and sparse random projection implementations.
Usage
Use this benchmark to compare the speed of Gaussian vs. Sparse random projections for dimensionality reduction, and to validate the Johnson-Lindenstrauss bounds for your data characteristics.
Code Reference
Source Location
- Repository: scikit-learn
- File: benchmarks/bench_random_projections.py
Signature
def type_auto_or_float(val)
def type_auto_or_int(val)
def compute_time(t_start, delta)
def bench_scikit_transformer(X, transformer)
from sklearn.random_projection import (
GaussianRandomProjection,
SparseRandomProjection,
johnson_lindenstrauss_min_dim,
)
Import
from sklearn.random_projection import GaussianRandomProjection, SparseRandomProjection
from sklearn.random_projection import johnson_lindenstrauss_min_dim
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | array-like | Yes | Input data matrix to be projected |
| transformer | object | Yes | Random projection transformer instance |
| --n-times | int | No | Number of benchmark repetitions |
| --n-features | int | No | Number of features in input data |
| --n-components | str/int | No | Target dimensionality or 'auto' |
Outputs
| Name | Type | Description |
|---|---|---|
| time_to_fit | float | Time to fit the transformer in seconds |
| time_to_transform | float | Time to transform the data in seconds |
| Console output | text | Timing results for each configuration |
Usage Examples
Basic Usage
from sklearn.random_projection import SparseRandomProjection
import numpy as np
X = np.random.randn(10000, 500)
transformer = SparseRandomProjection(n_components=100, random_state=42)
X_reduced = transformer.fit_transform(X)
print("Reduced shape:", X_reduced.shape)