当前位置:   article > 正文

推荐系统之TF-IDF算法实现_tfidf聊天推荐

tfidf聊天推荐

推荐系统之TF-IDF算法实现

词频-逆文档频率(TF-IDF)是一种用于资讯检索与文本挖掘的常加权技术。该技术是一种统计方法,用以评估一个字词对于一个文件集或一个语料库中一个文件的重要程度。字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降。

如下公式:TF是字词在文件中出现的频率,即词频;IDF是字词在语料库中出现的频率,即逆文档频率。

在这里插入图片描述
下面我们看一下词频TF的计算公式,分子表示i在j中出现的次数,分母表示文档j的总词数。
在这里插入图片描述
我们再看一下逆向文件频率的公式,其中N表示文档集中文档总数,Ni表示文档集中包含词i的文档数,加1的目的方式分子或者分母为0.
在这里插入图片描述

代码如下:
我们给出两句话:docA = 'The cat sat on my bed' docB = 'The dog sat on my knees',分别求这两句话的词频-逆文档频率TF-IDF,判断每个词的重要性。

步骤大概如下:

1-首先将两句话合并起来,构建为词库wordSet。
2-wordDictA统计第一句话词出现的次数,wordDictB统计第二句话词出现的次数。
3-computeTF()方法计算词频 TF,computeIDF()方法计算逆文档频率IDF,TFIDF()方法计算词频-逆文档频率(TF-IDF),TF-IDF越大说明该词的重要性越大。

import numpy as np
import pandas as pd

docA = 'The cat sat on my bed'
docB = 'The dog sat on my knees'

bowA = docA.split(" ")
bowB = docB.split(" ")

#构建词库
wordSet = set(bowA).union(set(bowB))
print(wordSet)

#进行词数统计
#用统计字典保存词出现的次数,初始化为0
wordDictA = dict.fromkeys(wordSet, 0)
wordDictB = dict.fromkeys(wordSet, 0)

#遍历文档,统计次数
for word in bowA:
    wordDictA[word] += 1
for word in bowB:
    wordDictB[word] += 1

print(pd.DataFrame([wordDictA,wordDictB]))
#计算TF
def computeTF(wordDict, bow):
    #用一个字典对象记录TF,把所有词对应在文档里的TF都算出来。
    tfDict = {}
    bowCount = len(bow)
    for word, count in wordDict.items():
        tfDict[word] = count / bowCount
    return tfDict

tfA = computeTF(wordDictA, bowA)
tfB = computeTF(wordDictB, bowB)
print(tfA)
print(tfB)

#计算逆文档频率
def computeIDF(wordDictList):
    #用一个字典对象来保存idf对象,每个词作为key,初始值为0
    idfDict = dict.fromkeys(wordDictList[0], 0)
    N = len(wordDictList)
    import math

    for wordDict in wordDictList:
        #遍历字典中的每个词汇,统计Ni
        for word, count in wordDict.items():
            if count > 0:
                #先把Ni增加1,存入到idfDict
                idfDict[word] += 1
         #带公式,计算
    for word, NI in idfDict.items():
        idfDict[word] = math.log10((N+1) / (NI+1))
    return idfDict

idfs = computeIDF([wordDictA,wordDictB])
print(idfs)

#计算TF-IDF
def TFIDF(tf, idfs):
    tfidf = {}
    for word, tfval in tf.items():
        tfidf[word] = tfval * idfs[word]
    return tfidf

tfidfA = TFIDF(tfA, idfs)
tfidfB = TFIDF(tfB, idfs)
print(pd.DataFrame([tfidfA,tfidfB]))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

其中前三行输出的是每个词出现的次数,第四五行是第一句话和第二句话词频TF,第六行是,每个词的逆文档频率IDF,最后三行是两句话中每个词的词频-逆文档频率(TF-IDF),TF-IDF越大说明该词的重要性越大。

我们看一下运行结果的最后三行,可以看到两句话中按权重比例,最重要的分别是cat , bed 和 dog ,knees,其它的词相对来说不重要。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/362414
推荐阅读
相关标签
  

闽ICP备14008679号