理解sklearn.feature.text中的CountVectorizer和TfidfVectorizer
"""
理解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的更多相关文章
- sklearn.feature_extraction.text.CountVectorizer 学习
CountVectorizer: CountVectorizer可以将文本文档集合转换为token计数矩阵.(token可以理解成词) 此实现通过使用scipy.sparse.csr_matrix产生 ...
- sklearn.feature_extraction.text 的TfidfVectorizer函数
TfidfVectorizer函数主要用于,将文档(句子)等通过 tf-idf值来进行表示,也就是用一个tf-idf值的矩阵来表示文档(句子也可). from sklearn.feature_extr ...
- 理解与应用css中的display属性
理解与应用css中的display属性 display属性是我们在前端开发中常常使用的一个属性,其中,最常见的有: none block inline inline-block inherit 下面, ...
- 理解和使用 JavaScript 中的回调函数
理解和使用 JavaScript 中的回调函数 标签: 回调函数指针js 2014-11-25 01:20 11506人阅读 评论(4) 收藏 举报 分类: JavaScript(4) 目录( ...
- 在SUBLIME TEXT中安装SUBLIMELINTER进行JS&CSS代码校验
一:Sublime Text 中需要先安装Package Control.(如果有则无需安装) 安装方法:打开Sublime Text控制台(快捷键Ctrl+`),在控制台粘贴以下代码,按回车执行. ...
- 如何在Sublime text中运行PHP文件
如何在Sublime text中运行PHP文件 2014-06-14 17:17 3709人阅读 评论(1) 收藏 举报 phpSublime Text 一.将PHP安装目录放如环境变量PATH 二. ...
- [转]理解与使用Javascript中的回调函数
在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...
- 【JavaScript】理解与使用Javascript中的回调函数
在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...
- Sublime Text 中使用Git插件连接GitHub
sublime Text的另一个强大之处在于它提供了非常丰富的插件,可以帮助程序员来适合大多数语言的开发.这些插件通过它自己的Package Controll(包管理)组件来安装,非常方便.一般常用的 ...
随机推荐
- eclipse如何debug调试jdk源码,并显示局部变量
http://blog.csdn.net/xuefeng0707/article/details/8738869 http://blog.csdn.net/leehsiao/article/detai ...
- Android -- 状态栏高度
干货 Class<?> c = null; Object obj = null; Field field = null; int x = 0, sbar = 0; try { c = Cl ...
- 运行代码时报linker command failed with exit code 1 错误
一个c语言项目,在.h文件中原来只有一些方法的声明,后来我加入了一些变量声明后,编译的时候报错: 运行代码时报linker command failed with exit code 1 错误 怎么回 ...
- Material Design学习之 ProgreesBar
转载奇怪注明出处:王亟亟的大牛之路 继续我们Material Design的内容,这一篇讲的是进度条,上一篇是Switch地址例如以下:http://blog.csdn.net/ddwhan0123/ ...
- NSobject 结构
参考地址: https://iosdevelopmenttutorials.wordpress.com/tag/uiscrollview/ Apple Documentation
- File /hbase could only be replicated to 0 nodes instead of minReplication (=1). There are 30 datanode(s) running and no node(s) are excluded in this operation.
原因: hdfs-site.xml中的配置为: <property> <name>dfs.datanode.du.reserved</name> <value ...
- setTimeout迭代替换setInterval
一.它们之间的区别 setTimeout - 仅执行一次 setInterval - 间隔执行 二.为什么推荐用setTimeout替换掉setIntelval? javascript是异 ...
- Postgresql中的数据类型大全
一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...
- 003-Go初探Iris
1.参考资源:https://iris-go.com/v10/start#Getting-Started 2.首先下载:go get -u github.com/kataras/iris,此处时间较长 ...
- Spring-boot加载resources下的文件
加载方式: FileInputStream keyStoreIn = new FileInputStream(ResourceUtils.getFile("classpath:ca/clie ...