当前位置:   article > 正文

scikit-learn文本特征提取:CountVectorizer与TfidfVectorizer_scikit-learn中一般使用countvectorizer和tfidfvectorizer这两

scikit-learn中一般使用countvectorizer和tfidfvectorizer这两个类来提取文本特征

sklearn.feature_extraction.text.CountVectorizer官方文档
sklearn.feature_extraction.text.TfidfVectorizer官方文档


CountVectorizer

  • 先根据所有训练文本,不考虑其出现顺序,只将训练文本中每个出现过的词汇单独视为一列特征,构成一个词汇表;(词袋模型)
  • CountVectorizer()返回一个document-vocabulary词频矩阵:对每篇文档,统计每个词出现的频率。
class sklearn.feature_extraction.text.CountVectorizer()
  • 1

参数:

input=’content’string {‘filename’, ‘file’, ‘content’}

  • “filename”:作为参数传递给fit的序列应该是一个文件名列表,需要读取这些文件名以获取要分析的原始内容。
  • “file”:序列项必须有一个“read”方法(类文件对象),该方法被调用来获取内存中的字节。
  • 其他:输入应该是一个项目序列,类型可以是string或byte。

encoding='utf-8'string

  • 如果字节或文件被给予分析,这种编码被用来解码。

decode_error='strict'{‘strict’, ‘ignore’, ‘replace’}

  • 如果给定要分析的字节序列包含不属于给定编码的字符该做什么。

strip_accents=None{‘ascii’, ‘unicode’}

  • 在预处理步骤中删除重音符号并执行其他字符规范化。’ ascii ’ 是一种快速的方法,只对有直接ascii映射的字符有效。“unicode” 是一种稍微慢一些的方法,适用于任何字符。None(默认)不执行任何操作。’ ascii ’ 和’ unicode ’ 都使用NFKD标准化从unicodedata.normalize。

lowercase=True:boolean

  • 在tokenizing之前将所有字符转换为小写。

preprocessor=Nonecallable

  • 重写预处理(字符串转换)阶段,同时保留tokenizing和ngram生成步骤。仅在analyzer不可调用时应用。

tokenizer=Nonecallable

  • 重写字符串tokenization步骤,同时保留preprocessing和ngram生成步骤。只适用于analyzer == ‘word’。

stop_words=Nonestring{‘english’}, list

  • ‘english’:使用了内置的英语停止词列表。
  • list:假设该列表包含停止词,所有这些词都将从结果标记中删除。只适用于analyzer == ‘word’。
  • None:没有停止词将被使用。max_df设置为范围[0.7,1.0]的值,将根据术语在语料库文档内的频率自动检测和过滤停止词。

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

  • 表示什么构成了“token”的正则表达式,仅在analyzer == 'word’时使用。默认的regexp选择2个或更多字母数字字符的标记(标点完全被忽略,总是作为标记分隔符处理)。

ngram_range=(1, 1)tuple (min_n, max_n)

  • 要提取的不同单词ngram或字符ngram的n个值范围的上边界,使用min_n <= n <= max_n内的所有n值。例如,ngram_range的(1,1)表示仅使用单字符,(1,2)表示单字符和双字符,(2,2)表示仅使用双字符。仅在analyzer不可调用时应用。

analyzer=’word’string, {‘word’, ‘char’, ‘char_wb’} or callable

  • 该特征是由词ngram组成还是由字符ngram组成。选项 char_wb 只从单词边界内的文本创建ngram,单词边缘的ngram用空格填充。
  • 如果传递了callable,则使用它从原始的、未处理的输入中提取特征序列。
  • 从v0.21版本开始,如果inputfilenamefile,则首先从文件中读取数据,然后传递给给定的可调用分析器。

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

  • 在构建词汇表时,忽略文档频率严格高于给定阈值的词汇(特定于语料库的停止词汇)。如果是浮点数,则该参数表示文档的比例,如果是整数,则表示绝对计数。如果vocabulary不是None,则忽略此参数。

min_df=1float in range [0.0, 1.0] or int

  • 在构建词汇表时,忽略文档频率严格低于给定阈值的词汇。这个值在文献中也称为cut-off。如果是浮点数,则该参数表示文档的比例,如果是整数则表示绝对计数。如果vocabulary不是None,则忽略此参数。

max_features=Noneint or None

  • 如果不是None,则构建一个词汇表vocabulary,该词汇表只考虑corpus中按词频排序的最大max_features个词汇。如果vocabulary不是None,该参数将被忽略。

vocabulary=NoneMapping or iterable, optional

  • 要么是一个映射(例如dict),其中键是terms,值是特征矩阵中的索引,或者是一个可迭代的项。如果没有给出,则从输入文档确定词汇表。映射中的索引不应该重复,并且在0和最大索引之间不应该有间隙。

binary=Falsebool

  • 如果为真,则将所有非零计数设置为1。这对于建模二进制事件而不是整数计数的离散概率模型是有用的。

dtype=np.int64type

  • 由fit_transform()或transform()返回的矩阵的类型。

属性:

  • vocabulary_:dict字典。terms到特征索引的映射。
  • get_feature_:boolean。如果用户提供了术语到索引映射的固定词汇表,则为True。
  • stop_words_:返回停用词表。被忽略的terms(出现在太多文档中max_df,出现在太少文档中min_df,被特征选择丢弃max_features)

方法:

fit(self, raw_documents[, y])

  • Learn a vocabulary dictionary of all tokens in the raw documents.
  • 学习原始文档中所有标记的词汇字典。

