理解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(包管理)组件来安装,非常方便.一般常用的 ...
随机推荐
- Set Matrix Zeroes leetcode java
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- Hadoop视频教程汇总
一 慕课网 1.Hadoop大数据平台架构与实践--基础篇(已学习) 链接:https://www.imooc.com/learn/391 2.Hadoop进阶(已学习) 链接:https://www ...
- 转:无监督特征学习——Unsupervised feature learning and deep learning
http://blog.csdn.net/abcjennifer/article/details/7804962 无监督学习近年来很热,先后应用于computer vision, audio clas ...
- C#获取程序启动目录
//WCF service: string servicePath = System.Web.Hosting.HostingEnvironment.MapPath("~"); // ...
- HighCharts设置图表背景透明
其实就一句话: backgroundColor: 'rgba(0,0,0,0)' 完整示例: $(function () { $('#container').highcharts({ chart: { ...
- (纪录片)鸟瞰中国 China from Above
简介: 类型: 纪录片官方网站: natgeotv.com/uk/china-from-above制片国家/地区: 美国语言: 英语集数: 2单集片长: 44分钟IMDb链接: tt4872012 主 ...
- 关于ThinkPHP的一些编程技巧
在TP学习过程中难免会遇到一些大大小小的问题,把这些问题积累下来就可以在以后遇到时能很快速的解决,提高编程效率. 1.让Runtime下的文件格式化:入口文件处:define(‘STRIP_RUNTI ...
- 1066: 单词游戏(game)
var i,j,k,n,ans,p,len,chk,zh:longint; ch:char; s:string; a:array[..] of longint; begin readln(ch); c ...
- 变址values(, %edi, 4)和间址4(%edi)
<汇编语言程序设计>Richard Blum著:5.2.4 在内存和寄存器之间传送数据 使用变址的内存位置: 可以在一个命令中指定把多个值存放到内存中: values: .in ...
- Linux常用命令之wget
wget:从网络上下载文件到当前目录.