寫在前面:HashVectorizer與tfidf類似,都是講文本向量化的表示方法,但它節省內存,也更快。當數據集較大時,可以作爲tfidf的替代。
from:https://www.cnblogs.com/pinard/p/6688348.html
使用說明
在使用時,我以from sklearn.feature_extraction.text.HashingVectorizer爲例:
1、默認n_features是1048576.也就是2的20次方。所以如果你原來詞典長度沒有這麼大,你想當與在增加維度。你可以手動修改參數
from sklearn.feature_extraction.text import HashingVectorizer
vectorizer = HashingVectorizer()
print(vectorizer.transform(['a very small document']).shape)
(1, 1048576)
small_vectorizer = HashingVectorizer(n_features=5)
print(small_vectorizer.transform(['a very small document']).shape)
(1, 5)
來自:
https://datascience.stackexchange.com/questions/22250/what-is-the-difference-between-a-hashing-vectorizer-and-a-tfidf-vectorizer
2.至於hash trick是怎麼個hash法:
同樣來自第一個鏈接。