五、特征工程

2018-10-21  本文已影响0人  一闪一闪亮日日日日日日

1 字典特征抽取

字典数据抽取:把字典中一些类别的数据分别进行转换成特征。
用到的代码是sklearn.feature_extraction.DictVectorizer

from sklearn.feature_extraction import DictVectorizer
def dictvec():
      dict = DicVectorizer(sparse=False) #矩阵形式 或者data.toarray()
      data = dict.fit_transform([{'city':'北京','temperature':100},{'city':'上海','temperature':60},{'city':'深圳','temperature':30}])  
      print(dict.get_feature_names())
      print(data)
if __name__=='__main__':
      dictvec()

输出:
['city=上海', 'city=北京', 'city=深圳', 'temperature']
[[ 0. 1. 0. 100.]
[ 1. 0. 0. 60.]
[ 0. 0. 1. 30.]]

2 文本特征抽取

2.1 CountVectorizer

对英文文本进行特征值化:
1、统计文章中所有的词,重复只看作一次
2、对每篇文章,在词的列表里进行统计每个词出现的次数
3、单个字母不统计。对于单个英文字母不同:没有分类依据
CountVectorizer:常用于文本分类,情感分析

from sklearn.feature_extraction.text import CountVectorizer
def cv():
    cv = CountVectorizer()
    data=cv.fit_transform(['life is short,i like python','life is too long,i dislike python'])
    print(cv.get_feature_names())
    print(data.toarray())

输出:
['dislike', 'is', 'life', 'like', 'long', 'python', 'short', 'too']
[[0 1 1 1 0 1 1 0]
[1 1 1 0 1 1 0 1]]

然而对于中文文本又是什么情况呢?

from sklearn.feature_extraction.text import CountVectorizer
def cv():
    cv = CountVectorizer()
    data = cv.fit_transform(['人生苦短,我喜欢python','人生漫长,不用python'])
    print(cv.get_feature_names())
    print(data.toarray())
if __name__=='__main__':
      cv()

输出:
['不用python', '人生漫长', '人生苦短', '我喜欢python']
[[0 0 1 1]
[1 1 0 0]]

我们可以看到程序自动给我们分隔了中文文本,然而划分的很粗糙,想划分的细点的话就要手动给他进行空格,如:

data = cv.fit_transform(['人生 苦短,我 喜欢 python','人生 漫长,不用 python'])

这样会很麻烦,我们可以用jieba进行分词:

from sklearn.feature_extraction.text import CountVectorizer
import jieba
def cut():
    con1=jieba.cut('今天很残酷,明天更残酷,后天很美好')
    con2=jieba.cut('我们看到的从很远星系来的光是在几百万年之前发出的,这样当我们看到宇宙时,我们是在看它的过去')
    con3=jieba.cut('如果只用一种方式了解某样事物,你就不会真正了解它。')
    #转成列表
    content1 = list(con1)
    content2 = list(con2)
    content3 = list(con3)
    #转成字符串
    c1 = ' '.join(content1)
    c2 = ' '.join(content2)
    c3 = ' '.join(content3)
    return c1,c2,c3
def cv():
    c1,c2,c3 = cut()
    print(c1,c2,c3)
    cv = CountVectorizer()
    data = cv.fit_transform([c1,c2,c3])
    print(cv.get_feature_names())
    print(data.toarray())
if __name__=='__main__':
    cv()

输出:

Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\ADMINI~1\AppData\Local\Temp\jieba.cache
Loading model cost 1.017 seconds.
今天 很 残酷 , 明天 更 残酷 , 后天 很 美好 我们 看到 的 从 很 远 星系 来 的 光是在 几百万年 之前 发出 的 , 这样 当 我们 看到 宇宙 时 , 我们 是 在 看 它 的 过去 如果 只用 一种 方式 了解 某样 事物 , 你 就 不会 真正 了解 它 。
['一种', '不会', '之前', '了解', '事物', '今天', '光是在', '几百万年', '发出', '只用', '后天', '如果', '宇宙', '我们', '方式', '明天', '星系', '某样', '残酷', '看到', '真正', '美好', '过去', '这样']
[[0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 2 0 0 1 0 0]
Prefix dict has been built succesfully.
 [0 0 1 0 0 0 1 1 1 0 0 0 1 3 0 0 1 0 0 2 0 0 1 1]
 [1 1 0 2 1 0 0 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 0]]

2.2 TfidfVectorizer

对于文本特征抽取,更常用的是TfidfVectorizer
它度量了一个词对于一个文档的重要程度,如果对于一篇文档重要,另一篇不重要,则可以作为划分标准
Tf-term frequency词的频率
idf-你文档频率(inverse document frequency) log(总文档数量/该次出现的文档数量)
tf*idf-重要性程度

from sklearn.feature_extraction.text import TfidfVectorizer
import jieba
def cutword():
    con1 = jieba.cut('今天很残酷,明天更残酷,后天很美好')
    con2 = jieba.cut('我们看到的从很远星系来的光是在几百万年之前发出的,这样当我们看到宇宙时,我们是在看它的过去')
    con3 = jieba.cut('如果只用一种方式了解某样事物,你就不会真正了解它。')
    content1 = list(con1)
    content2 = list(con2)
    content3 = list(con3)
    c1 = ' '.join(content1)
    c2 = ' '.join(content2)
    c3 = ' '.join(content3)
    return c1,c2,c3
def tfidfvec():
c1,c2,c3=cutword()
    tf = TfidfVectorizer()
    data = tf.fit_transform([c1,c2,c3])
    print(tf.get_feature_names())
    print(data.toarray())
    return None
if __name__=='__main__':
    tfidfvec()

输出

Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\ADMINI~1\AppData\Local\Temp\jieba.cache
Loading model cost 1.266 seconds.
['一种', '不会', '之前', '了解', '事物', '今天', '光是在', '几百万年', '发出', '只用', '后天', '如果', '宇宙', '我们', '方式', '明天', '星系', '某样', '残酷', '看到', '真正', '美好', '过去', '这样']
Prefix dict has been built succesfully.
[[0.         0.         0.         0.         0.         0.35355339
  0.         0.         0.         0.         0.35355339 0.
  0.         0.         0.         0.35355339 0.         0.
  0.70710678 0.         0.         0.35355339 0.         0.        ]
 [0.         0.         0.21821789 0.         0.         0.
  0.21821789 0.21821789 0.21821789 0.         0.         0.
  0.21821789 0.65465367 0.         0.         0.21821789 0.
  0.         0.43643578 0.         0.         0.21821789 0.21821789]
 [0.28867513 0.28867513 0.         0.57735027 0.28867513 0.
  0.         0.         0.         0.28867513 0.         0.28867513
  0.         0.         0.28867513 0.         0.         0.28867513
  0.         0.         0.28867513 0.         0.         0.        ]]
上一篇下一篇

猜你喜欢

热点阅读