我正在查看sklearn的TfidfVectorizer,特别是在preprocessor
输入参数中,它具有以下文档:
“在保留标记化和n-gram生成步骤的同时,覆盖预处理(字符串转换)阶段。”
我正试图弄清楚当我不覆盖它时,预处理阶段会做什么(如果有的话)?
我有一个实验,正在使用以下代码查看所得稀疏矩阵中存储的元素数:
vectorizer = TfidfVectorizer(stop_words=words, preprocessor=process, ngram_range=(1,1), strip_accents='unicode')
vect = vectorizer.fit_transform(twenty_train.data)
items_stored = vect.nnz
s = re.sub("[^a-zA-Z]", " ", s)
的方法重写预处理器时,结果矩阵将存储1331597个元素。很显然,它与默认的sklearn结果有所不同,没有进行预处理,而我尝试复制预处理步骤。我努力寻找有关默认情况下预处理器具体功能的文档。
我还检查了TfidfVectorizer
中的source code-但是我也无法从这里弄清楚预处理器在做什么。
有人碰巧知道sklearn的默认预处理器执行了什么代码或采取了哪些预处理步骤?
答案 0 :(得分:0)
您在寻找这个吗?
def build_preprocessor(self):
"""Return a function to preprocess the text before tokenization"""
if self.preprocessor is not None:
return self.preprocessor
# unfortunately python functools package does not have an efficient
# `compose` function that would have allowed us to chain a dynamic
# number of functions. However the cost of a lambda call is a few
# hundreds of nanoseconds which is negligible when compared to the
# cost of tokenizing a string of 1000 chars for instance.
noop = lambda x: x
# accent stripping
if not self.strip_accents:
strip_accents = noop
elif callable(self.strip_accents):
strip_accents = self.strip_accents
elif self.strip_accents == 'ascii':
strip_accents = strip_accents_ascii
elif self.strip_accents == 'unicode':
strip_accents = strip_accents_unicode
else:
raise ValueError('Invalid value for "strip_accents": %s' %
self.strip_accents)
if self.lowercase:
return lambda x: strip_accents(x.lower())
else:
return strip_accents
从这里:https://github.com/scikit-learn/scikit-learn/blob/bac89c2/sklearn/feature_extraction/text.py#L230