【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 aregenerated 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 usedby `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)>>> yarray([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_stdX = []y = []n_centers = centers.shape[0]n_samples_per_center = [int(n_samples // n_centers)] * n_centersfor i in range(n_samples % n_centers):n_samples_per_center[i] += 1for 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] * nX = 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练习时建议给定数值。
【Python】sklearn 中的 make_blobs() 函数详解相关推荐
- python getattr_Python中的getattr()函数详解:
标签:Python中的getattr()函数详解: getattr(object, name[, default]) -> value Get a named attribute from an ...
- python getattr_Python中的getattr()函数详解
最近看Dive into python第四章自省中提到getattr()函数,作为一个内建函数平时自己没怎么用过所以也不太理解这个函数的一些用法 看了下函数本身的doc getattr(object, ...
- python 闭包中的匿名函数详解!
匿名函数 孔子曰:温故而知新,可以为师矣. 天若有情天亦老,人间正道是沧桑. Python–lambda表达式 lambda表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就 ...
- python input函数详解_对Python3中的input函数详解
下面介绍python3中的input函数及其在python2及pyhton3中的不同. python3中的ininput函数,首先利用help(input)函数查看函数信息: 以上信息说明input函 ...
- Python中的bbox_overlaps()函数详解
Python中的bbox_overlaps()函数详解 想要编写自己的目标检测算法,就需要掌握bounding box(边界框)之间的关系.在这之中,bbox_overlaps()函数是一个非常实用的 ...
- timm 视觉库中的 create_model 函数详解
timm 视觉库中的 create_model 函数详解 最近一年 Vision Transformer 及其相关改进的工作层出不穷,在他们开源的代码中,大部分都用到了这样一个库:timm.各位炼丹师 ...
- java的匿名函数_JAVA语言中的匿名函数详解
本文主要向大家介绍了JAVA语言中的匿名函数详解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助. 一.使用匿名内部类 匿名内部类由于没有名字,所以它的创建方式有点儿奇怪.创建格式如下: ...
- python open写入_Python3 open() 函数详解 读取文件写入文件追加文件二进制文件
Python3 open() 函数详解 读取文件写入文件追加文件二进制文件 open()函数的主要作用是打开文件并返回相应文件对象,使用文件对象可以对当前文件进行读取.写入.追加等操作,默认情况下&q ...
- python bytearray转为byte_Python3 bytearray() 函数详解 将参数转为可变的字节数组
Python3 bytearray() 函数详解 将参数转为可变的字节数组 bytearray()函数的主要用途是将参数转换为一个新的字节数组,它是一个可变的整数序列,它的取值范围是0 <= x ...
最新文章
- 程序员注意了:这个微信群可以学英语,而且全程免费
- shell 补齐路径_bash shell:命令的文件名自动补全设置
- sap 分割评估_SAP那些事-实战篇-73-受托加工的几种方案探讨
- Flask 上下文管理-- (session,request,current_app的传递)--类似本地线程实现,以及多app应用...
- mysql之配置mysql使其可用python远程控制
- ubuntu linux 从入门到精通.pdf,UBUNTU LINUX从入门到精通(附DVD)
- 苹方字体 for linux,使用macOS苹方替换Windows 10微软雅黑
- 图片去底色怎么去?图片怎么去底色变透明?
- Spring Cloud技术栈简述
- 学完了Scratch,我要开始学Python了~~~
- css中reset属性详解,css中如何使用counter-reset属性
- Linux解压压缩包到同名目录,里面的文件会自动覆盖吗?
- 智能家居(12)——树莓派USB摄像头捕捉人脸并识别
- 盘丝洞服务器维护,梦幻西游:明日维护公告解读!盘丝法宝调整,新增人物志玩法!...
- vimdiff比较两个文件
- [GKCTF2020]CheckIN
- u盘解写保护软件usbmon
- 【Moasure魔尺】什么是运动测量
- 2W 字详解设计模式
- app打开QQ聊天对话框
热门文章
- python的继承机制
- 发现一家国产自研NAS操作系统------操作简单,内网穿透免费
- Unity学习日记--2D 精灵移动跳跃
- 乐高(LEGO)儿童数码系列电子产品
- STM32F4 读写 AT24C512问题
- 一台电脑接两个显示器,双屏显示 安装双显示器 电脑后面有两个VGA接口 干什么用的 一台主机两个显示 器怎么连接 HDMI转VGA转换器
- Adobe Acrobat DC 安装后报错1603,Microsoft Visual C++2013(x64)运行时安装失败,补丁软件被自动删除的解决办法
- VLSI/SoC设计综合实验(★)
- 逆变器5KW混合型储能逆变器资料AD版原理图,pcb,源代码
- Java+SpringBoot+vue+elementui在线答疑网站系统mysql maven毕业设计