Ref: 文本挖掘预处理之向量化与Hash Trick

Ref: 文本挖掘预处理之TF-IDF

Ref: sklearn.feature_extraction.text.CountVectorizer

Ref: TF-IDF与余弦相似性的应用(一):自动提取关键词

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的更多相关文章

  1. 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

    在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...

  2. 【TensorFlow】tf.nn.max_pool实现池化操作

    max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...

  3. 【转载】 tf.ConfigProto和tf.GPUOptions用法总结

    原文地址: https://blog.csdn.net/C_chuxin/article/details/84990176 -------------------------------------- ...

  4. 【Tensorflow】tf.nn.depthwise_conv2d如何实现深度卷积?

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/mao_xiao_feng/article/ ...

  5. 【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?膨胀卷积

    介绍关于空洞卷积的理论可以查看以下链接,这里我们不详细讲理论: 1.Long J, Shelhamer E, Darrell T, et al. Fully convolutional network ...

  6. 【六】tf和cgi进行联合试验,完成日志服务器

    [任务6]tf和cgi进行联合试验,完成日志服务器 [任务6]tf和cgi进行联合试验,完成日志服务器 改装gen-cpp目录下client.cpp文件 启动Nginx服务和gen-cpp目录下编译后 ...

  7. 【转载】 tf.train.slice_input_producer()和tf.train.batch()

    原文地址: https://www.jianshu.com/p/8ba9cfc738c2 ------------------------------------------------------- ...

  8. 【TensorFlow】tf.nn.embedding_lookup函数的用法

    tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素.tf.nn.embedding_lookup(tensor, id):tensor就是输入张量,id就是张量 ...

  9. 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

随机推荐

  1. 编程写一个方法时,注意方法中传参数的数量最好不要超过5个,超过5个怎么办?可以用struct或class,或一个字典类

    图  1 一.从图1发现了什么问题呢? 答案:1.参数传的的太多了:2.另外注释也没写好. 说明:一个方法中,传参数的数量最好不要超过5个. 应该采用:struct或class,或一个字典类都行.其中 ...

  2. c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息

    我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统名称.物理内存等. 首先需要引入命名空间: us ...

  3. FPGA该如何应对ASIC的大爆发?

    有人认为,除了人才短缺.开发难度较大,相比未来的批量化量产的ASIC芯片,FPGA在成本.性能.功耗方面仍有很多不足.这是否意味着,在ASIC大爆发之际,FPGA将沦为其“过渡”品的命运? 安路科技市 ...

  4. bzoj3326: [Scoi2013]数数

    Description Fish 是一条生活在海里的鱼,有一天他很无聊,就开始数数玩. 他数数玩的具体规则是: 1. 确定数数的进制B 2. 确定一个数数的区间[L, R] 3. 对于[L, R] 间 ...

  5. C++ namespace命名空间

    1.什么是命名空间 简而言之:划片取名 加入有两个公司,公司A里面有一个员工小明,公司B里面有一个员工小明,两个公司在同一栋楼.这时,你去找小明,你再楼下大喊:“小明!你给我下来!”,这时两个小明都会 ...

  6. R语言学习——欧拉计划(11)Largest product in a grid

    Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 0 ...

  7. [转]MSSQL 判断临时表是否存在

    原文来自:http://www.cnblogs.com/szfhquan/p/4229150.html 方法一: 1 if exists (select * from tempdb.dbo.sysob ...

  8. python unittest单元测试框架-2discover

    基于TestLoader提供的discover方法实现用例执行 当用例达达到数十条后,在runtest.py文件中通过addTest()添加/删除测试用例就非常麻烦.此时可以使用discover方法找 ...

  9. shell 11函数

    函数定义 function 方法名(){ command return int; } 注意:function可加可不加 #shell #!/bin/sh function fun1(){ echo & ...

  10. centos 7.5 安装mysql

    1.Mysql: 在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1.下载并安装MySQL官 ...