Python sklearn 中的 make_blobs() 函数示例详解

作者:旅途中的宽~ 时间:2022-07-24 21:45:48 

一、介绍

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

标签:Python,sklearn,make,blobs
0
投稿

猜你喜欢

  • Python机器学习之scikit-learn库中KNN算法的封装与使用方法

    2021-04-05 15:32:40
  • PHP判断数组是否为空的常用方法(五种方法)

    2024-05-11 09:25:53
  • Python使用内置json模块解析json格式数据的方法

    2023-07-30 14:10:45
  • Django contenttypes 框架详解(小结)

    2023-11-13 14:39:47
  • Python浮点数取整、格式化和NaN处理的操作方法

    2023-01-12 11:41:19
  • Python上传package到Pypi(代码简单)

    2022-04-21 17:09:33
  • Python使用openpyxl模块处理Excel文件

    2021-10-03 06:45:10
  • CSS模块化设计—从空格谈起

    2007-12-15 09:41:00
  • 基于Bootstrap分页的实例讲解(必看篇)

    2024-04-16 10:31:58
  • Python列表常用函数使用详解

    2021-08-21 01:52:05
  • python机器学习之神经网络(一)

    2023-06-21 23:28:42
  • python os.listdir()乱码解决方案

    2021-09-20 02:52:42
  • vue-music关于Player播放器组件详解

    2024-04-28 09:26:11
  • python日志模块loguru详解

    2023-10-23 20:50:08
  • python文字和unicode/ascll相互转换函数及简单加密解密实现代码

    2023-08-23 08:13:59
  • uni-app入门页面布局之window和tabbar详解

    2024-04-08 10:53:33
  • python使用chardet判断字符串编码的方法

    2023-02-02 06:45:20
  • python 图像插值 最近邻、双线性、双三次实例

    2023-01-09 07:48:56
  • JS遮罩层效果 兼容ie firefox jQuery遮罩层

    2024-02-24 23:04:24
  • Oracle与SQL Server在企业应用的比较

    2024-01-25 07:22:11
  • asp之家 网络编程 m.aspxhome.com