sklearn 中 make_blobs模块使用

作者: fred_33c7 | 来源:发表于2019-06-23 12:35 被阅读1次

最近在学习K-means算法。如果自己想写一个K-means算法的话,需要造数据集,这个时候,用sklearn中的make_blobs模块就很有用。
官方API说明
输入和输出:

输入和输出
翻译如下:
  • n_samples是待生成的样本的总数。
  • n_features是每个样本的特征数。
  • centers表示类别数。
  • cluster_std表示每个类别的方差,例如我们希望生成2类数据,其中一类比另一类具有更大的方差,可以将cluster_std设置为[1.0,3.0]。

样例samples:

from sklearn.datasets import make_blobs
from matplotlib import pyplot

data, label = make_blobs(n_samples=100, n_features=2, centers=4)
# 绘制样本显示
pyplot.scatter(data[:, 0], data[:, 1], c=label)
pyplot.show()
结果1
如果要设置方差,可以增加cluster_std参数
from sklearn.datasets import make_blobs
from matplotlib import pyplot

data, label = make_blobs(n_samples=30, n_features=2, centers=3, cluster_std=[0.8, 2.5, 4.5])
# 绘制样本显示
pyplot.scatter(data[:, 0], data[:, 1], c=label)
pyplot.show()
结果2

相关文章

网友评论

    本文标题:sklearn 中 make_blobs模块使用

    本文链接:https://www.haomeiwen.com/subject/lzyiqctx.html