TF-IDF词频-逆文档频率算法 python代码实现:

0.引入依赖

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(" ")
bowA

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

 

 

2.进行次数统计

# 用一个统计字典 保存词出现次数
wordDictA = dict.fromkeys( wordSet, 0 )
wordDictB = dict.fromkeys( wordSet, 0 )


# 遍历文档统计词数
for word in bowA:
    wordDictA[word] += 1
for word in bowB:
    wordDictB[word] += 1   
    
pd.DataFrame([wordDictA, wordDictB])

 

 

3.计算词频TF

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

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

 

 

4.计算逆文档频率IDF

def computeIDF( wordDictList ):
    # 用一个字典对象保存IDF结果。每个词作为key, 初始值为0
    idfDict = dict.fromkeys(wordDictList[0], 0)
    N = len(wordDictList)
    import math
    
    for wordDict in wordDictList:
        # 遍历字典中的每一个词汇
        for word, count in wordDict.items():
            if count > 0:
                # 先把NI增加1, 存入到idfDict
                idfDict[word] += 1
                
    # 已经得到所有词汇i对应的Ni,现在根据公式把它替换成idf值
    for word, ni in idfDict.items():
        idfDict[word] = math.log10 ( (N+1)/(ni+1) )

    return idfDict

idfs = computeIDF([wordDictA, wordDictB])
idfs

 

 

5. 计算TF-IDF

def computeTFIDF( tf, idfs ):
    tfidf = {}
    for word, tfval in tf.items():
        tfidf[word] = tfval * idfs[word]
    return tfidf
    
tfidfA = computeTFIDF( tfA, idfs )
tfidfB = computeTFIDF( tfB, idfs )

pd.DataFrame( [tfidfA, tfidfB] )

 

原文地址:http://www.cnblogs.com/slowlydance2me/p/16872816.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性