参考链接:https://blog.csdn.net/ichuzhen/article/details/51768934
Abstract
ref: http://scikit-learn.org/0.17/modules/generated/sklearn.datasets.make_blobs.html
Generate isotropic Gaussian blobs for clustering.
sklearn.datasets.make_blobs(n_samples=100, n_features=2, centers=3, cluster_std=1.0, center_box=(-10.0, 10.0), shuffle=True, random_state=None)
Parameters
n_samples: int, optional (default=100)
The total number of points equally divided among clusters.
待生成的样本的总数。
n_features: int, optional (default=2)
The number of features for each sample.
每个样本的特征数。
centers: int or array of shape [n_centers, n_features], optional (default=3)
The number of centers to generate, or the fixed center locations.
要生成的样本中心(类别)数,或者是确定的中心点。
cluster_std: float or sequence of floats, optional (default=1.0)
The standard deviation of the clusters.
每个类别的方差,例如我们希望生成2类数据,其中一类比另一类具有更大的方差,可以将cluster_std设置为[1.0,3.0]。
center_box: pair of floats (min, max), optional (default=(-10.0, 10.0))
The bounding box for each cluster center when centers are generated at random.
shuffle: boolean, optional (default=True)
Shuffle the samples.
random_state: int, RandomState instance or None, optional (default=None)
If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
Returns
X : array of shape [n_samples, n_features]
The generated samples.
生成的样本数据集。
y : array of shape [n_samples]
The integer labels for cluster membership of each sample.
样本数据集的标签。
Sample
例如要生成5类数据(100个样本,每个样本有2个特征),代码如下:
from sklearn.datasets import make_blobs
from matplotlib import pyplot
#一共100个点,n_features表示特征数,centers表示要生成的类别,每个类别的点的数量是:n_samples/centers
data, label = make_blobs(n_samples=100, n_features=2, centers=5)
# 绘制样本显示
pyplot.scatter(data[:, 0], data[:, 1], c=label)
pyplot.show()
如果希望为每个类别设置不同的方差,需要在上述代码中加入cluster_std参数:
from sklearn.datasets import make_blobs
from matplotlib import pyplot
#一共三个类别,因为centers=3,因此加入cluster_std的list需要有3个元素
data, label = make_blobs(n_samples=10, n_features=2, centers=3, cluster_std=[0.8, 2.5, 4.5])
# 绘制样本显示
pyplot.scatter(data[:, 0], data[:, 1], c=label)
pyplot.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8