Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
import numbers
2
import numpy as np
3
4
from sklearn.utils import check_array, check_random_state
5
from sklearn.utils import shuffle as shuffle_
6
from sklearn.utils.deprecation import deprecated
7
8
9
@deprecated("Please import make_blobs directly from scikit-learn")
10
def make_blobs(n_samples=100, n_features=2, centers=2, cluster_std=1.0,
11
center_box=(-10.0, 10.0), shuffle=True, random_state=None):
12
"""Generate isotropic Gaussian blobs for clustering.
13
14
Read more in the :ref:`User Guide <sample_generators>`.
15
16
Parameters
17
----------
18
n_samples : int, or tuple, optional (default=100)
19
The total number of points equally divided among clusters.
20
21
n_features : int, optional (default=2)
22
The number of features for each sample.
23
24
centers : int or array of shape [n_centers, n_features], optional
25
(default=3)
26
The number of centers to generate, or the fixed center locations.
27
28
cluster_std: float or sequence of floats, optional (default=1.0)
29
The standard deviation of the clusters.
30
31
center_box: pair of floats (min, max), optional (default=(-10.0, 10.0))
32
The bounding box for each cluster center when centers are
33
generated at random.
34
35
shuffle : boolean, optional (default=True)
36
Shuffle the samples.
37
38
random_state : int, RandomState instance or None, optional (default=None)
39
If int, random_state is the seed used by the random number generator;
40
If RandomState instance, random_state is the random number generator;
41
If None, the random number generator is the RandomState instance used
42
by `np.random`.
43
44
Returns
45
-------
46
X : array of shape [n_samples, n_features]
47
The generated samples.
48
49
y : array of shape [n_samples]
50
The integer labels for cluster membership of each sample.
51
52
Examples
53
--------
54
>>> from sklearn.datasets.samples_generator import make_blobs
55
>>> X, y = make_blobs(n_samples=10, centers=3, n_features=2,
56
... random_state=0)
57
>>> print(X.shape)
58
(10, 2)
59
>>> y
60
array([0, 0, 1, 0, 2, 2, 2, 1, 1, 0])
61
62
See also
63
--------
64
make_classification: a more intricate variant
65
"""
66
generator = check_random_state(random_state)
67
68
if isinstance(centers, numbers.Integral):
69
centers = generator.uniform(center_box[0], center_box[1],
70
size=(centers, n_features))
71
else:
72
centers = check_array(centers)
73
n_features = centers.shape[1]
74
75
if isinstance(cluster_std, numbers.Real):
76
cluster_std = np.ones(len(centers)) * cluster_std
77
78
X = []
79
y = []
80
81
n_centers = centers.shape[0]
82
if isinstance(n_samples, numbers.Integral):
83
n_samples_per_center = [int(n_samples // n_centers)] * n_centers
84
for i in range(n_samples % n_centers):
85
n_samples_per_center[i] += 1
86
else:
87
n_samples_per_center = n_samples
88
89
for i, (n, std) in enumerate(zip(n_samples_per_center, cluster_std)):
90
X.append(centers[i] + generator.normal(scale=std,
91
size=(n, n_features)))
92
y += [i] * n
93
94
X = np.concatenate(X)
95
y = np.array(y)
96
97
if shuffle:
98
X, y = shuffle_(X, y, random_state=generator)
99
100
return X, y
101
102