【问题标题】:Meaning of TfidfVectorizer toarray() and HashingVectorizerTfidfVectorizer toarray() 和 HashingVectorizer 的含义
【发布时间】: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


    【解决方案1】:
    • 在第一种情况下,您会看到重复的数字,因为您的“语料库”中有多个具有相同 IDF(逆文档频率)的单词。例如,单词 dogfox 在您的文本中具有完全相同的出现模式,因此它们具有相同的 IDF;这两个由 1.28768207 值表示。 the 这个词出现在每个文本中,所以它用 1 表示。词汇表中的其余词在第一个文本中出现一次,而不在其他两个文本中出现,所以它们都完全相同以色列国防军。你可以用vectorizer.get_feature_names()查看哪个特征对应哪个词。
    • 使用 HashingVectorizer,您选择的特征数为 20,但文本中唯一单词的总数少于 20,因此您将有很多特征为 0。您得到的非特征数少于 8 - 零元素,因为存在一些哈希冲突 - 那是因为 20 的特征太少而无法避免冲突(考虑默认值为 2^20)。如果您选择更高的n_features,您将获得正好 8 个非零元素。您有重复的值,因为几乎所有特征在该文本中都有相同的频率。
    • 对于标题中的问题,toarray() method 将 sklearn 使用的稀疏矩阵的有效表示转换为普通可读的密集 ndarray 表示。

    【讨论】:

      【解决方案2】:

      TfidfVectorizer()

      将原始文档集合转换为 TF-IDF 特征矩阵。 你在跑步

      vectorizer.fit(文本)

      仅 我建议你跑

      vectorizer.fit_transform(文本)

      然后它对您的文本进行标记,为您的文本创建了一个特征。因为您的文本有 8 个特征({'the': 7, 'quick': 6, 'brown': 0, 'fox': 2, 'jumped': 3, 'over': 5, “懒惰”:4,“狗”:1} 它返回了与它们对应的 8 个频率。你也可以通过运行来交叉验证它

      打印(vectorizer.get_feature_names())

      这会给你 ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the']

      打印(vectorizer.fit_transform(text).shape)

      会给你

      (3, 8)

      【讨论】:

        猜你喜欢
        • 2014-11-23
        • 2014-10-09
        • 1970-01-01
        • 2015-04-08
        • 2018-07-11
        • 2019-10-10
        • 2016-04-08
        • 2021-01-11
        • 1970-01-01
        相关资源
        最近更新 更多