编辑2:我对我的问题想得更好,并意识到这是概括的方式,这只是一个基本问题;
从手套文件 (glove.6B.300d.txt( 创建一个新数组,其中仅包含我在文档中的单词列表。
我知道这实际上与这个特定的 GloVe 文件无关,我应该学习如何对任何两个单词列表进行操作......
我假设我只是不知道如何正确地寻找这个来学习如何执行这部分,即我应该寻找什么库/函数/buuzzwords。
编辑 1:我正在添加我使用的代码,该代码适用于整个 GloVe 库;
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):
def __missing__(self, key):
value = self[key] = []
return value
def __add__(self, x):
if not self and isinstance(x, Number):
return x
raise ValueError
def __sub__(self, x):
if not self and isinstance(x, Number):
return -1 * x
raise ValueError
def build_word_vector_matrix(vector_file, n_words):
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):
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 =
'/Users/.../Documents/GloVe/glove.6B/glove.6B.300d.txt'
n_words = 1000
reduction_factor = 0.5
n_clusters = 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=n_clusters,
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]
print "n"
原始问题:
假设我有一个特定的文本(比如 500 个单词(。 我想执行以下操作:
- 创建此文本中所有单词的嵌入(即仅包含这 500 个单词的 GloVe 向量列表(
- 集群它(*我知道该怎么做(
我该怎么做这样的事情?
这是一个非常简单的问题。从你的描述中,我推断你有 500 个单词,并且你有可用的向量。我建议您前往Scikit学习库,并为该任务应用一种标准聚类方法。我建议从K-means开始。使用以下链接在Scikit-learn中选择正确的方法:https://scikit-learn.org/stable/tutorial/machine_learning_map/index.html