python文本特征提取实例_理解python scikitlearn中的文本特征提取TfidfVectorizer

我在这篇文章中看到几个问题。How do the different arguments in TfidfVectorizer interact with one another?

你真的需要用它来培养直觉(不管怎样,这是我的经验)。

TfidfVectorizer是一种包字方法。在NLP中,单词序列和它们的窗口是很重要的;这种类型破坏了某些上下文。

如何控制输出哪些令牌?

设置ngram_range为(1,1)只输出一个单词标记,(1,2)为一个单词和两个单词标记,(2,3)为两个单词和三个单词标记,等等

ngram_range与analyzer携手工作。将analyzer设置为“word”以输出单词和短语,或将其设置为“char”以输出字符ngram。

如果希望输出同时具有“word”和“char”功能,请使用sklearn的FeatureUnion。示例here。

如何删除不需要的内容?

使用stop_words删除不太有意义的英语单词。

sklearn使用的停止词列表可以在以下位置找到:from sklearn.feature_extraction.stop_words import ENGLISH_STOP_WORDS

删除停止词的逻辑与以下事实有关:这些词没有太多含义,而且它们在大多数文本中都出现过:[('the', 79808),

('of', 40024),

('and', 38311),

('to', 28765),

('in', 22020),

('a', 21124),

('that', 12512),

('he', 12401),

('was', 11410),

('it', 10681),

('his', 10034),

('is', 9773),

('with', 9739),

('as', 8064),

('i', 7679),

('had', 7383),

('for', 6938),

('at', 6789),

('by', 6735),

('on', 6639)]

由于停止词的频率通常很高,因此使用max_df作为0.95的浮点值来删除前5%可能是有意义的,但是您假设前5%都是停止词,这可能不是这样。这真的取决于你的文本数据。在我的工作中,由于我在非常特定的主题中处理密集的文本(搜索查询数据),所以顶部的单词或短语不是停止词是非常常见的。

使用min_df作为整数来删除罕见的单词。如果它们只出现一两次,就不会增加多少价值,而且通常都很模糊。此外,通常会有很多这样的问题,因此使用saymin_df=5忽略它们可以大大减少内存消耗和数据大小。

我该如何包括被剥离的内容?

token_pattern使用正则表达式模式\b\w\w+\b,这意味着标记必须至少有2个字符长,这样“I”、“a”之类的单词和0-9之类的数字就会被删除。你还会注意到它去掉了撇号What happens first, ngram generation or stop word removal?

让我们做个小测试。import numpy as np

import pandas as pd

from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

from sklearn.feature_extraction.stop_words import ENGLISH_STOP_WORDS

docs = np.array(['what is tfidf',

'what does tfidf stand for',

'what is tfidf and what does it stand for',

'tfidf is what',

"why don't I use tfidf",

'1 in 10 people use tfidf'])

tfidf = TfidfVectorizer(use_idf=False, norm=None, ngram_range=(1, 1))

matrix = tfidf.fit_transform(docs).toarray()

df = pd.DataFrame(matrix, index=docs, columns=tfidf.get_feature_names())

for doc in docs:

print(' '.join(word for word in doc.split() if word not in ENGLISH_STOP_WORDS))

打印出来:tfidf

does tfidf stand

tfidf does stand

tfidf

don't I use tfidf

1 10 people use tfidf

现在让我们打印df:10 and does don for in is \

what is tfidf 0.0 0.0 0.0 0.0 0.0 0.0 1.0

what does tfidf stand for 0.0 0.0 1.0 0.0 1.0 0.0 0.0

what is tfidf and what does it stand for 0.0 1.0 1.0 0.0 1.0 0.0 1.0

tfidf is what 0.0 0.0 0.0 0.0 0.0 0.0 1.0

why don't I use tfidf 0.0 0.0 0.0 1.0 0.0 0.0 0.0

1 in 10 people use tfidf 1.0 0.0 0.0 0.0 0.0 1.0 0.0

it people stand tfidf use \

what is tfidf 0.0 0.0 0.0 1.0 0.0

