zoukankan      html  css  js  c++  java
  • 数据的特征抽取

    机器学习库scikit-learn,进行特征抽取

    特征工程:

    特征工程是将原始数据转换为更好地代表预测模型的潜在问题的特征的过程,从而提高了对未知数据的模型准确性

    特征工程意义:

    直接影响模型的预测结果

    字典特征抽取:

     

     1 from sklearn.feature_extraction import DictVectorizer
     2 
     3 
     4 
     5 def dict_vec():
     6     """
     7     字典数据,特征抽取
     8     :return: None
     9     """
    10     # 实例化
    11     d = DictVectorizer(sparse=False)  # 有无sparse参数返回的数据类型不一样
    12     # 调用 fit_transform
    13     data = d.fit_transform([{'city': '北京', 'temperature': 100}, {'city': '上海', 'temperature': 60}, {'city': '深圳', 'temperature': 80}])
    14     print(d.get_feature_names())
    15     print(data)
    16     return None
    17 
    18 # 字典数据抽取:把字典中一些类别数据,分别进行转换成特征
    19 if __name__ == '__main__':
    20     dict_vec()

    输出结果:

      ['city=上海', 'city=北京', 'city=深圳', 'temperature']
      [[ 0. 1. 0. 100.]
      [ 1. 0. 0. 60.]
      [ 0. 0. 1. 80.]]

     文本特征抽取:

     

     1 from sklearn.feature_extraction.text import CountVectorizer
     2 
     3 
     4 def count_vec():
     5     # 对文本进行特征值化
     6     cv = CountVectorizer()
     7     data = cv.fit_transform(['life is short, I like python is', 'life is too long, dislike python'])
     8     # print(data)
     9     print(cv.get_feature_names())
    10     print(data.toarray())  #统计个数,单个字母不统计
    11     # 中文的话要用到jieba分词

    中文文本特征抽取:

     1 from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
     2 import jieba
     3 
     4 
     5 def cn_vec():
     6     # 中文特征值化
     7     c1, c2, c3 = cutword()
     8     cv = CountVectorizer()
     9     data = cv.fit_transform([c1, c2, c3])
    10     print(cv.get_feature_names())
    11     print(data.toarray())
    12 
    13 def cutword():
    14     # 中文分词
    15     c1 = jieba.cut('今天很残酷,明天更残酷,后天很美好,但绝对大部分是死在明天晚上,所以每个人不要放弃今天。')
    16     c2 = jieba.cut('我们看到的从很远星系来的光是在几百万年之前发出的,这样当我们看到宇宙时,我们是在看它的过去。')
    17     c3 = jieba.cut('如果只用一种方式了解某样事物,你就不会真正了解它。了解事物真正含义的秘密取决于如何将其与我们所了解的事物相联系')
    18     content1 = list(c1)  # 转化为列表
    19     content2 = list(c2)
    20     content3 = list(c3)
    21     # 把列表转化为字符串
    22     content1 = ' '.join(content1)
    23     content2 = ' '.join(content2)
    24     content3 = ' '.join(content3)
    25     return content1, content2, content3

    TF-IDF判断词语的重要性:

     

     分类机器学习算法的重要依据!

     

     

     

     1 from sklearn.feature_extraction.text import TfidfVectorizer
     2 
     3 
     4 def tfidf_vec():
     5     # tfidf 得出词的重要性
     6     c1, c2, c3 = cutword()
     7     tf = TfidfVectorizer()
     8     data = tf.fit_transform([c1, c2, c3])
     9     print(tf.get_feature_names())
    10     print(data.toarray())
  • 相关阅读:
    搭建Keil C51开发环境
    源码分析之Handler
    Android中的算法
    Android中的数据结构
    高级UI-UI绘制流程
    高级UI-Path和PathMeasure
    高级UI-画板Canvas
    高级UI-滤镜和颜色通道
    高级UI-高级渲染
    高级UI-画笔Paint
  • 原文地址:https://www.cnblogs.com/springionic/p/11637509.html
Copyright © 2011-2022 走看看