【346】TF-IDF
Ref: 文本挖掘预处理之TF-IDF
Ref: sklearn.feature_extraction.text.CountVectorizer
Ref: TF-IDF与余弦相似性的应用(一):自动提取关键词
Ref: TF-IDF与余弦相似性的应用(二):找出相似文章
>>> from sklearn.feature_extraction.text import TfidfTransformer
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> corpus=["I come to China to travel",
"This is a car polupar in China",
"I love tea and Apple ",
"The work is to write some papers in science"]
>>> vectorizer=CountVectorizer()
>>> transformer = TfidfTransformer()
>>> tfidf = transformer.fit_transform(vectorizer.fit_transform(corpus))
>>> print(tfidf)
(0, 16) 0.4424621378947393
(0, 15) 0.697684463383976
(0, 4) 0.4424621378947393
(0, 3) 0.348842231691988
(1, 14) 0.45338639737285463
(1, 9) 0.45338639737285463
(1, 6) 0.3574550433419527
(1, 5) 0.3574550433419527
(1, 3) 0.3574550433419527
(1, 2) 0.45338639737285463
(2, 12) 0.5
(2, 7) 0.5
(2, 1) 0.5
(2, 0) 0.5
(3, 18) 0.3565798233381452
(3, 17) 0.3565798233381452
(3, 15) 0.2811316284405006
(3, 13) 0.3565798233381452
(3, 11) 0.3565798233381452
(3, 10) 0.3565798233381452
(3, 8) 0.3565798233381452
(3, 6) 0.2811316284405006
(3, 5) 0.2811316284405006
>>> print(vectorizer.get_feature_names())
['and', 'apple', 'car', 'china', 'come', 'in', 'is', 'love', 'papers', 'polupar', 'science', 'some', 'tea', 'the', 'this', 'to', 'travel', 'work', 'write']
说明:其中 (0, 16) 表示第一行文本,索引为 16 的词,对应的是“travel”,以此类推。
继续上面的信息,获取对应 term 的 tfidf 值,tfidf 变量对应的是 (4, 19) 矩阵的值,对应不同的句子,不同的 term。
>>> tfidf_array = tfidf.toarray() #获取array,然后遍历array,并分别转为list
>>> names_list = vectorizer.get_feature_names() #获取names的list
>>> for i in range(0, len(corpus)):
print(corpus[i],'\n')
tmp_list = tfidf_array[i].tolist()
for j in range(0, len(names_list)):
if tmp_list[j] != 0:
if len(names_list[j])>=7:
print(names_list[j],'\t',tmp_list[j])
else:
print(names_list[j],'\t\t',tmp_list[j])
print('') I come to China to travel china 0.348842231691988
come 0.4424621378947393
to 0.697684463383976
travel 0.4424621378947393 This is a car polupar in China car 0.45338639737285463
china 0.3574550433419527
in 0.3574550433419527
is 0.3574550433419527
polupar 0.45338639737285463
this 0.45338639737285463 I love tea and Apple and 0.5
apple 0.5
love 0.5
tea 0.5 The work is to write some papers in science in 0.2811316284405006
is 0.2811316284405006
papers 0.3565798233381452
science 0.3565798233381452
some 0.3565798233381452
the 0.3565798233381452
to 0.2811316284405006
work 0.3565798233381452
write 0.3565798233381452 >>>
获取 TF(Term Frequency)
>>> X = vectorizer.fit_transform(corpus)
>>> X.toarray()
array([[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0],
[0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1]],
dtype=int64)
>>> vector_array = X.toarray()
>>> for i in range(0, len(corpus)):
print(corpus[i],'\n')
tmp_list = vector_array[i].tolist()
for j in range(0, len(names_list)):
if tmp_list[j] != 0:
if len(names_list[j])>=7:
print(names_list[j],'\t',tmp_list[j])
else:
print(names_list[j],'\t\t',tmp_list[j])
print('') I come to China to travel china 1
come 1
to 2
travel 1 This is a car polupar in China car 1
china 1
in 1
is 1
polupar 1
this 1 I love tea and Apple and 1
apple 1
love 1
tea 1 The work is to write some papers in science in 1
is 1
papers 1
science 1
some 1
the 1
to 1
work 1
write 1 >>>
【346】TF-IDF的更多相关文章
- 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...
- 【TensorFlow】tf.nn.max_pool实现池化操作
max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...
- 【转载】 tf.ConfigProto和tf.GPUOptions用法总结
原文地址: https://blog.csdn.net/C_chuxin/article/details/84990176 -------------------------------------- ...
- 【Tensorflow】tf.nn.depthwise_conv2d如何实现深度卷积?
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/mao_xiao_feng/article/ ...
- 【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?膨胀卷积
介绍关于空洞卷积的理论可以查看以下链接,这里我们不详细讲理论: 1.Long J, Shelhamer E, Darrell T, et al. Fully convolutional network ...
- 【六】tf和cgi进行联合试验,完成日志服务器
[任务6]tf和cgi进行联合试验,完成日志服务器 [任务6]tf和cgi进行联合试验,完成日志服务器 改装gen-cpp目录下client.cpp文件 启动Nginx服务和gen-cpp目录下编译后 ...
- 【转载】 tf.train.slice_input_producer()和tf.train.batch()
原文地址: https://www.jianshu.com/p/8ba9cfc738c2 ------------------------------------------------------- ...
- 【TensorFlow】tf.nn.embedding_lookup函数的用法
tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素.tf.nn.embedding_lookup(tensor, id):tensor就是输入张量,id就是张量 ...
- 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...
随机推荐
- webpack 打包性能分析工具
webpack-bundle-analyzer,推荐使用 新版 vue-cli (旧版按照新版的进行配置即可)已经集成该插件,在项目的 package.json 文件中注入如下命令,然后运行(npm ...
- HTML5 localStorage使用教程
在客户端存储数据,HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...
- php 实现欧拉函数Euler
欧拉函数ph(n)的意思是所有小于n且与n互质的个数.比如说ph(10) = 4{1,3,7,9与10互质} 代码如下: function Euler($x) { $res = $x; $now = ...
- loadrunner 函数解释
一.lr_save_string 使用介绍1.该函数主要是将程序中的常量或变量保存为lr中的参数.格式: //将常量保存为参数lr_save_string("777"," ...
- 【Spring学习笔记-MVC-1.0】Spring MVC架构介绍
作者:ssslinppp 1. 核心架构图 2. 核心架构的具体流程步骤 3. 具体的核心开发步骤 4. 常用注解 5. <mvc:annotation-driven>配置 6 ...
- 【ActiveMQ入门-8】ActiveMQ学习-与Spring集成
概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:topic 环境: 主要包括4个文件: HelloSender.java: JMSTest.java: Pro ...
- form 表单提交浏览器的enctype(编码方式)
1. method 为 get 时 enctype :x-www-form-urlencoded(默认), 把form数据append到对应的url后面: 2. method 为 post 时 Bro ...
- 操作系统-百科:Kylin (中国自主知识产权操作系统)
ylbtech-操作系统-百科:Kylin (中国自主知识产权操作系统) Kylin操作系统是国家高技术研究发展计划(863计划)的重大成果之一,是以国防科技大学为主导,与中软.联想等单位联合设计和开 ...
- 学习笔记之C# / .NET Core 2.0
C# 教程 | 菜鸟教程 http://www.runoob.com/csharp/csharp-tutorial.html .NET API Browser | Microsoft Docs htt ...
- 利用.pbk来实现ADSL开机自动拨号
当你新建拨号连接或者VPN连接之后在你的电脑里会创建一个.pbk的文件 这个.pbk的文件可以说是一个集合,将你电脑的所有连接都保存在一起. 同时你还可以将此连接复制起来传给其他人. 系统默认的.pb ...