sklearn.feature_extraction.FeatureHasher(n_features=1048576, input_type="dict", dtype=<class 'numpy.float64'>, alternate_sign=True, non_negative=False):
  特征散列化的实现类。
  此类将符号特性名称(字符串)的序列转换为scipy.sparse矩阵,使用哈希函数计算与名称对应的矩阵列。使用的散列函数是带符号的32位版本的Murmurhash3.

  字节字符串类型的特征名称按原样使用。Unicode字符串首先转换为UTF-8,但没有进行Unicode规范化。特征值必须是(有限)数字
  本类是DictVectorizer和CountVectorizer的低内存替代品,用于大规模(在线)学习和内存紧张的情况,例如在嵌入式设备上运行预测代码时。

  n_features: integer
    输出矩阵的特征数,少量的特征可能引发hash冲突,大量的特征会导致线性学习的维度扩大。
  input_type:
    "dict"表示输入数据是字典形式的[{feature_name: value}, …],
    "pair"表示输入数据是pair形式的[[(feature_name1, value1), (feature_name2, value2)], …]
    "string"表示数据是字符串形式的[[feature_name1, feature_name1]],其中有个value1个feature_name1,value2个feature_name2
    其中feature_name必须是字符串,value必须是数字。在"string"的情况下,每个feature_name隐含value是1。特征名称会进行hash处理,来计算该特征名称对应的hash列。value的符号在输出的数据中可能会发生反转。
  dtype:
    特征值得类型。这个值将传递给scipy.sparse矩阵作为构造器dtype参数的值。这个参数不能设置为bool,np.boolean或者其他无符号的整型。
  alternate_sign:
    如果为True,则在特征计算出的hash值上交替添加一个符号(正数变成负数),以便于在散列空间中大致的保留内部积。这种方法类似于稀疏随机投影。

  non_negative:
    如果为真,在计算结果返回前,对特征矩阵进行绝对值计算。当与alternate_sign=True一同使用时,会显著降低内部积的保存性能。

  该类的方法与其他的特征提取类的方法一致。
  以下代码例子来源自sklearn官网API。
  地址: https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.FeatureHasher.html#sklearn.feature_extraction.FeatureHasher

例子1:

   from sklearn.feature_extraction import FeatureHasher
h = FeatureHasher(n_features=10, input_type='string', dtype=int, alternate_sign=False)
d = [{'dog': 1, 'cat': 2, 'elephant': 4}, {'dog': 2, 'run': 5}]
d = [[('dog', 1), ('cat', 2), ('elephant', 4)], [('dog', 2), ('run', 5)]]
d = [['dog', 'cat', 'cat', 'elephant', 'elephant','elephant','elephant',],
["dog", "dog", "run", 'run', 'run', 'run', 'run'],
["run", "run"]]
f = h.transform(d)
print(f.toarray())
print(h.get_params())

例子2:

