【问题标题】:clustering group of word semantically语义聚类词组
【发布时间】:2013-12-10 19:27:13
【问题描述】:

我有一个关于根据它们的相似性将单词聚类成多个组/簇的问题。 相似度实际上是使用 WordNet 词法数据库衡量的语义相似度。在提取和计算语义相似度后,我得到了 n*n 对称矩阵,形式为:

A B C D 1 0.2 0.5 0.0 B 0.2 1 0.0 0.3 C 0.5 0.0 1 0.8 D 0.0 0.3 0.8 1

矩阵由从大规模数据集中提取的一万个单词构成。

我的问题是(这很简单,因为我对集群的了解很少),可以用于此目的的适当集群技术是什么?他们是否有任何 java 工具可以做到这一点,甚至是 excel 中的工具包,所以可以直接使用?教程?真诚感谢任何帮助..

【问题讨论】:

  • 你的意思是语义相似度越高,同义词的值越高?

标签: hierarchical-clustering


【解决方案1】:

有多种层次聚类技术,其中大多数可能适合您的问题。两种主要类型是自下而上(凝聚)和自上而下(分裂)。一般来说,自下而上更容易实现,因此使用更频繁,而自上而下可以以更有效的方式实现。除非速度对您来说是一个主要问题,否则您最好采用自下而上的方式,特别是因为您是新手。在某些情况下,一种方法可能优于另一种方法,但这往往很大程度上取决于您的确切数据。

区分不同算法的主要其他特征是如何计算集群之间的距离。请注意,大多数聚类算法使用距离,而您有相似度值。距离和相似度本质上是互逆的,所以you can get distance with 1/(similarity+1),或者,因为你的相似度都小于或等于1,你可以计算距离为1 - similarity。计算簇间距离最常用的技术是:

  • “平均链接” - 两个集群中心之间的距离。这可以通过取两组中每对成员之间距离的平均值来计算。
  • “单链接” - 两个集群中最近的两个成员之间的距离
  • “完整链接”- 两个集群中最远的两个成员之间的距离。

UPGMA 是这种算法的一个经典例子,如果你在 google 上搜索,就会发现各种各样的 java 实现(我无法评论哪个更好或更坏,因为我不太了解 Java )。 MultidendrogramsHAC 都是更通用的层次凝聚聚类的 Java 实现。

【讨论】:

  • 这真的很棒@seaotternerd!
【解决方案2】:

K 最近邻会为共现矩阵生成非常好的聚类,例如您那里的矩阵。我整理了一个快速的blog post 使用 K 均值对语义相似的词进行聚类,但这里是快速代码:

from __future__ import division
from sklearn.cluster import KMeans
from numbers import Number
from pandas import DataFrame
import sys, codecs, numpy

class autovivify_list(dict):
        '''Pickleable class to replicate the functionality of collections.defaultdict'''
        def __missing__(self, key):
                value = self[key] = []
                return value

        def __add__(self, x):
                '''Override addition for numeric types when self is empty'''
                if not self and isinstance(x, Number):
                        return x
                raise ValueError

        def __sub__(self, x):
                '''Also provide subtraction method'''
                if not self and isinstance(x, Number):
                        return -1 * x
                raise ValueError

def build_word_vector_matrix(vector_file, n_words):
        '''Iterate over the GloVe array read from sys.argv[1] and return its vectors and labels as arrays'''
        numpy_arrays = []
        labels_array = []
        with codecs.open(vector_file, 'r', 'utf-8') as f:
                for c, r in enumerate(f):
                        sr = r.split()
                        labels_array.append(sr[0])
                        numpy_arrays.append( numpy.array([float(i) for i in sr[1:]]) )

                        if c == n_words:
                                return numpy.array( numpy_arrays ), labels_array

        return numpy.array( numpy_arrays ), labels_array

def find_word_clusters(labels_array, cluster_labels):
        '''Read in the labels array and clusters label and return the set of words in each cluster'''
        cluster_to_words = autovivify_list()
        for c, i in enumerate(cluster_labels):
                cluster_to_words[ i ].append( labels_array[c] )
        return cluster_to_words

if __name__ == "__main__":

        input_vector_file = sys.argv[1]
        n_words           = int(sys.argv[2])
        reduction_factor  = float(sys.argv[3])
        clusters_to_make  = int( n_words * reduction_factor )
        df, labels_array  = build_word_vector_matrix(input_vector_file, n_words)
        kmeans_model      = KMeans(init='k-means++', n_clusters=clusters_to_make, n_init=10)
        kmeans_model.fit(df)

        cluster_labels    = kmeans_model.labels_
        cluster_inertia   = kmeans_model.inertia_
        cluster_to_words  = find_word_clusters(labels_array, cluster_labels)

        for c in cluster_to_words:
                print cluster_to_words[c]

如果将此脚本保存为 cluster_vectors.py,则可以运行:

wget http://www-nlp.stanford.edu/data/glove.6B.300d.txt.gz
gunzip glove.6B.300d.txt.gz
python cluster_vectors.py glove.6B.300d.txt 10000 .1

读取 GloVe 词向量的前 10000 行(从术语共现推断的语义词向量)并将这些词聚类到 10000 * .1 = 1000 个聚类中。集群将如下所示:

[u'Chicago', u'Boston', u'Houston', u'Atlanta', u'Dallas', u'Denver', u'Philadelphia', u'Baltimore', u'Cleveland', u'Pittsburgh', u'Buffalo', u'Cincinnati', u'Louisville', u'Milwaukee', u'Memphis', u'Indianapolis', u'Auburn', u'Dame']

[u'Product', u'Products', u'Shipping', u'Brand', u'Customer', u'Items', u'Retail', u'Manufacturer', u'Supply', u'Cart', u'SKU', u'Hardware', u'OEM', u'Warranty', u'Brands']

[u'home', u'house', u'homes', u'houses', u'housing', u'offices', u'household', u'acres', u'residence']

[...]

[u'Night', u'Disney', u'Magic', u'Dream', u'Ultimate', u'Fantasy', u'Theme', u'Adventure', u'Cruise', u'Potter', u'Angels', u'Adventures', u'Dreams', u'Wonder', u'Romance', u'Mystery', u'Quest', u'Sonic', u'Nights']

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 2012-11-25
    • 2013-04-19
    • 2014-08-06
    • 2014-02-19
    • 2021-09-05
    • 2016-02-16
    • 2011-01-28
    相关资源
    最近更新 更多