【发布时间】:2012-01-30 04:14:46
【问题描述】:
我知道特征散列(散列技巧)用于降低维数和处理位向量的稀疏性,但我不明白它是如何工作的。谁能给我解释一下。是否有任何 python 库可用于进行特征散列?
谢谢。
【问题讨论】:
-
您在寻找这样的东西吗? shogun-toolbox.org
标签: python hash vector machine-learning
我知道特征散列(散列技巧)用于降低维数和处理位向量的稀疏性,但我不明白它是如何工作的。谁能给我解释一下。是否有任何 python 库可用于进行特征散列?
谢谢。
【问题讨论】:
标签: python hash vector machine-learning
在 Pandas 上,您可以使用如下内容:
import pandas as pd
import numpy as np
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
data = pd.DataFrame(data)
def hash_col(df, col, N):
cols = [col + "_" + str(i) for i in range(N)]
def xform(x): tmp = [0 for i in range(N)]; tmp[hash(x) % N] = 1; return pd.Series(tmp,index=cols)
df[cols] = df[col].apply(xform)
return df.drop(col,axis=1)
print hash_col(data, 'state',4)
输出将是
pop year state_0 state_1 state_2 state_3
0 1.5 2000 0 1 0 0
1 1.7 2001 0 1 0 0
2 3.6 2002 0 1 0 0
3 2.4 2001 0 0 0 1
4 2.9 2002 0 0 0 1
同样在系列级别,您可以
将 numpy 导入为 np, os 将 sys、pandas 导入为 pd
def hash_col(df, col, N):
df = df.replace('',np.nan)
cols = [col + "_" + str(i) for i in range(N)]
tmp = [0 for i in range(N)]
tmp[hash(df.ix[col]) % N] = 1
res = df.append(pd.Series(tmp,index=cols))
return res.drop(col)
a = pd.Series(['new york',30,''],index=['city','age','test'])
b = pd.Series(['boston',30,''],index=['city','age','test'])
print hash_col(a,'city',10)
print hash_col(b,'city',10)
这将适用于单个系列,列名将被假定为 Pandas 索引。它还用 nan 替换空白字符串,并浮动所有内容。
age 30
test NaN
city_0 0
city_1 0
city_2 0
city_3 0
city_4 0
city_5 0
city_6 0
city_7 1
city_8 0
city_9 0
dtype: object
age 30
test NaN
city_0 0
city_1 0
city_2 0
city_3 0
city_4 0
city_5 1
city_6 0
city_7 0
city_8 0
city_9 0
dtype: object
但是,如果有一个词汇表,而您只是想进行一次热编码,则可以使用
import numpy as np
import pandas as pd, os
import scipy.sparse as sps
def hash_col(df, col, vocab):
cols = [col + "=" + str(v) for v in vocab]
def xform(x): tmp = [0 for i in range(len(vocab))]; tmp[vocab.index(x)] = 1; return pd.Series(tmp,index=cols)
df[cols] = df[col].apply(xform)
return df.drop(col,axis=1)
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
df = pd.DataFrame(data)
df2 = hash_col(df, 'state', ['Ohio','Nevada'])
print sps.csr_matrix(df2)
这将给
pop year state=Ohio state=Nevada
0 1.5 2000 1 0
1 1.7 2001 1 0
2 3.6 2002 1 0
3 2.4 2001 0 1
4 2.9 2002 0 1
我还添加了最终数据帧的稀疏化。在增量设置中,我们可能没有事先遇到所有值(但我们以某种方式获得了所有可能值的列表),可以使用上述方法。增量 ML 方法在每次增量时需要相同数量的特征,因此 one-hot 编码必须在每个批次中产生相同数量的行。
【讨论】:
hash 可能使用随机种子(在较新的 Python 3.x 中默认称为 python -R)。脚本运行的结果可能不同。请参阅我的答案以获得更强大的实现。
Here(抱歉,由于某种原因,我无法将其添加为评论。)另外,Feature Hashing for Large Scale Multitask Learning 的第一页很好地解释了它。
【讨论】:
大稀疏特征可以衍生自交互,U 为用户,X 为电子邮件,因此 U x X 的维度是内存密集型的。通常,垃圾邮件过滤等任务也有时间限制。
哈希技巧像其他哈希函数一样存储二进制位(索引),这使得大规模训练变得可行。理论上,散列长度越大,性能提升越大,如原始论文中所述。
它将原始特征分配到不同的桶中(特征空间的有限长度),以便保留它们的语义。即使垃圾邮件发送者使用错字错过雷达。尽管存在失真错误,但其哈希形式仍然接近。
例如,
“快速棕色狐狸”转换为:
h(the) mod 5 = 0
h(quick) mod 5 = 1
h(brown) mod 5 = 1
h(fox) mod 5 = 3
使用索引而不是文本值,节省空间。
总结一些应用:
高维特征向量的降维
稀疏化
动态词袋
跨产品特性
多任务学习
参考:
原稿:
特征散列用于大规模多任务学习
Shi, Q.、Petterson, J.、Dror, G.、Langford, J.、Smola, A.、Strehl, A. 和 Vishwanathan, V. (2009)。 哈希内核
Gionis, A.、Indyk, P. 和 Motwani, R. (1999)。通过散列进行高维相似性搜索
实施:
【讨论】: