Implementation:Scikit learn Scikit learn RandomSampling
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Random Sampling |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for providing random sampling utilities including sampling without replacement and sparse random matrix generation, provided by scikit-learn.
Description
The sklearn.utils.random module provides utilities for random sampling. It re-exports sample_without_replacement from the Cython-optimized _random module and implements _random_choice_csc, which generates sparse random matrices given column class distributions. The sparse matrix generation is useful for creating random multi-label classification targets.
Usage
Use these utilities when you need efficient random sampling without replacement or need to generate sparse random matrices with specific class distributions, particularly in the context of multi-label classification data generation.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/utils/random.py
Signature
def _random_choice_csc(n_samples, classes, class_probability=None, random_state=None):
# Re-exported from sklearn.utils._random:
sample_without_replacement
Import
from sklearn.utils.random import sample_without_replacement
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| n_samples | int | Yes | Number of samples to draw in each column |
| classes | list of arrays | Yes | List of classes for each column (output) |
| class_probability | list of arrays or None | No | Class distribution for each column (default None uses uniform) |
| random_state | int, RandomState or None | No | Controls randomness of sampled classes (default None) |
Outputs
| Name | Type | Description |
|---|---|---|
| random_matrix | sparse csc matrix of shape (n_samples, n_outputs) | Sparse random matrix with sampled class labels |
Usage Examples
Basic Usage
from sklearn.utils.random import sample_without_replacement
import numpy as np
# Sample 5 unique indices from a population of 100
indices = sample_without_replacement(100, 5, random_state=42)
print(indices)