TF-IDF算法如何从源码实现中提取?
摘要:TF-IDF(Term Frequency-Inverse Document Frequency),是用来衡量一个词在文档中的重要性,下面看一下TDF-IDF的公式: 首先是TF,也就是词频,用来衡量一个词在文档中出现频率的指标。假设某词在
TF-IDF(Term Frequency-Inverse Document Frequency),是用来衡量一个词在文档中的重要性,下面看一下TDF-IDF的公式:
首先是TF,也就是词频,用来衡量一个词在文档中出现频率的指标。假设某词在文档中出现了( n )次,而文档总共包含( N )个词,则该词的TF定义为:
注意:(t,d)中的t表示的是文档中的词汇,d表示的是文档的词汇集合,通过计算TF也就是进行词频率的统计,好的,那么看一下代码的实现。
def compute_tf(word_dict, doc_words):
"""
:param word_dict: 字符的统计个数
:param doc_words: 文档中的字符集合
:return:
"""
tf_dict = {}
words_len = len(doc_words)
for word_i, count_i in word_dict.items():
tf_dict[word_i] = count_i / words_len
return tf_dict
# 示例文档
doc1 = "this is a sample"
doc2 = "this is another example example example"
doc3 = "this is a different example example"
# 分割单词
doc1_words = doc1.split()
doc2_words = doc2.split()
doc3_words = doc3.split()
# 计算每个文档的词频
word_dict1 = Counter(doc1_words)
word_dict2 = Counter(doc2_words)
word_dict3 = Counter(doc3_words)
# 计算TF
tf1 = compute_tf(word_dict1, doc1_words)
tf2 = compute_tf(word_dict2, doc2_words)
tf3 = compute_tf(word_dict3, doc3_words)
print(f'tf1:{tf1}')
print(f'tf2:{tf2}')
print(f'tf3:{tf3}')
# tf1:{'this': 0.25, 'is': 0.25, 'a': 0.25, 'sample': 0.25}
# tf2:{'this': 0.16666666666666666, 'is': 0.16666666666666666, 'another': 0.16666666666666666, 'example': 0.5}
# tf3:{'this': 0.16666666666666666, 'is': 0.16666666666666666, 'a': 0.16666666666666666, 'different': 0.16666666666666666, 'example': 0.3333333333333333}
看完TF的计算之后,我们看一下IDF的定义,公式和对应的实现吧,IDF的定义是:即逆文档频率,反映了词的稀有程度,IDF越高,说明词越稀有。这个逆文档频率也就是说一个词的文档集合中出现的次数越少,他就越具有表征型,因为在文中有很多“的”,“了”这种词,这些词重要性不大,反而出现少的词重要性大一点,来看一下IDF的公式:
其中,( D )是文档总数,( df_t )是包含词( t )的文档数量。
