【发布时间】:2013-11-29 05:13:10
【问题描述】:
我正在使用 CountVectorizer 中的 fit_transform 方法,并且正在阅读代码以尝试了解它在做什么。我对 CountVectorizer 中的 _count_vocab 方法有点困惑,特别是在嵌套的 for 循环下。对于原始文档,我有一个句子列表,并且 fixed_vocab = False。
def _count_vocab(self, raw_documents, fixed_vocab):
"""Create sparse feature matrix, and vocabulary where fixed_vocab=False"""
if fixed_vocab:
vocabulary = self.vocabulary_
else:
# Add a new value when a new vocabulary item is seen
vocabulary = defaultdict(None)
vocabulary.default_factory = vocabulary.__len__
analyze = self.build_analyzer()
j_indices = _make_int_array()
indptr = _make_int_array()
indptr.append(0)
for doc in raw_documents:
for feature in analyze(doc):
try:
j_indices.append(vocabulary[feature])
except KeyError:
# Ignore out-of-vocabulary items for fixed_vocab=True
continue
indptr.append(len(j_indices))
if not fixed_vocab:
# disable defaultdict behaviour
vocabulary = dict(vocabulary)
if not vocabulary:
raise ValueError("empty vocabulary; perhaps the documents only"
" contain stop words")
# some Python/Scipy versions won't accept an array.array:
if j_indices:
j_indices = np.frombuffer(j_indices, dtype=np.intc)
else:
j_indices = np.array([], dtype=np.int32)
indptr = np.frombuffer(indptr, dtype=np.intc)
values = np.ones(len(j_indices))
X = sp.csr_matrix((values, j_indices, indptr),
shape=(len(indptr) - 1, len(vocabulary)),
dtype=self.dtype)
X.sum_duplicates()
return vocabulary, X
这里的词汇表是一个空的 defaultdict 对象。因此 j_indices 不会附加元素,因为词汇表是空的,所以词汇表[feature] 返回一个错误并且错误被忽略,继续下一个 for 循环迭代。它将继续对 raw_documents 中的所有 doc 以及由 analyze(doc) 返回的标记中的所有功能执行此操作。在这个 j_indices 和 indptr 的末尾是空的 array.array 对象。
我以为 _count_vocab 会在遇到新的词汇时创建自己的词汇对象并附加值,但它看起来不像。
在这种情况下,我应该提供我自己的词汇表吗?既然我没有,我在哪里可以得到一本单词词典?
感谢您的帮助。
【问题讨论】:
标签: python scikit-learn feature-extraction