自然语言处理_tf-idf

python 复制代码
import pandas as pd
import math

1.数据预处理

python 复制代码
docA = "The cat sat on my face"
docB = "The dog sat on my bed"

wordsA = docA.split(" ")
wordsB = docB.split(" ")

wordsSet = set(wordsA).union(set(wordsB))
print(wordsSet)
复制代码
{'on', 'my', 'face', 'sat', 'dog', 'The', 'cat', 'bed'}

2.计算词的频数

python 复制代码
wordCountA = dict.fromkeys(wordsSet, 0)
wordCountB = dict.fromkeys(wordsSet, 0)

for word in wordsA:
    wordCountA[word] += 1
for word in wordsB:
    wordCountB[word] += 1

pd.DataFrame([wordCountA, wordCountB])    

| | on | my | face | sat | dog | The | cat | bed |
| 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |

1 1 1 0 1 1 1 0 1

3.计算词的频率

python 复制代码
def computeTF(wordCount, docWords):
    tfDict = {}
    docCount = len(docWords)
    for word, count in wordCount.items():
        tfDict[word] = count / float(docCount)
    return tfDict

tfA = computeTF(wordCountA, wordsA)
tfB = computeTF(wordCountB, wordsB)
print("tfA ", tfA)
复制代码
tfA  {'on': 0.16666666666666666, 'my': 0.16666666666666666, 'face': 0.16666666666666666, 'sat': 0.16666666666666666, 'dog': 0.0, 'The': 0.16666666666666666, 'cat': 0.16666666666666666, 'bed': 0.0}

4.计算逆文档频率

python 复制代码
def computeIDF(docList):
    idfDict = {}
    doc_len = len(docList)
    
    idfDict = dict.fromkeys(docList[0].keys(), 0)
    
    for doc in docList:
        for word, count in doc.items():
            if count > 0:
                idfDict[word] += 1
      
    for word, count in idfDict.items():
        idfDict[word] = math.log10((doc_len + 1) / float(count + 1))
    return idfDict

idf = computeIDF([wordCountA, wordCountB])
print(idf)
复制代码
{'on': 0.0, 'my': 0.0, 'face': 0.17609125905568124, 'sat': 0.0, 'dog': 0.17609125905568124, 'The': 0.0, 'cat': 0.17609125905568124, 'bed': 0.17609125905568124}

5.计算 TF-IDF

python 复制代码
def computeTFIDF(tf, idf):
    tfidf = {}
    for word, tf in tf.items():
        tfidf[word] = tf * idf[word]
    return tfidf

tfidfA = computeTFIDF(tfA, idf)
tfidfB = computeTFIDF(tfB, idf)
pd.DataFrame([tfidfA, tfidfB])

| | on | my | face | sat | dog | The | cat | bed |
| 0 | 0.0 | 0.0 | 0.029349 | 0.0 | 0.000000 | 0.0 | 0.029349 | 0.000000 |

1 0.0 0.0 0.000000 0.0 0.029349 0.0 0.000000 0.029349
相关推荐
NGBQ121386 天前
Imgflip社交媒体表情包数据集-202208条多模板meme数据-包含完整图片URL和文本说明-适用于NLP模型训练和社交媒体分析
人工智能·自然语言处理·媒体
homelook6 天前
Transformer架构,这是现代自然语言处理和人工智能领域的核心技术。
人工智能·自然语言处理·transformer
赋创小助手6 天前
服务器主板为何不再采用ATX?以超微X14DBM-AP 为例解析
运维·服务器·人工智能·深度学习·自然语言处理·硬件架构
摘星编程6 天前
大语言模型(Large Language Models,LLM)如何颠覆未来:深入解析应用、挑战与趋势
人工智能·语言模型·自然语言处理
小陈phd7 天前
多模态大模型学习笔记(六)——多模态全景认知
人工智能·机器学习·自然语言处理
taoqick7 天前
修改GRPO Advantages的一些思路(pass@k)
人工智能·机器学习·自然语言处理
云器科技7 天前
云器Lakehouse新版本特性解读:MCP Server —— AI 数据工程师的深度解析与实战指南
大数据·人工智能·自然语言处理·数据平台·湖仓平台
陈天伟教授7 天前
人工智能应用- 预测化学反应:06. BERT 模型简介
人工智能·深度学习·机器学习·自然语言处理·bert·推荐算法
Loo国昌7 天前
【AI应用开发实战】Guardrail风险控制中间件:Agent系统的安全防线
人工智能·python·安全·自然语言处理·中间件·prompt
小龙报8 天前
【Coze-AI智能体平台】Coze 工作流 = 智能体的 “流程管家”?一文解锁自动化落地新玩法
人工智能·语言模型·自然语言处理·性能优化·数据分析·知识图谱·需求分析