"""
理解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. HTTP 响应实体主体:XML 及 XML parser

    本文内容 HTTP 响应实体主体:XML XML parser 总结 各编程语言实现的 XML parser   HTTP 响应实体主体:XML 实体主体(entity-body)通常是HTTP响应里 ...

  2. 如果使用xutils出现了ExceptionInInitializerError这个错误

    看看是否初始化 1)在Application初始化   x.Ext.init(this); // 在application的onCreate中初始化 /** * 初始化xUtils3 */ publi ...

  3. Class.isAssignableFrom(Class clz)方法 与 instanceof 关键字的区别

    Class.isAssignableFrom()是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的子类或接口.   格式为:        Class1.isAssignable ...

  4. Redis分布式锁实现秒杀业务(乐观锁、悲观锁)

    https://blog.csdn.net/lmb55/article/details/78266905

  5. Zoning and LUN Masking

    In a SAN ( Storage Area Network ), if all the hosts are allowed to access all the drives in the SAN, ...

  6. Mina.Net实现的断线重连

    using Mina.Filter.Codec; using Mina.Filter.Codec.TextLine; using System; using System.Collections.Ge ...

  7. 保密员(baomi)

    #include<iostream> #include<string> #include<stdio.h> #include<algorithm> #i ...

  8. Hessian 原理分析

    Hessian 原理分析 一.远程通讯协议的基本原理 网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络 IO 来实现,其中传输协议比较出名的有 http . tcp . u ...

  9. Aerospike系列:6:AerospikeTools & Utilities

    1:Aerospike Query Language类似于SQL命令.可以用来管理索引和用户自定义函数,也可以测试大多数数据库的功能. [root@localhost ~]#aql OPTIONS - ...

  10. 【MongoDB:】稍微复杂的操作

    1:插入数据稍微复杂的形式 doc=( {"user_id" : "ABCDBWN", "password" :"ABCDBWN& ...