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

 更新时间:2023年02月22日 11:33:00   作者:旅途中的宽~  
make_blobs() 是 sklearn.datasets中的一个函数,这篇文章主要介绍了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练习时建议给定数值。

到此这篇关于Python sklearn 中的 make_blobs() 函数详解的文章就介绍到这了,更多相关Python sklearn make_blobs() 函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python中单下划线与双下划线的区别及说明

    python中单下划线与双下划线的区别及说明

    这篇文章主要介绍了python中单下划线与双下划线的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • 解决Python 写文件报错TypeError的问题

    解决Python 写文件报错TypeError的问题

    这篇文章主要介绍了解决Python 写文件报错TypeError的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • Python3常用内置方法代码实例

    Python3常用内置方法代码实例

    这篇文章主要介绍了Python3常用内置方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • python模拟点击在ios中实现的实例讲解

    python模拟点击在ios中实现的实例讲解

    在本篇文章里小编给大家整理的是一篇关于python模拟点击在ios中实现的实例讲解内容,有需要的朋友们可以参考下。
    2020-11-11
  • 在python中,使用scatter绘制散点图的实例

    在python中,使用scatter绘制散点图的实例

    今天小编就为大家分享一篇在python中,使用scatter绘制散点图的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • 快速解决pandas.read_csv()乱码的问题

    快速解决pandas.read_csv()乱码的问题

    今天小编就为大家分享一篇快速解决pandas.read_csv()乱码的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • 简单了解django缓存方式及配置

    简单了解django缓存方式及配置

    这篇文章主要介绍了简单了解django缓存概述及配置,由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,需要的朋友可以参考下
    2019-07-07
  • Python 基础教程之闭包的使用方法

    Python 基础教程之闭包的使用方法

    这篇文章主要介绍了Python 基础教程之闭包的使用方法的相关资料,希望大家通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • Python 下载及安装详细步骤

    Python 下载及安装详细步骤

    这篇文章主要介绍了载及安装Python详细步骤,安装python分三个步骤,具体安装方法本文给大家介绍的非常详细,需要的朋友可以参考下
    2019-11-11
  • Python3多线程版TCP端口扫描器

    Python3多线程版TCP端口扫描器

    这篇文章主要为大家详细介绍了Python3多线程版TCP端口扫描器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08

最新评论