赞
踩
在对文本进行分类时,需要首先对文本进行向量表示,常用到词袋模型。
词袋模型(Bow,Bag of Words)不考虑文本中词与词之间的上下文关系,仅仅只考虑所有词的权重(与词在文本中出现的频率有关),类似于将所有词语装进一个袋子里,每个词都是独立的,不含语义信息。
词袋模型的作用
词袋模型的缺点
如“我喜欢滑雪”“我不喜欢滑雪”两个文本是严重不相似的,但词袋模型会判为高度相似;而“我喜欢滑雪”与“我爱滑雪”表达的意思是极为接近,但词袋模型不能表示“喜欢”和“爱”的相似关系。
生成文本的词袋模型的步骤
词集模型(SoW,Set of Words)与词带模型类似,但仅考虑词是否在文本中出现,而不考虑词频。多数时候一般使用词袋模型。
e.g. 语料库中有4个文本:
I come to China to travel
This is a car polupar in China
I love tea and Apple
The work is to write some papers in science
上述语料生成的词典共有21个单词:
‘a’,
‘and’,
‘apple’,
‘car’,
‘china’,
‘come’,
‘i’,
‘in’,
‘is’,
‘love’,
‘papers’,
‘polupar’,
‘science’,
‘some’,
‘tea’,
‘the’,
‘this’,
‘to’,
‘travel’,
‘work’,
‘write’
每个单词的One-Hot Representation如下:
‘a’: [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] \;\;\;\;[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
‘and’: [ 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] \;\;[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
…
‘write’: [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,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 1/6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1/3, 1/6, 0, 0] [0,0,0,0,1/6,1/6,1/6,0,0,0,0,0,0,0,0,0,0,1/3,1/6,0,0]
[ 1 / 7 , 0 , 0 , 1 / 7 , 1 / 7 , 0 , 0 , 1 / 7 , 1 / 7 , 0 , 0 , 1 / 7 , 0 , 0 , 0 , 0 , 1 / 7 , 0 , 0 , 0 , 0 ] [1/7, 0, 0, 1/7, 1/7, 0, 0, 1/7, 1/7, 0, 0, 1/7, 0, 0, 0, 0, 1/7, 0, 0, 0, 0] [1/7,0,0,1/7,1/7,0,0,1/7,1/7,0,0,1/7,0,0,0,0,1/7,0,0,0,0]
[ 0 , 1 / 5 , 1 / 5 , 0 , 0 , 0 , 1 / 5 , 0 , 0 , 1 / 5 , 0 , 0 , 0 , 0 , 1 / 5 , 0 , 0 , 0 , 0 , 0 , 0 ] [0, 1/5, 1/5, 0, 0, 0, 1/5, 0, 0, 1/5, 0, 0, 0, 0, 1/5, 0, 0, 0, 0, 0, 0] [0,1/5,1/5,0,0,0,1/5,0,0,1/5,0,0,0,0,1/5,0,0,0,0,0,0]
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 / 9 , 1 , 0 , 1 / 9 , 0 , 1 / 9 , 1 / 9 , 0 , 1 / 9 , 0 , 1 / 9 , 0 , 1 / 9 , 1 / 9 ] [0, 0, 0, 0, 0, 0, 0, 1/9, 1, 0, 1/9, 0, 1/9, 1/9, 0, 1/9, 0, 1/9, 0, 1/9, 1/9] [0,0,0,0,0,0,0,1/9,1,0,1/9,0,1/9,1/9,0,1/9,0,1/9,0,1/9,1/9]
在大规模的文本处理中,由于特征的维度对应分词词汇表的大小,维度将会非常高,常使用Hash Trick的方法进行降维。
此外,词袋模型中的值也可以采用单词的TF-IDF值。
主要通过sklearn.feature_extraction.text
中的CountVectorizer
类实现。
CountVectorizer
是常见的特征数值计算类,支持通过stop_words
参数传入停止词,如果提供停止词参数,则对于英文会使用内置的停止词列表(a built-in stop word list for English)
对于每个文本通过fit_transform
方法计算每个单词在该文本中出现的频率,形成词频矩阵。
通过get_feature_names
可查看所有文本关键字,通过toarray
可查看到文本的词袋模型结果。
代码如下:
from sklearn.feature_extraction.text import CountVectorizer
corpus=["I come to China to travel",
"This is a car polupar in China",
"I love tea and Apple ",
"The work is to write some papers in science"]
vectorizer=CountVectorizer()
print("词频统计:")
#输出4个文本的词频统计:左边的括号中的两个数字分别为(文本序号,词序号),右边数字为频次
print(vectorizer.fit_transform(corpus))
print("\n词袋模型:")
print(vectorizer.fit_transform(corpus).toarray())
输出如下:
HashingVectorizer
类实现了基于signed hash trick的算法,进行降维。
代码如下:
from sklearn.feature_extraction.text import HashingVectorizer
vectorizerH=HashingVectorizer(n_features = 6,norm = None) #将19维词汇表哈希降维到6维
print("词频统计:")
print(vectorizerH.fit_transform(corpus))
print("\n词袋模型:")
print(vectorizerH.fit_transform(corpus).toarray())
输出如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。