首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scipy.cluster.hierarchy.fclusterdata +距离度量

Scipy.cluster.hierarchy.fclusterdata +距离度量
EN

Stack Overflow用户
提问于 2010-03-31 01:53:34
回答 1查看 3.4K关注 0票数 6

1)我正在使用scipy的hcluster模块。

所以我能控制的变量是阈值变量。我如何知道我的每个阈值的性能?也就是说,在Kmeans中,这种表现将是所有点到其质心的总和。当然,这必须进行调整,因为更多的簇通常=更小的距离。

对于这一点,我可以用hcluster做一些观察吗?

2)我意识到fclusterdata有很多可用的指标。我是基于关键术语的tf-idf对文本文档进行聚类。问题是,一些文档比其他文档更长,我认为余弦是“标准化”这个长度问题的好方法,因为文档越长,如果它们的内容是一致的,那么它在n维域中的“方向”应该保持不变。还有没有其他方法可以推荐呢?我如何评估?

Thx

EN

回答 1

Stack Overflow用户

发布于 2010-05-26 20:50:53

可以计算集群中x的平均距离|x -集群中心|,就像K-means一样。下面的代码实现了这一暴力行为。(它必须是scipy.cluster或scipy.spatial.distance中的内置文件,但我也找不到它。)

在你的问题2上,通过。欢迎任何关于分层聚类的优秀教程的链接。

代码语言:javascript
复制
#!/usr/bin/env python
""" cluster cities: pdist linkage fcluster plot
    util: clusters() avdist()
"""

from __future__ import division
import sys
import numpy as np
import scipy.cluster.hierarchy as hier  # $scipy/cluster/hierarchy.py
import scipy.spatial.distance as dist
import pylab as pl
from citiesin import citiesin  # 1000 US cities

__date__ = "27may 2010 denis"

def clusterlists(T):
    """ T = hier.fcluster( Z, t ) e.g. [a b a b a c]
        -> [ [0 2 4] [1 3] [5] ] sorted by len
    """
    clists = [ [] for j in range( max(T) + 1 )]
    for j, c in enumerate(T):
        clists[c].append( j )
    clists.sort( key=len, reverse=True )
    return clists[:-1]  # clip the []

def avdist( X, to=None ):
    """ av dist X vecs to "to", None: mean(X) """
    if to is None:
        to = np.mean( X, axis=0 )
    return np.mean( dist.cdist( X, [to] ))

#...............................................................................
Ndata = 100
method = "average"
t = 0
crit = "maxclust"
    # 'maxclust': Finds a minimum threshold `r` so that the cophenetic distance
    # between any two original observations in the same flat cluster
    # is no more than `r` and no more than `t` flat clusters are formed.
    # but t affects cluster sizes only weakly ?
    # t 25: [10, 9, 8, 7, 6
    # t 20: [12, 11, 10, 9, 7
plot = 0
seed = 1

exec "\n".join( sys.argv[1:] )  # Ndata= t= ...
np.random.seed(seed)
np.set_printoptions( 2, threshold=100, edgeitems=10, suppress=True )  # .2f
me = __file__.split('/') [-1]

    # biggest US cities --
cities = np.array( citiesin( n=Ndata )[0] )  # N,2

if t == 0:  t = Ndata // 4

#...............................................................................
print "# %s  Ndata=%d  t=%d  method=%s  crit=%s " % (me, Ndata, t, method, crit)

Y = dist.pdist( cities )  # n*(n-1) / 2
Z = hier.linkage( Y, method )  # n-1
T = hier.fcluster( Z, t, criterion=crit )  # n

clusters = clusterlists(T)
print "cluster sizes:", map( len, clusters )
print "# average distance to centre in the biggest clusters:"
for c in clusters:
    if len(c) < len(clusters[0]) // 3:  break
    cit = cities[c].T
    print "%.2g %s" % (avdist(cit.T), cit)
    if plot:
        pl.plot( cit[0], cit[1] )

if plot:
    pl.title( "scipy.cluster.hierarchy of %d US cities, %s t=%d" % (
        Ndata, crit, t) )
    pl.grid(False)
    if plot >= 2:
        pl.savefig( "cities-%d-%d.png" % (Ndata, t), dpi=80 )
    pl.show()
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2547391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档