脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - Python sklearn 中的 make_blobs() 函数示例详解

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

2023-02-28 11:32旅途中的宽~ Python

make_blobs() 是 sklearn.datasets中的一个函数,这篇文章主要介绍了Python sklearn 中的 make_blobs() 函数,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

一、介绍

make_blobs() 是 sklearn.datasets中的一个函数。

主要是产生聚类数据集,产生一个数据集和相应的标签。

函数的源代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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

二、函数的使用

?
1
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() 函数内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/wzk4869/article/details/129117675

延伸 · 阅读

精彩推荐
  • PythonPython实现pdf文档转txt的方法示例

    Python实现pdf文档转txt的方法示例

    这篇文章主要介绍了Python实现pdf文档转txt的方法,结合实例形式分析了Python基于第三方库pdfminier实现针对pdf格式文档的读取、转换等相关操作技巧,需要的朋...

    肥宝Fable6562021-01-06
  • PythonPython中使用Beautiful Soup库的超详细教程

    Python中使用Beautiful Soup库的超详细教程

    这篇文章主要介绍了Python中使用Beautiful Soup库的超详细教程,示例代码基于Python2.x版本,极力推荐!需要的朋友可以参考下...

    崔庆才2292020-06-19
  • Pythonpython读取txt文件,去掉空格计算每行长度的方法

    python读取txt文件,去掉空格计算每行长度的方法

    今天小编就为大家分享一篇python读取txt文件,去掉空格计算每行长度的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    weixin_390120479122021-05-05
  • Python7个关于Python的经典基础案例

    7个关于Python的经典基础案例

    这篇文章主要给大家分享 7个关于Python的经典基础案例,列表排序、调换字典键值、删除列表中的重复元素、输出质数、判断是一年中第几天、猜数字、进...

    小小程序员ol9742022-02-25
  • Python解决python中画图时x,y轴名称出现中文乱码的问题

    解决python中画图时x,y轴名称出现中文乱码的问题

    今天小编就为大家分享一篇解决python中画图时x,y轴名称出现中文乱码的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Pywin6912021-05-24
  • PythonPYQT5 实现界面的嵌套方式

    PYQT5 实现界面的嵌套方式

    这篇文章主要介绍了PYQT5 实现界面的嵌套方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    leonrun6152021-09-19
  • PythonPython中os.path用法分析

    Python中os.path用法分析

    这篇文章主要介绍了Python中os.path用法,实例分析了os.path的各种常用方法,具有一定参考借鉴价值,需要的朋友可以参考下 ...

    脚本之家5182020-05-19
  • Pythonpandas 查询函数query的用法说明

    pandas 查询函数query的用法说明

    这篇文章主要介绍了pandas 查询函数query的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    yyyyyyyyyyang12392021-09-15