from __future__ import print_function
from collections import defaultdict
import re
import sys
from time import time import numpy as np from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction import DictVectorizer, FeatureHasher
from memory_profiler import profile def n_nonzero_columns(X):
"""Returns the number of non-zero columns in a CSR matrix X."""
return len(np.unique(X.nonzero()[1])) def tokens(doc):
"""
简单的将doc拆分成词语,删除英文字母外的符号,并且都小写化
:param doc:
:return:
"""
return (tok.lower() for tok in re.findall(r"\w+", doc)) def token_freqs(doc):
"""
对doc中的词语进行频率统计
:param doc:
:return:
"""
freq = defaultdict(int)
for tok in tokens(doc):
freq[tok] += 1
return freq @profile
def dict_vectorizer(raw_data, data_size_mb):
print("DictVectorizer")
t0 = time()
vectorizer = DictVectorizer()
X = vectorizer.fit_transform(token_freqs(d) for d in raw_data)
duration = time() - t0
print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration))
print("Found %d unique terms\n" % len(vectorizer.get_feature_names()))
print("X.shape: ", X.shape) @profile
def feature_hasher_freq(raw_data, data_size_mb, n_features):
print("FeatureHasher on frequency dicts")
t0 = time()
hasher = FeatureHasher(n_features=n_features)
X = hasher.transform(token_freqs(d) for d in raw_data)
duration = time() - t0
print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration))
print("Found %d unique terms\n" % n_nonzero_columns(X))
print("X.shape: ", X.shape)
del X @profile
def feature_hasher_terms(raw_data, data_size_mb, n_features):
print("FeatureHasher on raw tokens")
t0 = time()
hasher = FeatureHasher(n_features=n_features, input_type="string")
X = hasher.transform(tokens(d) for d in raw_data)
duration = time() - t0
print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration))
print("Found %d unique terms" % n_nonzero_columns(X))
print("X.shape: ", X.shape)
del X @profile
def compare():
# 1. 只选择一部分数据
categories = [
'alt.atheism',
'comp.graphics',
'comp.sys.ibm.pc.hardware',
'misc.forsale',
'rec.autos',
'sci.space',
'talk.religion.misc',
] print("Usage: %s [n_features_for_hashing]" % sys.argv[0])
print(" The default number of features is 2**18.\n\n") try:
n_features = int(sys.argv[1])
except IndexError:
n_features = 2 ** 18
except ValueError:
print("not a valid number of features: %r" % sys.argv[1])
sys.exit(1) print("Loading 20 newsgroups training data")
# 2. 第一次运行时,下载文件需要较长的时间
# data_home 下载下来的文件保存的位置
# 如果data_home中没有文件,download_if_missing设置为True,程序会自动下载文件到data_home
raw_data = fetch_20newsgroups(data_home=r"D:\学习\sklearn_dataset\20newsbydate",
subset='train',
categories=categories,
download_if_missing=True
).data # 3. 计算文本的大小
data_size_mb = sum(len(s.encode('utf-8')) for s in raw_data) / 1e6
print("%d documents - %0.3fMB\n" % (len(raw_data), data_size_mb)) dict_vectorizer(raw_data, data_size_mb)
feature_hasher_freq(raw_data, data_size_mb, n_features)
feature_hasher_terms(raw_data, data_size_mb, n_features) if __name__ == '__main__':
compare()

例子2输出:
  

