gpt4 book ai didi

machine-learning - CountVectorizer 如何处理测试数据中的新词?

转载 作者:行者123 更新时间:2023-12-05 03:44:50 26 4
gpt4 key购买 nike

我了解 CountVectorizer 的一般工作原理。它获取单词标记并创建文档(行)和标记计数(列)的稀疏计数矩阵,我们可以将其用于 ML 建模。

但是,它如何处理可能出现在测试数据中但未出现在训练数据中的新词?它会忽略它们吗?

此外,从建模的角度来看,是否应该假设某些词非常罕见以至于它们根本没有出现在训练数据中,并且它们与您可能执行的任何建模都不相关?

最佳答案

我假设您指的是 scikit-learn CountVectorizer。不是我自己知道是否还有其他人。

是的,当对新文档进行编码时,不属于词汇表(从训练数据创建)的词会被计数向量器忽略。

创建词汇表的例子:(https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html)

>>> from sklearn.feature_extraction.text import CountVectorizer
>>> corpus = [
... 'This is the first document.',
... 'This document is the second document.',
... 'And this is the third one.',
... 'Is this the first document?',
... ]
>>> vectorizer = CountVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> print(vectorizer.get_feature_names())
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
>>> print(X.toarray())
[[0 1 1 1 0 0 1 0 1]
[0 2 0 1 0 1 1 0 1]
[1 0 0 1 1 0 1 1 1]
[0 1 1 1 0 0 1 0 1]]

现在,对新文档使用转换,您可以看到超出词汇表的单词被忽略了:

>>> print(vectorizer.transform(['not in any of the document second second']).toarray())
[[0 1 0 0 0 2 1 0 0]]

关于不属于训练数据的稀有词,我同意你的说法,即它对建模并不重要,因为我们希望相信与创建和概括良好的词最相关的词模型已经是训练数据的一部分。

关于machine-learning - CountVectorizer 如何处理测试数据中的新词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66215827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com