无法获得杂乱的层次聚类

2024-05-17 10:18:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个简单的脚本,旨在对一个简单的测试数据集进行层次聚类。The test data that was used.

我发现函数fclusterdata是将数据分为两个簇的候选函数。它需要两个必需的调用参数:数据集和阈值。 问题是,我找不到一个阈值来生成预期的两个集群。在

如果有人能告诉我我做错了什么,我会很高兴的。如果有人能指出更适合我的集群的其他方法,我也会很高兴(我明确地希望避免事先指定集群的数量)

这是我的代码:

import time
import scipy.cluster.hierarchy as hcluster
import numpy.random as random
import numpy

import pylab
pylab.ion()

data = random.randn(2,200)

data[:100,:100] += 10

for i in range(5,15):
    thresh = i/10.
    clusters = hcluster.fclusterdata(numpy.transpose(data), thresh)
    pylab.scatter(*data[:,:], c=clusters)
    pylab.axis("equal")
    title = "threshold: %f, number of clusters: %d" % (thresh, len(set(clusters)))
    print title
    pylab.title(title)
    pylab.draw()
    time.sleep(0.5)
    pylab.clf()

输出如下:

^{pr2}$

Tags: 数据函数importnumpydatatimetitleas
1条回答
网友
1楼 · 发布于 2024-05-17 10:18:01

请注意,function reference有一个错误。t参数的正确定义是:“集群函数的截止阈值或最大集群数(criterian='maxclust')”。在

所以试试这个:

clusters = hcluster.fclusterdata(numpy.transpose(data), 2, criterion='maxclust', metric='euclidean', depth=1, method='centroid')

相关问题 更多 >