from: https://blog.csdn.net/mmc2015/article/details/46866537

http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html#sklearn.feature_extraction.text.CountVectorizer

class sklearn.feature_extraction.text.CountVectorizer(

                                 input=u'content'

                                 encoding=u'utf-8'

                                 decode_error=u'strict',

                                 strip_accents=None

                                 lowercase=True

                                 preprocessor=None

                                 tokenizer=None

                                 stop_words=None,

                                 token_pattern=u'(?u)\b\w\w+\b'

                                 ngram_range=(11)

                                 analyzer=u'word'

                                 max_df=1.0

                                 min_df=1,

                                 max_features=None

                                 vocabulary=None

                                 binary=False

                                 dtype=<type 'numpy.int64'>)

[source]

作用:Convert a collection of text documents to a matrix of token counts(计算词汇的数量,即tf);结果由 scipy.sparse.coo_matrix进行稀疏表示。

看下参数就知道CountVectorizer在提取tf时都做了什么:

strip_accents : {‘ascii’, ‘unicode’, None}:是否除去“音调”,不知道什么是“音调”?看:http://textmechanic.com/?reqp=1&reqr=nzcdYz9hqaSbYaOvrt==

lowercase : boolean, True by default:计算tf前,先将所有字符转化为小写。这个参数一般为True。

preprocessor : callable or None (default):复写the preprocessing (string transformation) stage,但保留tokenizing and n-grams generation steps.这个参数可以自己写。

tokenizer : callable or None (default):复写the string tokenization step,但保留preprocessing and n-grams generation steps.这个参数可以自己写。

stop_words : string {‘english’}, list, or None (default):如果是‘english’, a built-in stop word list for English is used。如果是a list,那么最终的tokens中将去掉list中的所有的stop word。如果是None, 不处理停顿词;但参数 max_df可以设置为 [0.7, 1.0) 之间,进而根据intra corpus document frequency(df) of terms自动detect and filter stop words。这个参数要根据自己的需求调整。

token_pattern : string:正则表达式,默认筛选长度大于等于2的字母和数字混合字符(select tokens of 2 or more alphanumeric characters ),参数analyzer设置为word时才有效。

ngram_range : tuple (min_n, max_n):n-values值得上下界,默认是ngram_range=(11),该范围之内的n元feature都会被提取出来!这个参数要根据自己的需求调整。

analyzer : string, {‘word’, ‘char’, ‘char_wb’} or callable:特征基于wordn-grams还是character n-grams。如果是callable是自己复写的从the raw, unprocessed input提取特征的函数。

max_df : float in range [0.0, 1.0] or int, default=1.0:

min_df : float in range [0.0, 1.0] or int, default=1:按比例,或绝对数量删除df超过max_df或者df小于min_df的word tokens。有效的前提是参数vocabulary设置成Node。

max_features : int or None, default=None:选择tf最大的max_features个特征。有效的前提是参数vocabulary设置成Node。

vocabulary : Mapping or iterable, optional:自定义的特征word tokens,如果不是None,则只计算vocabulary中的词的tf。还是设为None靠谱。

binary : boolean, default=False:如果是True,tf的值只有0和1,表示出现和不出现,useful for discrete probabilistic models that model binary events rather than integer counts.。

dtype : type, optional:Type of the matrix returned by fit_transform() or transform().。

结论:

CountVectorizer提取tf都做了这些:去音调、转小写、去停顿词、在word(而不是character,也可自己选择参数)基础上提取所有ngram_range范围内的特征,同时删去满足max_dfmin_df,max_features的特征的tf。当然,也可以选择tf为binary。

这样应该就放心CountVectorizer处理结果是不是自己想要的了。。。。哇哈哈。

最后看下两个函数:

fit(raw_documents[, y]) Learn a vocabulary dictionary of all tokens in the raw documents.
fit_transform(raw_documents[, y]) Learn the vocabulary dictionary and return term-document matrix.
fit(raw_documentsy=None)[source]

Learn a vocabulary dictionary of all tokens in the raw documents.

Parameters:

raw_documents : iterable

An iterable which yields either str, unicode or file objects.
Returns:

self :

fit_transform(raw_documentsy=None)[source]

