Python sklearn 中的 make_blobs() 函数示例详解
作者:旅途中的宽~ 发布时间:2022-07-24 21:45:48
标签:Python,sklearn,make,blobs
一、介绍
make_blobs()
是 sklearn.datasets中的一个函数。
主要是产生聚类数据集,产生一个数据集和相应的标签。
函数的源代码如下:
def 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):
"""Generate isotropic Gaussian blobs for clustering.
Read more in the :ref:`User Guide <sample_generators>`.
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.
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.
Examples
--------
>>> from sklearn.datasets.samples_generator import make_blobs
>>> X, y = make_blobs(n_samples=10, centers=3, n_features=2,
... random_state=0)
>>> print(X.shape)
(10, 2)
>>> y
array([0, 0, 1, 0, 2, 2, 2, 1, 1, 0])
See also
--------
make_classification: a more intricate variant
"""
generator = check_random_state(random_state)
if isinstance(centers, numbers.Integral):
centers = generator.uniform(center_box[0], center_box[1],
size=(centers, n_features))
else:
centers = check_array(centers)
n_features = centers.shape[1]
if isinstance(cluster_std, numbers.Real):
cluster_std = np.ones(len(centers)) * cluster_std
X = []
y = []
n_centers = centers.shape[0]
n_samples_per_center = [int(n_samples // n_centers)] * n_centers
for i in range(n_samples % n_centers):
n_samples_per_center[i] += 1
for i, (n, std) in enumerate(zip(n_samples_per_center, cluster_std)):
X.append(centers[i] + generator.normal(scale = std,
size = (n, n_features)))
y += [i] * n
X = np.concatenate(X)
y = np.array(y)
if shuffle:
indices = np.arange(n_samples)
generator.shuffle(indices)
X = X[indices]
y = y[indices]
return X, y
二、函数的使用
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)
可以看到它有 7 个参数:
n_samples = 100
,表示数据样本点个数,默认值100;n_features = 2
,是每个样本的特征(或属性)数,也表示数据的维度,默认值是2;centers = 3
,表示类别数(标签的种类数),默认值3;cluster_std = 1.0
,表示每个类别的方差,例如我们希望生成2类数据,其中一类比另一类具有更大的方差,可以将cluster_std设置为[1.0, 3.0],浮点数或者浮点数序列,默认值1.0;center_box = (-10.0, 10.0)
,中心确定之后的数据边界,默认值(-10.0, 10.0);shuffle = True
,将数据进行洗乱,默认值是True;random_state = None
,官网解释是随机生成器的种子,可以固定生成的数据,给定数之后,每次生成的数据集就是固定的。若不给定值,则由于随机性将导致每次运行程序所获得的的结果可能有所不同。在使用数据生成器练习机器学习算法练习或python练习时建议给定数值。
来源:https://blog.csdn.net/wzk4869/article/details/129117675


猜你喜欢
- 如何将产生的密码记录并发送给用户?这里使用了cdonts邮件组件来发送邮件,前提服务器得支持cdonts组件。好了,看看具体实现方法吧,不是
- 不久前因业务需要,我在自己的笔记本中安装了搜霸。当时一个做平面的朋友过来和我做一些设计交流,我在笔记本前准备输入一个网址,他靠近我的电脑,大
- 问题:希望仅仅允许某个指定IP的计算机连接到SQL Server服务器,但不允许其他的客户端进行连接。解决方法如下:你可以直接在防火墙中做限
- 引言本集开始,将会深入Document接口。打开或创建一个文档都会产生一个Document对象,它代表文档本身,所以绝大部分文档的操作都会依
- 实现原理PS的扩散效果可以产生类似毛玻璃质感的效果,使画面有些毛毛的感觉。其实现可通过操作像素三通道数值的方式实现,定义一个随机数器,将图像
- 1. 换源地址# 中科大deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-
- 一、噪声 我们将常会听到平滑(去噪),锐化(和平滑是相反的),那我们就会有疑惑?什么是噪声呢?图像噪声是指存在于图像数
- 一、实例将以下列表的backup_unit_id全部提取出来示例:dbs = [{ &nbs
- 目录前期准备界面编写截图功能实现OCR实现内容显示总结前期准备在这个阶段主要准备整个小程序的结构,既然要实现ocr,那么输入就是一张图片,而
- 相对于C++的继承编写,Python更简洁,而且效率也是很高的,下面编写一个简单Python的继承例子。#!/usr/bin/python&
- 年前在重写淘宝旺铺里的会员卡脚本的时候,无意中发现了一个有趣的事情。代码类似:var associative_array = new Arr
- 问题你想在一个消息传输层如 sockets 、multiprocessing connections 或 ZeroMQ 的基础之上实现一个简
- 本文实例总结了微信小程序实现给嵌套template模板传递数据的方式。分享给大家供大家参考,具体如下:一、template模板调用的数据是单
- 普通爬虫正常流程:数据来源分析发送请求获取数据解析数据保存数据环境介绍python 3.8pycharm 2021专业版【付费VIP完整版】
- 本教程将分步讲解如何使用JQuery和CSS打造一个炫酷动感菜单。jQuery的"write less, do more"
- type指示type要使用的验证器。可识别的类型值为:string:类型必须为string。type 默认是 string// 校验stri
- 本文实例讲述了php多进程中的阻塞与非阻塞操作。分享给大家供大家参考,具体如下:我们通过pcntl_fork来创建子进程,使用pcntl_w
- Python 面向对象Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的。本章节我们将
- sql语句查询数据库中的表名/列名/主键/自动增长值 ----查询数据库中用户创建的表 ----jsj01 为数据库名 select nam
- 网站上传图片后生成缩略图应该是非常常用的功能了,通常来讲为了网站显示美观,缩略图会是同样尺寸,比如最近笔者做的一个站点,缩略图规格要求都是1