我们先引入make_blobs并调用,参数请看注释:
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=20, # 生成20个样本
n_features=2, # 每个样本2个特征
centers=3 # 3个中心
)
查看生成的数据长度:
X.shape,y.shape
((20, 2), (20,))
查看X,y的值:
X,y
(array([[ 5.89960601, 8.71170696],
[ -4.97240689, -5.58567093],
[-10.84160353, 1.7629505 ],
[ -9.97435178, 2.08737815],
[ 5.9428016 , 10.73028316],
[ 5.66663745, 9.18073685],
[ 6.25187124, 10.79703471],
[ -7.02515868, -6.5240859 ],
[ -5.73605444, -4.19309637],
[-10.70728071, 2.36406501],
[-10.37438672, 2.07198502],
[ 3.96774726, 8.25325771],
[ -6.45551268, -4.5922466 ],
[ -9.86581166, 3.80563866],
[ -7.98049509, -6.16329193],
[ -6.78344711, -5.59575455],
[ -9.0294714 , 1.21256105],
[ -5.74061642, -5.77171784],
[ -9.70159157, 3.65504855],
[ 6.229 , 8.38946947]]),
array([2, 0, 1, 1, 2, 2, 2, 0, 0, 1, 1, 2, 0, 1, 0, 0, 1, 0, 1, 2]))
用matplotlib画出来:
import matplotlib.pyplot as plt
plt.scatter(
X[:,0], #第一个特征值当x坐标
X[:,1], #第二个特征值当y坐标
c=y # 数据类别标签当做颜色,相同标签的颜色也相同
)
plt.show()