Learn the vocabulary dictionary and return term-document matrix.

This is equivalent to fit followed by transform, but more efficiently implemented.

Parameters:

raw_documents : iterable

An iterable which yields either str, unicode or file objects.
Returns:

X : array, [n_samples, n_features]

Document-term matrix.

scikit-learn:CountVectorizer提取tf都做了什么的更多相关文章

  1. scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)

    scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups #-*- coding: UTF-8 -*- import ...

  2. Scikit Learn: 在python中机器学习

    转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...

  3. gcc都做了什么优化

    直接上程序: setjmp和longjmp是处理函数嵌套调用的,goto语句不能跨越函数,所以不选择goto. #include <setjmp.h> int setjmp(jmp_buf ...

  4. configure, make, make install都做了什么

    1. 我的理解./configure:  确保接下来的make以及make install所依赖的文件没有问题make:  build编译连接生成可执行程序make install: 将编译好的可执行 ...

  5. (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探

    一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...

  6. (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探

    目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...

  7. 从架构演进的角度聊聊Spring Cloud都做了些什么?

    Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...

  8. Java对象的创建 —— new之后JVM都做了什么?

    Java对象创建过程 1. 类加载检查 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没 ...

  9. 从架构演进的角度聊聊Spring Cloud都做了些什么

    1.从架构演进的角度聊聊Spring Cloud都做了些什么?2.中小型互联网公司微服务实践-经验和教训3.Spring Cloud在国内中小型公司能用起来吗?

随机推荐

  1. Ubuntu Nginx Uwsgi Python布置服务器Django项目

     1 安装python 3.6 ubuntu预装了2.7,不需要卸载预装库,直接安装python3.6(其实现在的阿里云18.04会给你自动装python2.7.15和3.6.8) 你可以分别查看一下 ...

  2. idea配置代码注释模板

    从eclipse换成idea后,有点不习惯,其中之一就是代码注释,感觉不如eclipse好用,下面是一些配置方法,配完之后差不多能实现eclipse的效果. 1.以配置Class的注释为例,其他文件的 ...

  3. 2.01_Python网络爬虫概述

    一:什么是网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取网络信息的程序或者脚本: 二:为什么要做网络爬虫? 大数据时代 ...

  4. docker 搭建zookeeper集群和kafka集群

    docker 搭建zookeeper集群 安装docker-compose容器编排工具 Compose介绍 Docker Compose 是 Docker 官方编排(Orchestration)项目之 ...

  5. rdb和aof二种持久化方式对比(Redis)

    我们已经知道对于一个企业级的redis架构来说,持久化是不可减少的 企业级redis集群架构:海量数据.高并发.高可用 持久化主要是做灾难恢复,数据恢复,也可以归类到高可用的一个环节里面去 比如你re ...

  6. VMware无法连接MKS:套接字连接尝试次数太多解决

    粘贴自:https://jingyan.baidu.com/article/425e69e61eb578be15fc16ae.html VMware在开启虚拟机的时候,突然弹出[无法连接MKS:套接字 ...

  7. 浅谈IT人的发展(转载)

    一个人如果能确定他喜欢的行业,他一生都会非常幸福. 相反,则往往痛苦,也许竟然会因此成为一个哲学家也说不定. 中国的贫穷决定了我们当中的大多数人不能根据自己的爱好来选择职业,而只是因为生活所迫,或者世 ...

  8. js获取页面元素的位置

    一.网页的大小和浏览器窗口的大小 首先,要明确两个基本概念. 一张网页的全部面积,就是它的大小.通常情况下,网页的大小由内容和CSS样式表决定. 浏览器窗口的大小,则是指在浏览器窗口中看到的那部分网页 ...

  9. 箭头函数与定时器的this指向问题

    函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象. 箭头函数本身没有this,this继承上级的this. 定时器中箭头函数的this指向包含定时器的函数,所以定时器中的箭头函数要 ...

  10. FFmpeg常用命令学习笔记(二)录制命令

    录制命令 1.FFmpeg录屏命令 ffmpeg -f avfoundation -i 1 -r 30 out.yuv -f:指定使用avfoundation采集数据 -i:指定从哪采集数据,它是一个 ...