【发布时间】:2019-02-07 18:25:06
【问题描述】:
我正在尝试理解 python 中的矢量化器.. 我正在使用这个示例代码:
from sklearn.feature_extraction.text import TfidfVectorizer
# list of text documents
text = ["The quick brown fox jumped over the lazy dog.", "The dog.", "The fox"]
print(text)
# create the transform
vectorizer = TfidfVectorizer()
# tokenize and build vocab
vectorizer.fit(text)
# summarize
print(vectorizer.idf_)
# encode document
vector = vectorizer.transform([text[0]])
# summarize encoded vector
print(vector.shape)
print(vector.toarray())
print(vectorizer.vocabulary_)
输出是这样的:
['The quick brown fox jumped over the lazy dog.', 'The dog.', 'The fox']
[1.69314718 1.28768207 1.28768207 1.69314718 1.69314718 1.69314718
1.69314718 1. ]
(1, 8)
[[0.36388646 0.27674503 0.27674503 0.36388646 0.36388646 0.36388646
0.36388646 0.42983441]]
{'the': 7, 'quick': 6, 'brown': 0, 'fox': 2, 'jumped': 3, 'over': 5,
'lazy': 4, 'dog': 1}
我不明白为什么 vector.toarray() 会为不同的单词产生重复的数字..例如有 0.36388646 四次..和 0.27674503 两次..这个数字是什么? 神经网络用来训练自己的数字是用vectorizer.vocabulary_打印的数字?
使用散列矢量化器,我有以下代码:
from sklearn.feature_extraction.text import HashingVectorizer
# list of text documents
text = ["The quick brown fox jumped over the lazy dog."]
# create the transform
vectorizer = HashingVectorizer(n_features=20)
# encode document
vector = vectorizer.fit_transform(text)
# summarize encoded vector
print(vector.shape)
print(vector.toarray())
这就是输出:
(1, 20)
[[ 0. 0. 0. 0. 0. 0.33333333
0. -0.33333333 0.33333333 0. 0. 0.33333333
0. 0. 0. -0.33333333 0. 0.
-0.66666667 0. ]]
是否使用了 0. 值?什么礼物?为什么即使在那里打印重复值? (0.3333333 和 -0.33333333)
【问题讨论】:
标签: python machine-learning scikit-learn