nltk k-means聚类或k-means与纯python

时间:2013-07-05 10:39:01

标签: python nltk

虽然我已经看到了很多与此相关的问题,但我真的得不到答案可能是因为我是使用nltk群集的新手。 我真的需要一个新手聚类的基本解释,尤其是NLTK K-mean聚类的矢量表示以及如何使用它。我有一个单词列表,如[猫,狗,小猫,小狗等]和另外两个单词列表,如[食肉动物,食草动物,宠物等]和[哺乳动物,家养等]。我希望能够基于第一个单词列表使用第一个单词作为均值或质心来聚类最后两个单词列表。我试过,我收到了像这样的AssertionError:

clusterer = cluster.KMeansClusterer(2, euclidean_distance, initial_means=means)
  File "C:\Python27\lib\site-packages\nltk\cluster\kmeans.py", line 64, in __init__
    assert not initial_means or len(initial_means) == num_means

AND
    print clusterer.cluster(vectors, True)
  File "C:\Python27\lib\site-packages\nltk\cluster\util.py", line 55, in cluster
    self.cluster_vectorspace(vectors, trace)
  File "C:\Python27\lib\site-packages\nltk\cluster\kmeans.py", line 82, in cluster_vectorspace
    self._cluster_vectorspace(vectors, trace)
  File "C:\Python27\lib\site-packages\nltk\cluster\kmeans.py", line 113, in _cluster_vectorspace
    index = self.classify_vectorspace(vector)
  File "C:\Python27\lib\site-packages\nltk\cluster\kmeans.py", line 137, in classify_vectorspace
    dist = self._distance(vector, mean)
  File "C:\Python27\lib\site-packages\nltk\cluster\util.py", line 118, in euclidean_distance
    diff = u - v
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

我认为在矢量表示中有一些含义。将高度赞赏矢量表示和示例代码的基本示例。任何使用nltk或纯python的解决方案都将受到赞赏。在此先感谢您的回复

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题,这样的事情应该有用。 kmeans的难点在于找到聚类中心,如果您已找到中心或知道您想要的中心,您可以:为每个点找到到每个聚类中心的距离并将该点指定给最近的聚类中心。

(旁注sklearn是一般用于群集和机器学习的绝佳方案。)

在您的示例中,它应如下所示:

Levenstein

# levenstein function is not my implementation; I copied it from the 
# link above 
def levenshtein(s1, s2):
    if len(s1) < len(s2):
        return levenshtein(s2, s1)

    # len(s1) >= len(s2)
    if len(s2) == 0:
        return len(s1)

    previous_row = xrange(len(s2) + 1)
    for i, c1 in enumerate(s1):
        current_row = [i + 1]
        for j, c2 in enumerate(s2):
            insertions = previous_row[j + 1] + 1 # j+1 instead of j since previous_row and current_row are one character longer
            deletions = current_row[j] + 1       # than s2
            substitutions = previous_row[j] + (c1 != c2)
            current_row.append(min(insertions, deletions, substitutions))
        previous_row = current_row

    return previous_row[-1]

def get_closest_lev(cluster_center_words, my_word):
    closest_center = None
    smallest_distance = float('inf')
    for word in cluster_center_words:
        ld = levenshtein(word, my_word)
        if ld < smallest_distance:
            smallest_distance = ld
            closest_center = word
    return closest_center

def get_clusters(cluster_center_words, other_words):
    cluster_dict = {}
    for word in cluster_center_words:
        cluster_dict[word] = []
    for my_word in other_words:
        closest_center = get_closest_lev(cluster_center_words, my_word)
        cluster_dict[closest_center].append(my_word)
    return cluster_dict

示例:

cluster_center_words = ['dog', 'cat']
other_words = ['dogg', 'kat', 'frog', 'car']

结果:

>>> get_clusters(cluster_center_words, other_words)
{'dog': ['dogg', 'frog'], 'cat': ['kat', 'car']}
相关问题