2

我在关键字之间有大量相似性的 csv,我想将其转换为三角距离矩阵(因为它非常大并且稀疏会更好)以使用 scipy 执行层次聚类。我当前的数据 csv 看起来像:

a,  b, 1
b,  a, 1
c,  a, 2
a,  c, 2

我不知道如何做到这一点,也找不到任何简单的 Python 集群教程。

谢谢你的帮助!

4

1 回答 1

3

这个问题有两个部分:

  • 如何将这种格式的 CSV 中的距离加载到(可能是稀疏的)三角距离矩阵中?

  • 给定一个三角距离矩阵,你如何用 scipy 进行层次聚类?

如何加载数据:我不认为scipy.cluster.hierarchy适用于稀疏数据,所以让我们做密集。我也打算把它做成完整的方阵,然后取 scipy 想要的上三角形,出于懒惰;如果你更聪明的话,你可以直接索引到压缩版本。

from collections import defaultdict
import csv
import functools
import itertools
import numpy as np

# name_to_id associates a name with an integer 0, 1, ...
name_to_id = defaultdict(functools.partial(next, itertools.count()))

with open('file.csv') as f:
    reader = csv.reader(f)

    # do one pass over the file to get all the IDs so we know how 
    # large to make the matrix, then another to fill in the data.
    # this takes more time but uses less memory than loading everything
    # in in one pass, because we don't know how large the matrix is; you
    # can skip this if you do know the number of elements from elsewhere.
    for name_a, name_b, dist in reader:
        idx_a = name_to_id[name_a]
        idx_b = name_to_id[name_b]

    # make the (square) distances matrix
    # this should really be triangular, but the formula for 
    # indexing into that is escaping me at the moment
    n_elem = len(name_to_id)
    dists = np.zeros((n_elem, n_elem))

    # go back to the start of the file and read in the actual data
    f.seek(0)
    for name_a, name_b, dist in reader:
        idx_a = name_to_id[name_a]
        idx_b = name_to_id[name_b]
        dists[(idx_a, idx_b) if idx_a < idx_b else (idx_b, idx_a)] = dist

condensed = dists[np.triu_indices(n_elem, 1)]

然后调用例如scipy.cluster.hierarchy.linkagecondensed要将索引映射回名称,您可以使用类似

id_to_name = dict((id, name) for name, id in name_to_id.iteritems())
于 2012-09-13T03:19:54.070 回答