特征抽取sklearn.feature_extraction 模块提供了从原始数据如文本,图像等众抽取能够被机器学习算法直接处理的特征向量。

1.特征抽取方法之 Loading Features from Dicts

measurements=[
{'city':'Dubai','temperature':33.},
{'city':'London','temperature':12.},
{'city':'San Fransisco','temperature':18.},
] from sklearn.feature_extraction import DictVectorizer
vec=DictVectorizer()
print(vec.fit_transform(measurements).toarray())
print(vec.get_feature_names()) #[[ 1. 0. 0. 33.]
#[ 0. 1. 0. 12.]
#[ 0. 0. 1. 18.]] #['city=Dubai', 'city=London', 'city=San Fransisco', 'temperature']

2.特征抽取方法之 Features hashing

3.特征抽取方法之 Text Feature Extraction

词袋模型 the bag of words represenatation

#词袋模型
from sklearn.feature_extraction.text import CountVectorizer
#查看默认的参数
vectorizer=CountVectorizer(min_df=1)
print(vectorizer) """
CountVectorizer(analyzer='word', binary=False, decode_error='strict',
dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',
lowercase=True, max_df=1.0, max_features=None, min_df=1,
ngram_range=(1, 1), preprocessor=None, stop_words=None,
strip_accents=None, token_pattern='(?u)\\b\\w\\w+\\b',
tokenizer=None, vocabulary=None) """ corpus=["this is the first document.",
"this is the second second document.",
"and the third one.",
"Is this the first document?"]
x=vectorizer.fit_transform(corpus)
print(x) """
(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
"""

默认是可以识别的字符串至少为2个字符

analyze=vectorizer.build_analyzer()
print(analyze("this is a document to anzlyze.")==
    (["this","is","document","to","anzlyze"])) #True

在fit阶段被analyser发现的每一个词语都会被分配一个独特的整形索引,该索引对应于特征向量矩阵中的一列

print(vectorizer.get_feature_names()==(
["and","document","first","is","one","second","the","third","this"]
))
#True
print(x.toarray())
"""
[[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]]
"""

获取属性

print(vectorizer.vocabulary_.get('document'))
#

对于一些没有出现过的字或者字符,则会显示为0

vectorizer.transform(["somthing completely new."]).toarray()
"""
[[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]]
"""

在上边的语料库中,第一个和最后一个单词是一模一样的,只是顺序不一样,他们会被编码成相同的特征向量,所以词袋表示法会丢失了单词顺序的前后相关性信息,为了保持某些局部的顺序性,可以抽取2个词和一个词

bigram_vectorizer=CountVectorizer(ngram_range=(1,2),token_pattern=r"\b\w+\b",min_df=1)
analyze=bigram_vectorizer.build_analyzer()
print(analyze("Bi-grams are cool!")==(['Bi','grams','are','cool','Bi grams',
'grams are','are cool'])) #True
x_2=bigram_vectorizer.fit_transform(corpus).toarray()
print(x_2) """
[[0 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0]
[0 0 1 0 0 1 1 0 0 2 1 1 1 0 1 0 0 0 1 1 0]
[1 1 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1 1 0 0 0]
[0 0 1 1 1 1 0 1 0 0 0 0 1 1 0 0 0 0 1 0 1]]
"""

sklearn特征抽取的更多相关文章

  1. 《机学一》特征工程1 ——文本处理:sklearn抽取、jieba中文分词、TF和IDF抽取

    零.机器学习整个实现过程: 一.机器学习数据组成 特征值: 目标值: 二.特征工程和文本特征提取 1.概要: 1.特征工程是什么 2.特征工程的意义:直接影响预测结果 3.scikit-learn库 ...

  2. python 机器学习(一)机器学习概述与特征工程

      一.机器学习概述 1.1.什么是机器学习? 机器学习是从数据中自动分析获得规律(模型),并利用规律对未知数据进行预测 1.2.为什么需要机器学习? 解放生产力,智能客服,可以不知疲倦的24小时作业 ...

  3. 特征抽取: sklearn.feature_extraction.FeatureHasher

    sklearn.feature_extraction.FeatureHasher(n_features=1048576, input_type="dict", dtype=< ...

  4. 特征抽取: sklearn.feature_extraction.DictVectorizer

    sklearn.featture_extraction.DictVectorizer: 将特征与值的映射字典组成的列表转换成向量. DictVectorizer通过使用scikit-learn的est ...

  5. 利用sklearn进行tfidf计算

    转自:http://blog.csdn.net/liuxuejiang158blog/article/details/31360765?utm_source=tuicool 在文本处理中,TF-IDF ...

  6. AI学习---特征工程【特征抽取、特征预处理、特征降维】

    学习框架 特征工程(Feature Engineering) 数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已 什么是特征工程: 帮助我们使得算法性能更好发挥性能而已 sklearn主 ...

  7. sklearn多分类问题

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  8. sklearn文本特征提取

    http://cloga.info/2014/01/19/sklearn_text_feature_extraction/ 文本特征提取 词袋(Bag of Words)表征 文本分析是机器学习算法的 ...

  9. sklearn算法库的顶层设计

    sklearn监督学习的各个模块 neighbors近邻算法,svm支持向量机,kernal_ridge核岭回归,discriminant_analysis判别分析,linear_model广义线性模 ...

随机推荐

  1. 第三百八十四节,Django+Xadmin打造上线标准的在线教育平台—路由映射与静态文件配置以及会员注册

    第三百八十四节,Django+Xadmin打造上线标准的在线教育平台—路由映射与静态文件配置以及会员注册 基于类的路由映射 from django.conf.urls import url, incl ...

  2. php生成二维码的几种方式[转]

      二维码是二维条形码的一种,可以将网址.文字.照片等信息通过相应的编码算法编译成为一个方块形条码图案,手机用户可以通过摄像头和解码软件将相关信息重新解码并查看内容.PHP可以使用php QR Cod ...

  3. mongodb 学习1

    基本概念 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式( 文件存储格式为BSON(一种JS ...

  4. 1 salt执行模块开发

    saltstack自带的模块已经很多了,但是有些时候我们需要自己开发出自己的模块来满足自己的需求,那就要自己开发了,下面请看例子 创建_modules目录在file_roots下面 我们需要在file ...

  5. V8学习资源

    http://wenku.baidu.com/link?url=7mk6ib9IJ-1K_-bFVZ-fXXo2nbI3jOJ3cnhr3XC3b08fPOKkc9v8vSAd-DgNMJZ2vdjg ...

  6. 图像中的artifacts

    artifacts 瑕疵 伪影(Artifacts) 伪影(Artifacts)-CT-基础术语 - 影像园 http://www.xctmr.com/baike/ct/c34b5413e305b45 ...

  7. web front end stack web 前段技术概览

    https://github.com/unruledboy/WebFrontEndStack

  8. 虚拟机中安装linux系统步骤

    参考:http://blog.csdn.net/u013111221/article/details/50856934 后面参考:http://blog.csdn.net/chenweitang123 ...

  9. java用substring函数截取string中一段字符串

    在String中有两个substring()函数,如下: 一:String.substring(int start) 参数: start:要截取位置的索引 返回: 从start开始到结束的字符串 例如 ...

  10. 兼容 iOS Retina(视网膜显示) 的程序

    首先我们需要明确一点,iOS设备上图片兼容retina的问题最初是由于iPhone4的分辨率由iPhone3的320X480提升到了640X960所产生. 为了让iPhone4能够兼容iPhone3上 ...