比较 NumPy 数组的相似性
Comparing NumPy Arrays for Similarity
我有一个形状为 (300,) 的目标 NumPy 数组和一组形状也为 (300,) 的候选数组。这些数组是单词的 Word2Vec 表示;我正在尝试使用它们的向量表示找到与目标词最相似的候选词。找到与目标词最相似的候选词的最佳方法是什么?
一种方法是将目标词和候选词之间的逐元素差异的绝对值相加,然后 select 具有最低总体绝对差异的候选词。例如:
candidate_1_difference = np.subtract(target_vector, candidate_vector)
candidate_1_abs_difference = np.absolute(candidate_1_difference)
candidate_1_total_difference = np.sum(candidate_1_abs_difference)
然而,这看起来很笨拙而且可能是错误的。执行此操作的更好方法是什么?
编辑以包含示例向量:
import numpy as np
import gensim
path = 'https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz'
def func1(path):
#Limited to 50K words to reduce load time
model = gensim.models.KeyedVectors.load_word2vec_format(path, binary=True, limit=50000)
context = ['computer','mouse','keyboard']
candidates = ['office','house','winter']
vectors_to_sum = []
for word in context:
vectors_to_sum.append(model.wv[word])
target_vector = np.sum(vectors_to_sum)
candidate_vector = candidates[0]
candidate_1_difference = np.subtract(target_vector, candidate_vector)
candidate_1_abs_difference = np.absolute(candidate_1_difference)
candidate_1_total_difference = np.sum(candidate_1_abs_difference)
return candidate_1_total_difference
你说的基本正确。您正在计算 L1 范数,它是绝对差的总和。另一个更常见的选择是计算欧几里德范数或 L2 范数,这是熟悉的平方和平方根的距离度量。
您可以使用 numpy.linalg.norm
来计算不同的范数,默认情况下计算向量的 L-2 范数。
distance = np.linalg.norm(target_vector - candidate_vector)
如果你有一个目标向量和多个候选向量存储在一个列表中,上面仍然有效,但你需要为范数指定轴,然后你得到一个范数向量,每个候选向量一个。
对于候选向量列表:
distance = np.linalg.norm(target_vector - np.array(candidate_vector), axis=1)
我有一个形状为 (300,) 的目标 NumPy 数组和一组形状也为 (300,) 的候选数组。这些数组是单词的 Word2Vec 表示;我正在尝试使用它们的向量表示找到与目标词最相似的候选词。找到与目标词最相似的候选词的最佳方法是什么?
一种方法是将目标词和候选词之间的逐元素差异的绝对值相加,然后 select 具有最低总体绝对差异的候选词。例如:
candidate_1_difference = np.subtract(target_vector, candidate_vector)
candidate_1_abs_difference = np.absolute(candidate_1_difference)
candidate_1_total_difference = np.sum(candidate_1_abs_difference)
然而,这看起来很笨拙而且可能是错误的。执行此操作的更好方法是什么?
编辑以包含示例向量:
import numpy as np
import gensim
path = 'https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz'
def func1(path):
#Limited to 50K words to reduce load time
model = gensim.models.KeyedVectors.load_word2vec_format(path, binary=True, limit=50000)
context = ['computer','mouse','keyboard']
candidates = ['office','house','winter']
vectors_to_sum = []
for word in context:
vectors_to_sum.append(model.wv[word])
target_vector = np.sum(vectors_to_sum)
candidate_vector = candidates[0]
candidate_1_difference = np.subtract(target_vector, candidate_vector)
candidate_1_abs_difference = np.absolute(candidate_1_difference)
candidate_1_total_difference = np.sum(candidate_1_abs_difference)
return candidate_1_total_difference
你说的基本正确。您正在计算 L1 范数,它是绝对差的总和。另一个更常见的选择是计算欧几里德范数或 L2 范数,这是熟悉的平方和平方根的距离度量。
您可以使用 numpy.linalg.norm
来计算不同的范数,默认情况下计算向量的 L-2 范数。
distance = np.linalg.norm(target_vector - candidate_vector)
如果你有一个目标向量和多个候选向量存储在一个列表中,上面仍然有效,但你需要为范数指定轴,然后你得到一个范数向量,每个候选向量一个。
对于候选向量列表:
distance = np.linalg.norm(target_vector - np.array(candidate_vector), axis=1)