Usage: D:/Project/nlplearn/sklearn_learn/plot_hashing_vs_dictvectorizer.py [n_features_for_hashing]
The default number of features is 2**18. Loading 20 newsgroups training data
3803 documents - 6.245MB DictVectorizer
done in 16.495944s at 0.379MB/s
Found 47928 unique terms X.shape: (3803, 47928)
Filename: D:/Project/nlplearn/sklearn_learn/plot_hashing_vs_dictvectorizer.py Line # Mem usage Increment Line Contents
================================================
42 98.9 MiB 98.9 MiB @profile
43 def dict_vectorizer(raw_data, data_size_mb):
44 98.9 MiB 0.0 MiB print("DictVectorizer")
45 98.9 MiB 0.0 MiB t0 = time()
46 98.9 MiB 0.0 MiB vectorizer = DictVectorizer()
47 130.7 MiB 1.3 MiB X = vectorizer.fit_transform(token_freqs(d) for d in raw_data)
48 130.7 MiB 0.0 MiB duration = time() - t0
49 130.7 MiB 0.0 MiB print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration))
50 130.7 MiB 0.0 MiB print("Found %d unique terms\n" % len(vectorizer.get_feature_names()))
51 130.7 MiB 0.0 MiB print("X.shape: ", X.shape) FeatureHasher on frequency dicts
done in 8.953512s at 0.697MB/s
Found 43873 unique terms X.shape: (3803, 262144)
Filename: D:/Project/nlplearn/sklearn_learn/plot_hashing_vs_dictvectorizer.py Line # Mem usage Increment Line Contents
================================================
53 106.5 MiB 106.5 MiB @profile
54 def feature_hasher_freq(raw_data, data_size_mb, n_features):
55 106.5 MiB 0.0 MiB print("FeatureHasher on frequency dicts")
56 106.5 MiB 0.0 MiB t0 = time()
57 106.5 MiB 0.0 MiB hasher = FeatureHasher(n_features=n_features)
58 116.8 MiB 4.0 MiB X = hasher.transform(token_freqs(d) for d in raw_data)
59 116.8 MiB 0.0 MiB duration = time() - t0
60 116.8 MiB 0.0 MiB print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration))
61 116.8 MiB 0.0 MiB print("Found %d unique terms\n" % n_nonzero_columns(X))
62 116.8 MiB 0.0 MiB print("X.shape: ", X.shape)
63 106.6 MiB 0.0 MiB del X FeatureHasher on raw tokens
done in 9.989571s at 0.625MB/s
Found 43873 unique terms
X.shape: (3803, 262144)
Filename: D:/Project/nlplearn/sklearn_learn/plot_hashing_vs_dictvectorizer.py Line # Mem usage Increment Line Contents
================================================
65 106.6 MiB 106.6 MiB @profile
66 def feature_hasher_terms(raw_data, data_size_mb, n_features):
67 106.6 MiB 0.0 MiB print("FeatureHasher on raw tokens")
68 106.6 MiB 0.0 MiB t0 = time()
69 106.6 MiB 0.0 MiB hasher = FeatureHasher(n_features=n_features, input_type="string")
70 118.6 MiB 4.0 MiB X = hasher.transform(tokens(d) for d in raw_data)
71 118.6 MiB 0.0 MiB duration = time() - t0
72 118.6 MiB 0.0 MiB print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration))
73 118.6 MiB 0.0 MiB print("Found %d unique terms" % n_nonzero_columns(X))
74 118.6 MiB 0.0 MiB print("X.shape: ", X.shape)
75 106.7 MiB 0.0 MiB del X Filename: D:/Project/nlplearn/sklearn_learn/plot_hashing_vs_dictvectorizer.py Line # Mem usage Increment Line Contents
================================================
78 71.5 MiB 71.5 MiB @profile
79 def compare():
80 # 1. 只选择一部分数据
81 categories = [
82 71.5 MiB 0.0 MiB 'alt.atheism',
83 71.5 MiB 0.0 MiB 'comp.graphics',
84 71.5 MiB 0.0 MiB 'comp.sys.ibm.pc.hardware',
85 71.5 MiB 0.0 MiB 'misc.forsale',
86 71.5 MiB 0.0 MiB 'rec.autos',
87 71.5 MiB 0.0 MiB 'sci.space',
88 71.5 MiB 0.0 MiB 'talk.religion.misc',
89 ]
90
91 71.5 MiB 0.0 MiB print("Usage: %s [n_features_for_hashing]" % sys.argv[0])
92 71.5 MiB 0.0 MiB print(" The default number of features is 2**18.\n\n")
93
94 71.5 MiB 0.0 MiB try:
95 71.5 MiB 0.0 MiB n_features = int(sys.argv[1])
96 71.5 MiB 0.0 MiB except IndexError:
97 71.5 MiB 0.0 MiB n_features = 2 ** 18
98 except ValueError:
99 print("not a valid number of features: %r" % sys.argv[1])
100 sys.exit(1)
101
102 71.5 MiB 0.0 MiB print("Loading 20 newsgroups training data")
103 # 2. 第一次运行时,下载文件需要较长的时间
104 # data_home 下载下来的文件保存的位置
105 # 如果data_home中没有文件,download_if_missing设置为True,程序会自动下载文件到data_home
106 71.5 MiB 0.0 MiB raw_data = fetch_20newsgroups(data_home=r"D:\学习\sklearn_dataset\20newsbydate",
107 71.5 MiB 0.0 MiB subset='train',
108 71.5 MiB 0.0 MiB categories=categories,
109 98.0 MiB 26.5 MiB download_if_missing=True
110 ).data
111
112 # 3. 计算文本的大小
113 98.9 MiB 0.1 MiB data_size_mb = sum(len(s.encode('utf-8')) for s in raw_data) / 1e6
114 98.9 MiB 0.0 MiB print("%d documents - %0.3fMB\n" % (len(raw_data), data_size_mb))
115
116 106.5 MiB 7.6 MiB dict_vectorizer(raw_data, data_size_mb)
117 106.6 MiB 0.1 MiB feature_hasher_freq(raw_data, data_size_mb, n_features)
118 106.7 MiB 0.1 MiB feature_hasher_terms(raw_data, data_size_mb, n_features)

  从输出信息可以看出:
    FeatureHasher相比于DictVectorizer:
      1. FeatureHasher转化的速度更快。如果更改n_features, FeatureHasher的速度会发生变化,但是仍然比DictVectorizer更快一些。
      2. FeatureHasher的特征数少于DictVectorizer,部分特征被压缩了。

