如何为 CountVectorizer 增加单词的权重
How to increase weight of a word for CountVectorizer
我有一个已标记化的文档,然后我使用另一个文档并通过计算它们的 余弦相似度.
来比较两者
但是,在我计算它们的相似度之前,我想预先增加其中一个词的权重。我正在考虑通过将该词的计数加倍来实现此目的,但我不知道该怎么做。
假设我有以下...
text = [
"This is a test",
"This is something else",
"This is also a test"
]
test = ["This is something"]
接下来我定义停用词并为两组文档调用 CountVectorizer
。
stopWords = set(stopwords.words('english'))
vectorizer = CountVectorizer(stop_words=stopWords)
trainVectorizerArray = vectorizer.fit_transform(text).toarray()
testVectorizerArray = vectorizer.transform(test).toarray()
在下一部分中,我计算 余弦相似度...
cosine_function = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)
for vector in trainVectorizerArray:
print(vector)
for testV in testVectorizerArray:
print(testV)
cosine = cosine_function(vector, testV)
print(cosine)
但是,在计算相似度之前,如何增加其中一个词的权重。假设在这个例子中我想增加something
的权重,我该怎么做呢?我认为你通过增加字数来做到这一点,但我不知道如何增加它。
我认为最简单的方法是将 CountVectorizer
的 get_feature_names
函数与 scipy.spatial.distance
中的 cosine
函数结合使用。但请注意,这会计算余弦距离而不是相似度,因此如果您只对相似度感兴趣,则必须使用 similarity = 1-distance
。使用你的例子
from scipy.spatial.distance import cosine
import numpy as np
word_weights = {'something': 2}
feature_names = vectorizer.get_feature_names()
weights = np.ones(len(feature_names))
for key, value in word_weights.items():
weights[feature_names.index(key)] = value
for vector in trainVectorizerArray:
print(vector)
for testV in testVectorizerArray:
print(testV)
cosine_unweight = cosine(vector, testV)
cosine_weighted = cosine(vector, testV, w=weights)
print(cosine_unweight, cosine_weighted)
应要求对 word_weights
词典进行更多解释。这是您分配给其他词的权重。除非您在 word_weights
字典中添加一个条目,否则每个权重都设置为 1
,因此 word_weights = {'test': 0}
将从余弦相似度中删除 "test",但是 word_weights = {'test': 1.5}
与其他词相比,权重会增加 50%。如果需要,您也可以包含多个条目,例如 word_weights = {'test': 1.5, 'something': 2}
将调整 "test" 和 "something" 与其他词相比的权重。
我有一个已标记化的文档,然后我使用另一个文档并通过计算它们的 余弦相似度.
来比较两者但是,在我计算它们的相似度之前,我想预先增加其中一个词的权重。我正在考虑通过将该词的计数加倍来实现此目的,但我不知道该怎么做。
假设我有以下...
text = [
"This is a test",
"This is something else",
"This is also a test"
]
test = ["This is something"]
接下来我定义停用词并为两组文档调用 CountVectorizer
。
stopWords = set(stopwords.words('english'))
vectorizer = CountVectorizer(stop_words=stopWords)
trainVectorizerArray = vectorizer.fit_transform(text).toarray()
testVectorizerArray = vectorizer.transform(test).toarray()
在下一部分中,我计算 余弦相似度...
cosine_function = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)
for vector in trainVectorizerArray:
print(vector)
for testV in testVectorizerArray:
print(testV)
cosine = cosine_function(vector, testV)
print(cosine)
但是,在计算相似度之前,如何增加其中一个词的权重。假设在这个例子中我想增加something
的权重,我该怎么做呢?我认为你通过增加字数来做到这一点,但我不知道如何增加它。
我认为最简单的方法是将 CountVectorizer
的 get_feature_names
函数与 scipy.spatial.distance
中的 cosine
函数结合使用。但请注意,这会计算余弦距离而不是相似度,因此如果您只对相似度感兴趣,则必须使用 similarity = 1-distance
。使用你的例子
from scipy.spatial.distance import cosine
import numpy as np
word_weights = {'something': 2}
feature_names = vectorizer.get_feature_names()
weights = np.ones(len(feature_names))
for key, value in word_weights.items():
weights[feature_names.index(key)] = value
for vector in trainVectorizerArray:
print(vector)
for testV in testVectorizerArray:
print(testV)
cosine_unweight = cosine(vector, testV)
cosine_weighted = cosine(vector, testV, w=weights)
print(cosine_unweight, cosine_weighted)
应要求对 word_weights
词典进行更多解释。这是您分配给其他词的权重。除非您在 word_weights
字典中添加一个条目,否则每个权重都设置为 1
,因此 word_weights = {'test': 0}
将从余弦相似度中删除 "test",但是 word_weights = {'test': 1.5}
与其他词相比,权重会增加 50%。如果需要,您也可以包含多个条目,例如 word_weights = {'test': 1.5, 'something': 2}
将调整 "test" 和 "something" 与其他词相比的权重。