sklearn.datasets
.make_blobs
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
)
[source]
Parameters: | n_samples : int, optional (default=100)
n_features : int, optional (default=2)
centers : int or array of shape [n_centers, n_features], optional
cluster_std : float or sequence of floats, optional (default=1.0)
center_box : pair of floats (min, max), optional (default=(-10.0, 10.0))
shuffle : boolean, optional (default=True)
random_state : int, RandomState instance or None, optional (default=None)
|
---|---|
Returns: | X : array of shape [n_samples, n_features]
y : array of shape [n_samples]
|
以上源于sklearn,http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_blobs.html
输入:
n_samples表示产生多少个数据。n_features表示数据是几维。centers表示数据点中心,可以输入int数字,代表有多少个中心,也可以输入几个坐标(fixed center locations)。cluster_std表示分布的标准差。
返回值:
X,[n_samples, n_features]形状的数组,代表产生的样本
y,[n_samples]形状的数组,代表每个点的标签(类别)
例子:
例如要生成5类数据(100个样本,每个样本有2个特征),代码如下:
from sklearn.datasets import make_blobs
from matplotlib import pyplot
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
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)#c=label为不同的类别设置不同的颜色
pyplot.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
本文为转载,来源:https://blog.youkuaiyun.com/ichuzhen/article/details/51768934
----------------------------------------------------------------------------------------------------
愿共同学习,共同进步,若有错误,欢迎指正!