特征抽取: sklearn.feature_extraction.FeatureHasher的更多相关文章

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

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

  2. sklearn.feature_extraction.text 的TfidfVectorizer函数

    TfidfVectorizer函数主要用于,将文档(句子)等通过 tf-idf值来进行表示,也就是用一个tf-idf值的矩阵来表示文档(句子也可). from sklearn.feature_extr ...

  3. sklearn.feature_extraction.text.CountVectorizer 学习

    CountVectorizer: CountVectorizer可以将文本文档集合转换为token计数矩阵.(token可以理解成词) 此实现通过使用scipy.sparse.csr_matrix产生 ...

  4. sklearn.feature_extraction.DictVectorizer

    sklearn.feature_extraction.DictVectorizer:将字典组成的列表转换成向量.(将特征与值的映射字典组成的列表转换成向量) 1. 特征矩阵行代表数据,列代表特征,0表 ...

  5. sklearn特征抽取

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

  6. sklearn文本特征提取

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

  7. Feature extraction - sklearn文本特征提取

    http://blog.csdn.net/pipisorry/article/details/41957763 文本特征提取 词袋(Bag of Words)表征 文本分析是机器学习算法的主要应用领域 ...

  8. sklearn中模型抽取

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

  9. Sklearn 与 TensorFlow 机器学习实战—一个完整的机器学习项目

    本章中,你会假装作为被一家地产公司刚刚雇佣的数据科学家,完整地学习一个案例项目.下面是主要步骤: 项目概述. 获取数据. 发现并可视化数据,发现规律. 为机器学习算法准备数据. 选择模型,进行训练. ...

随机推荐

  1. P4555 【[国家集训队]最长双回文串】

    不知道有没有人跟我一样数据结构学傻了 首先这道题是要求回文串,那么我们可以想到manacher算法 但由于\(manacher\)不能求出双回文子串,我们要考虑一些性质 首先对于一个回文串,删掉两边的 ...

  2. php单点登录SSO(Single Sign On)的解决思路

    一.什么是单点登录 解释:登录一个系统后,其它系统无需再次登录,即可进入. 二.举个例子: 你登录了淘宝,然后你进入天猫,发现你不用登录了.这时你要注意到,淘宝跟天猫可是完全不一样的域名. 你登录淘宝 ...

  3. OSPF基本详解

  4. Java NIO?看这一篇就够了!

    现在使用NIO的场景越来越多,很多网上的技术框架或多或少的使用NIO技术,譬如Tomcat,Jetty.学习和掌握NIO技术已经不是一个JAVA攻城狮的加分技能,而是一个必备技能.在前面2篇文章< ...

  5. 4、vueJs基础知识04

    简单的目录结构: |-index.html |-main.js 入口文件 |-App.vue vue文件(组件),官方推荐命名法(首字母大写) |-components      组件存放的文件夹 | ...

  6. 使用IDEA创建一个Maven Web工程:无法创建Java Class文件

    今天用IDEA新建了一个maven web工程,项目目录是这样的: 在新创建一个Java class 文件时,却没有Java class功能,无法创建,如图: 解决方案: 选择 File——>P ...

  7. 五笔字典86版wubi拆字图编码查询

    五笔字典86版 软件能查询以下数据,五笔编码,汉字拆字图,拼音,部首,笔划,笔顺,解释,五笔口诀等等.这些数据只针对单个汉字查询(大概7000字左右).词组查询只支持五笔编码查询(有60000个词组+ ...

  8. PKUWC2020 游记

    因为CSP-S挂的并不厉害,蜜汁来到了PKU,所以有了这篇游记. DAY 0 上午在机房颓废,中途还整了一个出校证. 九点多,两个THU的大神去拿笔记本和手机颓废了,不久被两个教练拉着和kx跑了出去. ...

  9. HTML中各标签对应的英文意思

    HTML中各标签对应的英文意思 一.总结 一句话总结: 结合标签的英语全称,可以更加方便的知道标签的意思 二.HTML中标签对应的英文(方便理解记忆)(转自) 转自:HTML中标签对应的英文(方便理解 ...

  10. 巧用 CSS 实现酷炫的充电动画

    循序渐进,看看只使用 CSS ,可以鼓捣出什么样的充电动画效果. 画个电池 当然,电池充电,首先得用 CSS 画一个电池,这个不难,随便整一个: 欧了,勉强就是它了.有了电池,那接下来直接充电吧.最最 ...