如何将预处理器传递给 TfidfVectorizer? 我做了一个函数,它接受一个字符串并返回一个预处理的字符串 然后我将处理器参数设置为该函数“preprocessor=preprocess”,但它不起作用。 找了很多次,没找到例子,好像没人用过。
我还有一个问题。 它(预处理器参数)是否覆盖了可以使用停止词和小写参数完成的删除停用词和小写字母的操作?
最佳答案
您只需定义一个函数,该函数将字符串作为输入并返回要预处理的内容。因此,例如,大写字符串的简单函数如下所示:
def preProcess(s):
return s.upper()
创建函数后,只需将其传递给 TfidfVectorizer
对象即可。例如:
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [
'This is the first document.',
'This is the second second document.',
'And the third one.',
'Is this the first document?'
]
X = TfidfVectorizer(preprocessor=preProcess)
X.fit(corpus)
X.get_feature_names()
结果:
[u'AND', u'DOCUMENT', u'FIRST', u'IS', u'ONE', u'SECOND', u'THE', u'THIRD', u'THIS']
这间接回答了您的后续问题,因为尽管小写被设置为 true,但大写的预处理函数会覆盖它。文档中也提到了这一点:
preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps.
关于python - 如何将预处理器传递给 TfidfVectorizer? - sklearn - python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23850256/