fit_transform(self, raw_documents, y=None)

  • 计算各个词语出现的次数,拟合模型,并返回文本矩阵。
  • 学习词汇表字典并返回document-term矩阵。
  • 这相当于在fit之后transform,但实现起来更有效。

transform(self, raw_documents)

  • 将文档转换为document-term矩阵。
  • 使用适合fit的词汇表或提供给构造函数的词汇表从原始文本文档中提取标记数。

get_feature_names(self)

  • 返回特征名称列表。。从特征整数索引到特征名称的数组映射。

示例

>>> from sklearn.feature_extraction.text import CountVectorizer
>>> corpus = [
...     'This is the first document.',
...     'This document is the second document.',
...     'And this is the third one.',
...     'Is this the first document?',
... ]
>>> vectorizer = CountVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> print(vectorizer.get_feature_names())
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
>>> print(X.toarray())
[[0 1 1 1 0 0 1 0 1]
 [0 2 0 1 0 1 1 0 1]
 [1 0 0 1 1 0 1 1 1]
 [0 1 1 1 0 0 1 0 1]]
>>> vectorizer2 = CountVectorizer(analyzer='word', ngram_range=(2, 2))
>>> X2 = vectorizer2.fit_transform(corpus)
>>> print(vectorizer2.get_feature_names())
['and this', 'document is', 'first document', 'is the', 'is this',
'second document', 'the first', 'the second', 'the third', 'third one',
 'this document', 'this is', 'this the']
 >>> print(X2.toarray())
 [[0 0 1 1 0 0 1 0 0 0 0 1 0]
 [0 1 0 1 0 1 0 1 0 0 1 0 0]
 [1 0 0 1 0 0 0 0 1 1 0 1 0]
 [0 0 1 0 1 0 1 0 0 0 0 0 1]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

TfidfVectorizer

  • 先根据所有训练文本,不考虑其出现顺序,只将训练文本中每个出现过的词汇单独视为一列特征,构成一个词汇表;(词袋模型)
  • TfidfVectorizer ()返回一个document-vocabularyTF-IDF矩阵:对每篇文档 d,统计每个词 t 出现的TF-IDF(d,t)。

词 频 ( T F ( d , t ) ) = 词 t 在 文 档 d 中 出 现 的 次 数 文 档 d 中 的 总 词 数 词频(TF(d,t))=\frac{词 t 在文档d中出现的次数}{文档d中的总词数} TF(d,t)=dtd

逆 文 档 频 率 ( I D F ( d , t ) ) = log ⁡ 文 档 总 数 包 含 单 词 t 的 文 章 总 数 + 1 逆文档频率(IDF(d,t))=\log\frac{文档总数}{包含单词t的文章总数+1} IDF(d,t)=logt+1

T F − I D F ( d , t ) = T F ( d , t ) × I D F ( d , t ) TF-IDF(d,t)=TF(d,t)\times IDF(d,t) TFIDF(d,t)=TF(d,t)×IDF(d,t)

逆文档频率用来衡量单词对表达语义所起的重要性:如果一个单词在非常多的文档中都出现,那么它可能是一个比较通用的词汇,对于区分某篇文档特殊语义的贡献较小。

  • TfidfVectorizer 相当于先后调用 CountVectorizer 和 TfidfTransformer 两种方法。
  • TfidfTransformer:转换 CountVectorizer 处理后的document-vocabulary矩阵为标准化的 tf-idf 矩阵。
class sklearn.feature_extraction.text.TfidfVectorizer
  • 1

参数:

除了有CountVectorizer中的所有参数外,还有以下参数。

norm=’l2’{‘l1’, ‘l2’}

  • 每个输出行都有单位范数
  • ’ l2 ':向量元素的平方和为1。当应用l2范数时,两个向量之间的余弦相似度是它们的点积。
  • *‘l1’:向量元素的绝对值之和为1。

use_idf=Truebool

  • 使inverse-document-frequency调整权重。

smooth_idf=Truebool

  • 通过在文档频率上增加1来平滑idf权重,就好像在一个额外的文档中只包含集合中的每一个词一样。防止零作除数。

sublinear_tf=Falsebool

  • 应用次线性tf缩放,即将 tf 替换为1 + log(tf)。

属性:

  • vocabulary_:dict字典。terms到特征索引的映射。
  • fixed_vocabulary_: bool。如果用户提供了术语到索引的固定词汇表则返回True。
  • idf_:array of shape (n_features,)。反文档频率(IDF)向量,只有在use_idf为真时才定义。
  • stop_words_:set,返回停用词表。被忽略的terms(出现在太多文档中max_df,出现在太少文档中min_df,被特征选择丢弃max_features)

方法:

fit(self, raw_documents[, y])

  • Learn a vocabulary dictionary of all tokens in the raw documents.
  • 学习原始文档中所有标记的词汇字典。

fit_transform(self, raw_documents, y=None)

  • 计算各个词语出现的次数,拟合模型,并返回文本矩阵。
  • 学习词汇表字典并返回document-term矩阵。
  • 这相当于在fit之后transform,但实现起来更有效。

transform(self, raw_documents)

  • 将文档转换为document-term矩阵。
  • 使用适合fit的词汇表或提供给构造函数的词汇表从原始文本文档中提取标记数。

get_feature_names(self)

  • 返回特征名称列表。。从特征整数索引到特征名称的数组映射。

示例

>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> corpus = [
...     'This is the first document.',
...     'This document is the second document.',
...     'And this is the third one.',
...     'Is this the first document?',
... ]
>>> vectorizer = TfidfVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> print(vectorizer.get_feature_names())
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
>>> print(X.shape)
(4, 9)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/361060
推荐阅读
相关标签
  

闽ICP备14008679号