gpt4 book ai didi

python - 有没有办法计算 make_blobs 生成的数据集的 cluster_std?

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:19 33 4
gpt4 key购买 nike

make_blobs() 用于生成用于聚类的各向同性高斯 Blob 。

参数 cluster_std 是集群的标准偏差。

我生成了一个数据集:

x, y = make_blobs(n_samples=100, centers=6,
cluster_std=0.60, random_state=1234)

并且正在尝试计算标准差:

np.std(x)

输出

5.122249276993561

与初始参数 0.60 相去甚远。

有没有办法正确计算标准偏差?

最佳答案

如果我们在 np.std() 中不提及 axis 值,那么所有数据点将合并到一个数组中,然后在计算中计算标准差.

来自 Documentation :

axis : None or int or tuple of ints, optional Axis or axes along which
the standard deviation is computed. The default is to compute the
standard deviation of the flattened array.

即使我们提到轴,我们也不会得到想要的结果

np.std(x,axis=0)
array([5.51732287, 4.27190484])

原因是标准偏差,我们之前提供的是针对每个集群而不是整个数据集。

来自 Documentation:

cluster_std : float or sequence of floats, optional (default=1.0) The
standard deviation of the clusters.

现在,如果我们计算每个集群的标准差:

>>> sample_size =  100
>>> x, y = make_blobs(n_samples=sample_size, centers=6,
cluster_std=0.60, random_state=1234)
>>> for i in range(6):
>>> print(np.std(x[y==i], axis=0))

[0.34529312 0.71426966]
[0.50416947 0.62691032]
[0.41684885 0.69941778]
[0.5760022 0.58054362]
[0.59779626 0.52716869]
[0.64628073 0.49280287]

不过,这些值并不总是接近给定值 0.60

现在,计算统计部分!只有当我们增加样本量时,我们才能看到样本标准偏差变得接近总体标准偏差(这是我们之前指定的值)。

如果我们将 sample_size 设置为 10,000,000,结果似乎非常接近!!

[0.600691   0.60049266]
[0.60009299 0.60028479]
[0.60048685 0.60019785]
[0.60000098 0.60000844]
[0.59989123 0.60017014]
[0.60010969 0.59936852]

关于python - 有没有办法计算 make_blobs 生成的数据集的 cluster_std?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56535509/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com