- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
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 )的文档数量。通过取对数,可以避免数值过大的问题,同时保证了IDF的单调递减特性,下面看一下代码的现实:
。
def compute_idf(doc_list): """ :param doc_list: 文档的集合 :return: """ sum_list = list(set([word_i for doc_i in doc_list for word_i in doc_i])) idf_dict = {word_i: 0 for word_i in sum_list} for word_j in sum_list: for doc_j in doc_list: if word_j in doc_j: idf_dict[word_j] += 1 return {k: math.log(len(doc_list) / (v + 1)) for k, v in idf_dict.items()} # 示例文档 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) # 计算整个文档集合的IDF idf = compute_idf([doc1_words, doc2_words, doc3_words]) # idf:{'different': 0.4054651081081644, 'another': 0.4054651081081644, 'a': 0.0, 'example': 0.0, 'this': -0.2876820724517809, 'sample': 0.4054651081081644, 'is': -0.2876820724517809}
通过结果可以发现,different、another和sample都比is、a等词汇的IDF值要高,代表越重要.
好的,最后看一下TF-IDF的公式吧, 。
$$TF-IDF=TF*IDF $$ 。
TF-IDF 就是TF*IDF,来综合的评价一个词在文档中的重要性.
最后看一下完整的代码, 。
import math from collections import Counter import math def compute_tfidf(tf_dict, idf_dict): tfidf = {} for word, tf_value in tf_dict.items(): tfidf[word] = tf_value * idf_dict[word] return tfidf 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 def compute_idf(doc_list): """ :param doc_list: 文档的集合 :return: """ sum_list = list(set([word_i for doc_i in doc_list for word_i in doc_i])) idf_dict = {word_i: 0 for word_i in sum_list} for word_j in sum_list: for doc_j in doc_list: if word_j in doc_j: idf_dict[word_j] += 1 return {k: math.log(len(doc_list) / (v + 1)) for k, v in idf_dict.items()} # 示例文档 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}') # 计算整个文档集合的IDF idf = compute_idf([doc1_words, doc2_words, doc3_words]) print(f'idf:{idf}') # 计算每个文档的TF-IDF tfidf1 = compute_tfidf(tf1, idf) tfidf2 = compute_tfidf(tf2, idf) tfidf3 = compute_tfidf(tf3, idf) print("TF-IDF for Document 1:", tfidf1) print("TF-IDF for Document 2:", tfidf2) print("TF-IDF for Document 3:", tfidf3) """ 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} idf:{'example': 0.0, 'different': 0.4054651081081644, 'this': -0.2876820724517809, 'another': 0.4054651081081644, 'is': -0.2876820724517809, 'a': 0.0, 'sample': 0.4054651081081644} TF-IDF for Document 1: {'this': -0.07192051811294523, 'is': -0.07192051811294523, 'a': 0.0, 'sample': 0.1013662770270411} TF-IDF for Document 2: {'this': -0.047947012075296815, 'is': -0.047947012075296815, 'another': 0.06757751801802739, 'example': 0.0} TF-IDF for Document 3: {'this': -0.047947012075296815, 'is': -0.047947012075296815, 'a': 0.0, 'different': 0.06757751801802739, 'example': 0.0} """
。
。
。
最后此篇关于TF-IDF算法原理以及源码实现的文章就讲到这里了,如果你想了解更多关于TF-IDF算法原理以及源码实现的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
本文全面深入地探讨了Docker容器通信技术,从基础概念、网络模型、核心组件到实战应用。详细介绍了不同网络模式及其实现,提供了容器通信的技术细节和实用案例,旨在为专业从业者提供深入的技术洞见和实
📒博客首页:崇尚学技术的科班人 🍣今天给大家带来的文章是《Dubbo快速上手 -- 带你了解Dubbo使用、原理》🍣 🍣希望各位小伙伴们能够耐心的读完这篇文章🍣 🙏博主也在学习阶段,如若发
一、写在前面 我们经常使用npm install ,但是你是否思考过它内部的原理是什么? 1、执行npm install 它背后帮助我们完成了什么操作? 2、我们会发现还有一个成为package-lo
Base64 Base64 是什么?是将字节流转换成可打印字符、将可打印字符转换为字节流的一种算法。Base64 使用 64 个可打印字符来表示转换后的数据。 准确的来说,Base64 不算
目录 协程定义 生成器和yield语义 Future类 IOLoop类 coroutine函数装饰器 总结 tornado中的
切片,这是一个在go语言中引入的新的理念。它有一些特征如下: 对数组抽象 数组长度不固定 可追加元素 切片容量可增大 容量大小成片增加 我们先把上面的理念整理在这
文章来源:https://sourl.cn/HpZHvy 引 言 本文主要论述的是“RPC 实现原理”,那么首先明确一个问题什么是 RPC 呢?RPC 是 Remote Procedure Call
源码地址(包含所有与springmvc相关的,静态文件路径设置,request请求入参接受,返回值处理converter设置等等): spring-framework/WebMvcConfigurat
请通过简单的java类向我展示一个依赖注入(inject)原理的小例子虽然我已经了解了spring,但是如果我需要用简单的java类术语来解释它,那么你能通过一个简单的例子向我展示一下吗?提前致谢。
1、背景 我们平常使用手机和电脑上网,需要访问公网上的网络资源,如逛淘宝和刷视频,那么手机和电脑是怎么知道去哪里去拿到这个网络资源来下载到本地的呢? 就比如我去食堂拿吃的,我需要
大家好,我是飞哥! 现在 iptables 这个工具的应用似乎是越来越广了。不仅仅是在传统的防火墙、NAT 等功能出现,在今天流行的的 Docker、Kubernets、Istio 项目中也经
本篇涉及到的所有接口在公开文档中均无,需要下载 GitHub 上的源码,自己创建私有类的文档。 npm run generateDocumentation -- --private yarn gene
我最近在很多代码中注意到人们将硬编码的配置(如端口号等)值放在类/方法的深处,使其难以找到,也无法配置。 这是否违反了 SOLID 原则?如果不是,我是否可以向我的团队成员引用另一个“原则”来说明为什
我是 C#、WPF 和 MVVM 模式的新手。很抱歉这篇很长的帖子,我试图设定我所有的理解点(或不理解点)。 在研究了很多关于 WPF 提供的命令机制和 MVVM 模式的文本之后,我在弄清楚如何使用这
可比较的 jQuery 函数 $.post("/example/handler", {foo: 1, bar: 2}); 将创建一个带有 post 参数 foo=1&bar=2 的请求。鉴于 $htt
如果Django不使用“延迟查询执行”原则,主要问题是什么? q = Entry.objects.filter(headline__startswith="What") q = q.filter(
我今天发现.NET框架在做计算时遵循BODMAS操作顺序。即计算按以下顺序进行: 括号 订单 部门 乘法 添加 减法 但是我四处搜索并找不到任何文档确认 .NET 绝对 遵循此原则,是否有此类文档?如
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
API 回顾 在创建 Viewer 时可以直接指定 影像供给器(ImageryProvider),官方提供了一个非常简单的例子,即离屏例子(搜 offline): new Cesium.Viewer(
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我是一名优秀的程序员,十分优秀!