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. 后台取IE的相关信息

    HttpBrowserCapabilities b = Request.Browser; Response.Write("浏览器名称和版本号:" + b.Type + " ...

  2. R(8): tidyr

    tidy(整洁),Tidyr包是由Hadely Wickham创建,这个包提高了整理原始数据的效率,tidyr包的4个常用的函数及其用途如下: gather()——它把多列放在一起,然后转化为key: ...

  3. g++编译后中文显示乱码解决方案

    环境:Windows 10 专业版 GCC版本:5.3.0 测试代码: #include <iostream> using namespace std; int main(int argc ...

  4. Android之WebViewClient与WebChromeClient的区别

    Android之WebViewClient与WebChromeClient的区别 2012-05-05      0个评论       收藏    我要投稿 ANDROID应用开发的时候可能会用到WE ...

  5. Excel数组排序+图片统一大小

    Sub 图片调整合适大小() ' Debug.Print ActiveWorkbook.Name 图片显示比例 = 0.9 '1为顶满单元格 Dim wb As Workbook, sh As Wor ...

  6. 【Tomcat-原】如何在Myeclipse中添加本地的Tomcat

    2014-10-27 16-24-09  liulin 说明: Myeclipse中自带Tomcat,如果不想用Myeclipse自带的Tomcat,可以使用本地的Tomcat, 下面将介绍如何在My ...

  7. 学习笔记之FluentAssertions

    dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...

  8. 搭建GlusterFS文件系统

    (1)环境准备 创建两个虚拟机配置如下 把仅主机第二张网卡配置如下: GlusterFS1 GlusterFS2 上传文件到opt目录下 文件内容如下 (2)GlusterFS安装配置 1.安装Glu ...

  9. VS Code 基本介绍 和 快捷键

    简介 VSCode是微软推出的一款轻量编辑器,采取了和VS相同的UI界面,搭配合适的插件可以大幅提升前端开发的效率. 布局:左侧是用于展示所要编辑的所有文件和文件夹的文件管理器,依次是:资源管理器,搜 ...

  10. php 七种数据类型介绍

    PHP有7个数据类型.七个类型: 字符串, 整数, 浮动, 布尔, 数组, 对象, 资源. 字符串 字符串保持字符,如“一”.“abc”,“www.manongjc.com”等.PHP字符串是区分大小 ...