数据的存在形式是多样的,除了我们平时常见的数值型数据之外,还有文本类型的数据,本篇讨论的是文本类型数据的处理方法,即对文本类型数据特征值化。
本文仍是借助机器学习语言工具中的sklearn模块来完成。
假设现有一段语言(文章):You will never know unless you try。我们要完成对这段文字进行特征抽取,我们先来看特征抽取的单词列表
# -*- coding:utf-8 -*- # @Author: 数据与编程之美 # @File: text_deal.py # @Time: 2020/12/29 22:34 from sklearn.feature_extraction.text import CountVectorizer def text_deal(): """ 文本特征抽取 :return: """ count_v = CountVectorizer() count_v.fit_transform(["You will never know unless you try"]) # 输出词条列表 print(count_v.get_feature_names()) if __name__ == '__main__': text_deal()
词条列表结果如下
用图简单法分析一下就是
get_feature_names()该方法返回的是文章中去重后的所有单词(单个的如:i 之类词的单个字母不会统计在内,因为没意义)
接下来再来看看这段文字统计词条列表对应的特征值化数据
# -*- coding:utf-8 -*- # @Author: 数据与编程之美 # @File: text_deal.py # @Time: 2020/12/29 22:34 from sklearn.feature_extraction.text import CountVectorizer def text_deal(): """ 文本特征抽取 :return: """ count_v = CountVectorizer() data = count_v.fit_transform(["You will never know unless you try"]) # 输出词条列表 print(count_v.get_feature_names()) # 输出词条列表对应的值化数据 print(data) if __name__ == '__main__': text_deal()
结果如下:
结果解释:
上述的坐标值数据结构其实是一个稀疏矩阵(Sparse matrix),意思是元素大部分为零的矩阵,上述结果为零的值很少是因为其适用于文章单词数远大于本案例所用的那一句话的单词个数,一般正常情况下稀疏矩阵是很明显的。
稀疏矩阵还有一个明显的特征就是只存储非零数据,这样做的目的是使运算速度更快和压缩存储达到节省空间,如
第0行,第5列存储的值是2
第0行,第4列存储的值是1
第0行,第1列存储的值是1
第0行,第0列存储的值是1
第0行,第3列存储的值是1
第0行,第2列存储的值是1
实际情况下会有多行的内容,后面会看到。为了更直观的观察文本特征值化后的结果数据,一般不会输出坐标形式的稀疏矩阵,而是将稀疏矩阵转化为数组的形式输出
转化方法便是 data.toarray()
这样可以很直观看到每个词条对应的统计次数
know : 1次
never : 1次
try : 1次
unless : 1次
will : 1次
you : 2次
下面以长文章示例
# -*- coding:utf-8 -*- # @Author: 数据与编程之美 # @File: text_deal.py # @Time: 2020/12/29 22:34 from sklearn.feature_extraction.text import CountVectorizer def text_deal(): """ 文本特征抽取 :return: """ count_v = CountVectorizer() data = count_v.fit_transform(["There are moments in life when you miss someone so much that you just want to pick them from your dreams", "Dream what you want to dream;go where you want to go", "be what you want to be", "because you have only one life and one chance to do all the things you want to do"]) # 输出词条列表 print(count_v.get_feature_names()) # 输出词条列表对应的值化数据 print(data.toarray()) if __name__ == '__main__': text_deal()
结果如下:
词条列表:
[‘all’, ‘and’, ‘are’, ‘be’, ‘because’, ‘chance’, ‘do’, ‘dream’, ‘dreams’, ‘from’, ‘go’, ‘have’, ‘in’, ‘just’, ‘life’, ‘miss’, ‘moments’, ‘much’, ‘one’, ‘only’, ‘pick’, ‘so’, ‘someone’, ‘that’, ‘the’, ‘them’, ‘there’, ‘things’, ‘to’, ‘want’, ‘what’, ‘when’, ‘where’, ‘you’, ‘your’]
显示0的‘稀疏矩阵’
[[0 0 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 0 1 1 0 1 0 2 1] [0 0 0 0 0 0 0 2 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 1 0 1 2 0] [0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0] [1 1 0 0 1 1 2 0 0 0 0 1 0 0 1 0 0 0 2 1 0 0 0 0 1 0 0 1 2 1 0 0 0 2 0]]
结果说明:
词条列表中的’all‘在红框年内容出现了0次,所有第0行第0列为0;’and‘在红框年内容出现了0次,所有第0行第1列为0;’are‘在红框年内容出现了1次,所有第0行第2列为1…以此类推。
汉字类型的文本型数据
对于中文默认是不支持特征抽取的,如果要对汉字进行特征值化需要做分词处理,如(用空格分开)
# -*- coding:utf-8 -*- # @Author: 数据与编程之美 # @File: text_deal.py # @Time: 2020/12/29 22:34 from sklearn.feature_extraction.text import CountVectorizer def text_deal(): """ 文本特征抽取 :return: """ count_v = CountVectorizer() data = count_v.fit_transform(["You will never know unless you try", "除非 你 努力", "否则 你 永远 不会 知道"]) # 输出词条列表 print(count_v.get_feature_names()) # 输出词条列表对应的值化数据 print(data.toarray()) if __name__ == '__main__': text_deal()
特征抽取结果(形式和英文文章一样):
对于含有大量中文的文本进行特征抽取时,认为分词就很耗时耗力,这里给大家介绍一个分词工具:jieba(结巴,我猜的)
安装jieba: pip3 install jieba
使用案例官方教程: https://github.com/fxsjy/jieba
更多精彩内容请关注公众号:
请先
!