"""
理解sklearn中的CountVectorizer和TfidfVectorizer
"""
from collections import Counter import numpy as np
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer sentences = ["there is a dog dog", "here is a cat"]
count_vec = CountVectorizer()
a = count_vec.fit_transform(sentences)
print(a.toarray())
print(count_vec.vocabulary_)
"""
输出
{'dog': 1, 'there': 4, 'here': 2, 'cat': 0, 'is': 3}
表示每个词汇对应的坐标
""" print("=" * 10)
tf_vec = TfidfVectorizer()
b = tf_vec.fit_transform(sentences)
print(b.toarray())
print(tf_vec.vocabulary_)
print(tf_vec.idf_) # 逆文档频率
print(tf_vec.get_feature_names()) def mytf_idf(s):
# 自己实现tfidf
words = tf_vec.get_feature_names()
tf_matrix = np.zeros((len(s), len(words)), dtype=np.float32)
smooth = 1
# 初始值加上平滑因子
df_matrix = np.ones(len(words), dtype=np.float32) * smooth
for i in range(len(s)):
s_words = s[i].split()
for j in range(len(words)):
cnt = Counter(s_words).get(words[j], 0)
tf_matrix[i][j] = cnt
if cnt > 0:
df_matrix[j] += 1
# idf一定是大于1的数值
idf_matrix = np.log((len(s) + smooth) / df_matrix) + 1
matrix = tf_matrix * idf_matrix
matrix = matrix / np.linalg.norm(matrix, 2, axis=1).reshape(matrix.shape[0], 1)
print(matrix) print("=" * 10)
mytf_idf(sentences)
"""
TODO:
* IDF可以学到,通过神经网络反向传播来学习IDF而不是直接计算得出
* CountVectorizer有时不需要考虑个数,只需要知道是否出现过即可
"""

理解sklearn.feature.text中的CountVectorizer和TfidfVectorizer的更多相关文章

  1. sklearn.feature_extraction.text.CountVectorizer 学习

    CountVectorizer: CountVectorizer可以将文本文档集合转换为token计数矩阵.(token可以理解成词) 此实现通过使用scipy.sparse.csr_matrix产生 ...

  2. sklearn.feature_extraction.text 的TfidfVectorizer函数

    TfidfVectorizer函数主要用于,将文档(句子)等通过 tf-idf值来进行表示,也就是用一个tf-idf值的矩阵来表示文档(句子也可). from sklearn.feature_extr ...

  3. 理解与应用css中的display属性

    理解与应用css中的display属性 display属性是我们在前端开发中常常使用的一个属性,其中,最常见的有: none block inline inline-block inherit 下面, ...

  4. 理解和使用 JavaScript 中的回调函数

    理解和使用 JavaScript 中的回调函数 标签: 回调函数指针js 2014-11-25 01:20 11506人阅读 评论(4) 收藏 举报  分类: JavaScript(4)    目录( ...

  5. 在SUBLIME TEXT中安装SUBLIMELINTER进行JS&CSS代码校验

    一:Sublime Text 中需要先安装Package Control.(如果有则无需安装) 安装方法:打开Sublime Text控制台(快捷键Ctrl+`),在控制台粘贴以下代码,按回车执行. ...

  6. 如何在Sublime text中运行PHP文件

    如何在Sublime text中运行PHP文件 2014-06-14 17:17 3709人阅读 评论(1) 收藏 举报 phpSublime Text 一.将PHP安装目录放如环境变量PATH 二. ...

  7. [转]理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  8. 【JavaScript】理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  9. Sublime Text 中使用Git插件连接GitHub

    sublime Text的另一个强大之处在于它提供了非常丰富的插件,可以帮助程序员来适合大多数语言的开发.这些插件通过它自己的Package Controll(包管理)组件来安装,非常方便.一般常用的 ...

随机推荐

  1. C#访问远程主机资源的方法

    实现访问远程主机的共享目录中的一个文件的解决方法: 一.调用Net use命令 // 使用方法:        //if (Connect("192.168.1.48", &quo ...

  2. 左手坐标系和右手坐标系 ZZ

    今天记录一下一些基本的数学知识,左手坐标系和右手坐标系.这些对于搞图像开发或者游戏开发的朋友来说,应该是很基础的东西,不过对于大部分人来说还是比较陌生的知识.之所以看这方面资料主要是因为在使用Andr ...

  3. 【Spark】SparkStreaming-Tasks-数量如何设置?

    SparkStreaming-Tasks-数量如何设置? sparkstreaming task 数量设置_百度搜索 spark内核揭秘-14-Spark性能优化的10大问题及其解决方案 - star ...

  4. 【Spark】SparkStreaming-foreachrdd foreachpartition

    SparkStreaming-foreachrdd foreachpartition foreachrdd foreachpartition_百度搜索 SparkStreaming之foreachRD ...

  5. fastjson生成json时Null属性不显示 (转)

    http://blog.csdn.net/u010648555/article/details/51422340 null对应的key已经被过滤掉:这明显不是我们想要的结果,这时我们就需要用到fast ...

  6. How to Sign in as a Different User in SharePoint 2013

    SharePoint used to have a menu option called "Sign in as Different User" in the top-right ...

  7. mac切换root

    方法一: sudo -i sudo su或是su. 转自:http://blog.csdn.net/duanyipeng/article/details/8621967

  8. Node.js 笔记(一) nodejs、npm、express安装

    Windows平台下的node.js安装 直接去nodejs的官网http://nodejs.org/上下载nodejs安装程序,双击安装就可以了 测试安装是否成功: 在命令行输入 node –v 应 ...

  9. jQuery.cookie应用操作

    //1.插件框架: /* * name @键 * value @值 * options @选项,包括有效期 路径 域名等 */ jQuery.cookie = function(name, value ...

  10. spring-boot 实现文件上传下载

    @Controller public class FileUploadCtrl { @Value("${file.upload.dir}") private String path ...