what does tfidf stand for 0.0 0.0 1.0 1.0 0.0

what is tfidf and what does it stand for 1.0 0.0 1.0 1.0 0.0

tfidf is what 0.0 0.0 0.0 1.0 0.0

why don't I use tfidf 0.0 0.0 0.0 1.0 1.0

1 in 10 people use tfidf 0.0 1.0 0.0 1.0 1.0

what why

what is tfidf 1.0 0.0

what does tfidf stand for 1.0 0.0

what is tfidf and what does it stand for 2.0 0.0

tfidf is what 1.0 0.0

why don't I use tfidf 0.0 1.0

1 in 10 people use tfidf 0.0 0.0

注:use_idf=False, norm=None设置这些值时,就相当于使用sklearn的countvector。它只会返回计数。

注意,单词“don't”被转换成了“don”。在这里,您可以将token_pattern更改为token_pattern=r"\b\w[\w']+\b"这样的值来包含撇号。

我们看到很多停止语

让我们删除stopwords并再次查看df:tfidf = TfidfVectorizer(use_idf=False, norm=None, stop_words='english', ngram_range=(1, 2))

输出:10 10 people does does stand \

what is tfidf 0.0 0.0 0.0 0.0

what does tfidf stand for 0.0 0.0 1.0 0.0

what is tfidf and what does it stand for 0.0 0.0 1.0 1.0

tfidf is what 0.0 0.0 0.0 0.0

why don't I use tfidf 0.0 0.0 0.0 0.0

1 in 10 people use tfidf 1.0 1.0 0.0 0.0

does tfidf don don use people \

what is tfidf 0.0 0.0 0.0 0.0

what does tfidf stand for 1.0 0.0 0.0 0.0

what is tfidf and what does it stand for 0.0 0.0 0.0 0.0

tfidf is what 0.0 0.0 0.0 0.0

why don't I use tfidf 0.0 1.0 1.0 0.0

1 in 10 people use tfidf 0.0 0.0 0.0 1.0

people use stand tfidf \

what is tfidf 0.0 0.0 1.0

what does tfidf stand for 0.0 1.0 1.0

what is tfidf and what does it stand for 0.0 1.0 1.0

tfidf is what 0.0 0.0 1.0

why don't I use tfidf 0.0 0.0 1.0

1 in 10 people use tfidf 1.0 0.0 1.0

tfidf does tfidf stand use \

what is tfidf 0.0 0.0 0.0

what does tfidf stand for 0.0 1.0 0.0

what is tfidf and what does it stand for 1.0 0.0 0.0

tfidf is what 0.0 0.0 0.0

why don't I use tfidf 0.0 0.0 1.0

1 in 10 people use tfidf 0.0 0.0 1.0

use tfidf

what is tfidf 0.0

what does tfidf stand for 0.0

what is tfidf and what does it stand for 0.0

tfidf is what 0.0

why don't I use tfidf 1.0

1 in 10 people use tfidf 1.0

外卖:标记“don use”的出现是因为don't I use去掉了't,并且I少于两个字符,所以它被删除,因此单词被连接到don use。。。实际上这不是结构,可能会改变结构一点!

回答:删除停止字,删除短字符,然后生成可返回意外结果的ngram。does it make sense to use max_df/min_df arguments together with use_idf argument?

我的观点是,术语频率逆文档频率的整个要点是允许对高度频繁的单词(出现在排序频率列表顶部的单词)进行重新加权。这种重新加权将采用最高频率的ngram,并将它们向下移动到较低的位置。因此,它应该处理max_df场景。

也许这更多的是个人的选择,你是想把它们从列表中移下来(“重新加权/取消它们的优先级”)还是完全删除它们。

我经常使用min_df,如果您使用的是一个巨大的数据集,那么使用min_df是有意义的,因为很少的单词不会增加值,只会导致很多处理问题。我不怎么用max_df但我肯定有在处理像维基百科一样的数据时,删除前x%可能有意义吗。

你可能感兴趣的:(python文本特征提取实例)