Create small deterministic datasets for examples and tests.
Regression model testing
Scikit-Learn · Function
Generates synthetic regression data from a random linear model. It supports configurable feature structure, informative features, target count, Gaussian noise, shuffling, and optional recovery of the true coefficients.
make_regression creates feature data and targets from a random linear model with a configurable number of nonzero regressors. It can add centered Gaussian noise, optionally produce multiple targets or the underlying coefficients, and generate either well-conditioned or low-rank feature matrices.
make_regression(n_samples=100, n_features=100, *, n_informative=10, n_targets=1, bias=0.0, effective_rank=None, tail_strength=0.5, noise=0.0, shuffle=True, coef=False, random_state=None)| Name | Type | Description |
|---|---|---|
n_samples | int | Number of samples to generate; default 100. |
n_features | int | Number of features; default 100. |
n_informative | int | Number of informative features with nonzero coefficients; default 10. |
n_targets | int | Number of regression targets; default 1. |
bias | float | Bias term added to the output; default 0.0. |
effective_rank | int or None | Effective rank of the generated feature matrix; default None. |
tail_strength | float | Controls the relative importance of the fat-tail profile; default 0.5. |
noise | float | Standard deviation of Gaussian noise applied to the output; default 0.0. |
shuffle | bool | Whether to shuffle samples and features; default True. |
coef | bool | Whether to return the underlying model coefficients; default False. |
random_state | int, RandomState instance or None | Controls random generation and reproducibility; default None. |
Returns X, an ndarray of shape (n_samples, n_features), and y, an ndarray of shape (n_samples,) or (n_samples, n_targets). If coef=True, also returns coef, an ndarray of shape (n_features,) or (n_features, n_targets), containing the underlying linear-model coefficients.
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression
X, y = make_regression(
n_samples=100,
n_features=4,
n_informative=2,
noise=1.0,
random_state=42,
)
model = LinearRegression().fit(X, y)
print(model.coef_.shape)Creates reproducible synthetic regression data and fits a linear model to it.
Regression model testing
Algorithm benchmarking
Multiple-output regression
Feature-structure experiments
make_regression generates synthetic regression data by applying a random linear model to generated features, using n_informative nonzero regressors. It can also add Gaussian noise to the outputs.
Follow-up: How would you inspect the true model? Set coef=True and capture the third returned value, which contains the underlying coefficients.
X has shape (n_samples, n_features). y has shape (n_samples,) for one target or (n_samples, n_targets) for multiple targets.
Follow-up: What changes when n_targets is greater than one? The target matrix becomes two-dimensional, with one column per generated target.
random_state controls the random generation and makes results reproducible when given a fixed integer. The function returns X and y by default, and additionally coefficients when coef=True.
Follow-up: How can you create a noisy but reproducible problem? Pass both a nonzero noise value and a fixed random_state.
make_low_rank_matrixmake_friedman1make_sparse_uncorrelated