在做文本分类等问题时,需要从大量语料中提取特征,词袋和TF-IDF模型是很好的选择

词袋模型

词袋模型是最原始的一类特征集,忽略掉了文本的语法和语序,用一组无序的单词序列来表达一段文字或者一个文档。可以这样理解,把整个文档集的所有出现的词都丢进袋子里面,然后无序的排出来(去掉重复的)。对每一个文档,按照词语出现的次数来表示文档。

句子1:我/有/一个/苹果

句子2:我/明天/去/一个/地方

句子3:你/到/一个/地方

句子4:我/有/我/最爱的/你

把所有词丢进一个袋子:我,有,一个,苹果,明天,去,地方,你,到,最爱的。这 4 句话中总共出现了这 10 个词。

建立一个无序列表:我,有,一个,苹果,明天,去,地方,你,到,最爱的。并根据每个句子中词语出现的次数来表示每个句子。

  • 句子 1 特征: ( 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 )
  • 句子 2 特征: ( 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 )
  • 句子 3 特征: ( 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 0 )
  • 句子 4 特征: ( 2 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 )

TF-IDF 模型

这种模型主要是用词汇的统计特征来作为特征集。TF-IDF 由两部分组成:TF(Term frequency,词频),IDF(Inverse document frequency,逆文档频率)两部分组成。

实现

在 Python 当中,我们可以通过 scikit-learn 来分别实现词袋模型以及 TF-IDF 模型。并且,使用 scikit-learn 库将会非常简单。这里要用到 CountVectorizer() 类以及 TfidfVectorizer() 类。

1
2
#词袋
sklearn.featur_extraction.text.CountVectorizer(min_df=1, ngram_range=(1,1))
  • min_df :忽略掉词频严格低于定阈值的词

  • ngram_range :将 text 分成 n1,n1+1,……,n2个不同的词组。比如比如’Python is useful’中ngram_range(1,3)之后可得到 ‘Python’ , ‘is’ , ‘useful’ , ‘Python is’ , ‘is useful’ , ‘Python is useful’。如果是ngram_range (1,1) 则只能得到单个单词’Python’ , ‘is’ , ‘useful’。

1
2
#Tf-idf
sklearn.feature_extraction.text.TfidfVectorizer(min_df=1,norm='l2',smooth_idf=True,use_idf=True,ngram_range=(1,1))
  • min_df: 忽略掉词频严格低于定阈值的词。

  • norm :标准化词条向量所用的规范。

  • smooth_idf:添加一个平滑 idf 权重,即 idf 的分母是否使用平滑,防止0权重的出现。

  • use_idf: 启用 idf 逆文档频率重新加权。

  • ngram_range:同词袋模型

词袋

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
28
29
30
31
32
33
34
35
36
37
38
39
#加载词袋类
>>> from sklearn.feature_extraction.text import CountVectorizer
#调整类的参数
>>> vectorizer = CountVectorizer(min_df=1, ngram_range=(1, 1))
#建立文本库
>>> corpus = ['This is the first document.',
... 'This is the second second document.',
... 'And the third one.',
... 'Is this the first document?']
#训练corpus获取词袋特征
>>> a = vectorizer.fit_transform(corpus)
>>> print(a)
(0, 1) 1
(0, 2) 1
(0, 6) 1
(0, 3) 1
(0, 8) 1
(1, 5) 2
(1, 1) 1
(1, 6) 1
(1, 3) 1
(1, 8) 1
(2, 4) 1
(2, 7) 1
(2, 0) 1
(2, 6) 1
(3, 1) 1
(3, 2) 1
(3, 6) 1
(3, 3) 1
(3, 8) 1
>>> a.toarray()
array([[0, 1, 1, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 2, 1, 0, 1],
[1, 0, 0, 0, 1, 0, 1, 1, 0],
[0, 1, 1, 1, 0, 0, 1, 0, 1]], dtype=int64)
#获取特征值
>>> vectorizer.get_feature_names()
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']

TF-IDF

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
28
29
30
31
32
33
34
35
>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> vectorizer = TfidfVectorizer(
... min_df=1, norm='l2', smooth_idf=True, use_idf=True, ngram_range=(1, 1))
>>> b = vectorizer.fit_transform(corpus)
>>> print(b)
(0, 8) 0.4387767428592343
(0, 3) 0.4387767428592343
(0, 6) 0.35872873824808993
(0, 2) 0.5419765697264572
(0, 1) 0.4387767428592343
(1, 8) 0.27230146752334033
(1, 3) 0.27230146752334033
(1, 6) 0.2226242923251039
(1, 1) 0.27230146752334033
(1, 5) 0.8532257361452784
(2, 6) 0.2884767487500274
(2, 0) 0.5528053199908667
(2, 7) 0.5528053199908667
(2, 4) 0.5528053199908667
(3, 8) 0.4387767428592343
(3, 3) 0.4387767428592343
(3, 6) 0.35872873824808993
(3, 2) 0.5419765697264572
(3, 1) 0.4387767428592343
>>> b.toarray()
array([[0. , 0.43877674, 0.54197657, 0.43877674, 0. ,
0. , 0.35872874, 0. , 0.43877674],
[0. , 0.27230147, 0. , 0.27230147, 0. ,
0.85322574, 0.22262429, 0. , 0.27230147],
[0.55280532, 0. , 0. , 0. , 0.55280532,
0. , 0.28847675, 0.55280532, 0. ],
[0. , 0.43877674, 0.54197657, 0.43877674, 0. ,
0. , 0.35872874, 0. , 0.43877674]])
>>> vectorizer.get_feature_names()
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']