我在试着理解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()为不同的word..for示例产生重复的数字--有0.36388646,4,times..and,0.27674503,2,times..what --会出现这个数字吗?神经网络用来自我训练的数字是用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)
发布于 2019-02-07 08:58:35
vectorizer.get_feature_names()对应的哪个单词。n_features,您将得到确切的8个非零元素。您有重复的值,因为,再次,几乎所有的特征都有相同的频率在该文本。发布于 2019-02-07 09:32:58
TfidfVectorizer()
将原始文档集合转换为TF-以色列国防军功能矩阵。你在跑
Vectorizer.fit(文本)
只是我建议你跑
Vectorizer.fit_transform(文本)
然后它标记您的文本,为您的文本创建一个功能。因为你的文本有8个功能({‘s’:7,'lazy':6,'brown':0,'fox':2,‘跃’:3,'over':5,‘懒惰’:4,‘狗’:1}返回了与它们对应的8个频率。您还可以通过运行
打印(vectorizer.get_feature_names())
这会给你‘布朗’,‘狗’,‘狐狸’,‘跳跃’,‘懒惰’,‘结束’,‘快速’,‘’‘
打印(vectorizer.fit_transform(文本).shape)
会给你
(3、8)
https://stackoverflow.com/questions/54562820
复制相似问题