我正在使用来自sklearn.feature_extraction.text的HashingVectorizer函数,但我不明白它是如何工作的。
我的代码
from sklearn.feature_extraction.text import HashingVectorizer
corpus = [ 'This is the first document.',
'This document is the second document.',
'And this is the third one.',
'Is this the first document?']
vectorizer = HashingVectorizer(n_features=2**3)
X = vectorizer.fit_transform(corpus)
print(X)我的结果
(0, 0) -0.8944271909999159
(0, 5) 0.4472135954999579
(0, 6) 0.0
(1, 0) -0.8164965809277261
(1, 3) 0.4082482904638631
(1, 5) 0.4082482904638631
(1, 6) 0.0
(2, 4) -0.7071067811865475
(2, 5) 0.7071067811865475
(2, 6) 0.0
(3, 0) -0.8944271909999159
(3, 5) 0.4472135954999579
(3, 6) 0.0我读了很多关于散列技巧的论文,比如本文https://medium.com/value-stream-design/introducing-one-of-the-best-hacks-in-machine-learning-the-hashing-trick-bf6a9c8af18f。
我理解这篇文章,但没有看到与上述结果之间的关系。
,你能用简单的例子解释一下HashingVectorizer的工作方式吗?
发布于 2020-04-08 06:14:43
我认为结果是没有意义的,因为负值和默认的正常化。
如果你这样做:
vectorizer = HashingVectorizer(n_features=2**3,norm=None,alternate_sign=False)您应该看到原始计数和结果应该开始有意义。如果您想要归一化术语频率,那么设置norm='l2'。
您正在打印的结果实质上是(document_id,position_in_matrix) counts。
有关更多信息,请参见这篇关于HashingVectorizer对CountVectorizer的文章。
发布于 2020-03-27 14:57:03
结果是矩阵(大小为4x8)的稀疏表示。
print(X.toarray())输出:
[[-0.89442719 0. 0. 0. 0. 0.4472136
0. 0. ]
[-0.81649658 0. 0. 0.40824829 0. 0.40824829
0. 0. ]
[ 0. 0. 0. 0. -0.70710678 0.70710678
0. 0. ]
[-0.89442719 0. 0. 0. 0. 0.4472136
0. 0. ]]为了得到一个令牌的向量,我们计算它的散列并得到矩阵中的列索引。列是令牌的向量。
https://stackoverflow.com/questions/56275693
复制相似问题