Clustering demonstrations
Create controlled labeled data for validating clustering algorithms.
Scikit-Learn · Function
Generates synthetic multiclass data as isotropic Gaussian clusters. It returns feature samples and integer cluster labels, with optional generated center coordinates.
make_blobs constructs a synthetic multiclass dataset by sampling points around isotropic Gaussian centers. You can choose how many samples and features to create, specify centers or let them be generated, control cluster spread, shuffle the rows, and optionally receive the generated center coordinates.
sklearn.datasets.make_blobs(n_samples=100, n_features=2, *, centers=None, cluster_std=1.0, center_box=(-10.0, 10.0), shuffle=True, random_state=None, return_centers=False)| Name | Type | Description |
|---|---|---|
n_samples | int or array-like of int, default=100 | Total number of samples, or an array specifying the sample count for each cluster. |
n_features | int, default=2 | Number of features in each sample. |
centers | int or array-like of shape (n_centers, n_features), default=None | Number of generated centers or fixed center locations. |
cluster_std | float or array-like of float, default=1.0 | Standard deviation of each cluster. |
center_box | tuple of float (min, max), default=(-10.0, 10.0) | Minimum and maximum bounds for randomly generated center coordinates. |
shuffle | bool, default=True | Whether to randomize the sample order. |
random_state | int, RandomState instance or None, default=None | Controls reproducible random generation. |
return_centers | bool, default=False | Whether to include the generated centers in the result. |
Returns X, an ndarray of shape (n_samples, n_features), and y, an ndarray of shape (n_samples, containing integer cluster-membership labels. If return_centers=True, it also returns centers, an ndarray of shape (n_centers, n_features).
from sklearn.datasets import make_blobs
X, y = make_blobs(
n_samples=120,
centers=3,
n_features=2,
cluster_std=0.6,
random_state=0,
)
print(X.shape, y.shape)Creates 120 two-feature samples around three Gaussian cluster centers and returns their cluster labels.
Create controlled labeled data for validating clustering algorithms.
Produce repeatable datasets for examples and tests.
Compare models under different cluster spreads and locations.
It returns X, the generated samples with shape (n_samples, n_features), and y, integer labels identifying each sample's cluster. If return_centers=True, it also returns the generated center coordinates.
Follow-up: How can the number of samples differ by cluster?
Set centers to an integer to request that many generated centers, or pass an array-like value containing fixed center locations. When centers are generated, center_box bounds their coordinates.
Follow-up: How would you make cluster sizes unequal?
cluster_std controls the standard deviation of the clusters and can be one float or an array-like value. A single float applies the same spread to every cluster, while per-cluster values allow different spreads.
Follow-up: What does random_state control?
make_classificationmake_circlesmake_moons