python - 带有 char_wb 的 tf-idf 会忽略自定义预处理器吗?

标签 python scikit-learn tfidfvectorizer

我有

import nltk
from nltk.stem.snowball import GermanStemmer

def my_tokenizer(doc):
   stemmer= GermanStemmer()

   return([stemmer.stem(t.lower()) for t in nltk.word_tokenize(doc) if 
   t.lower() not in my_stop_words])

text="hallo df sdfd"
singleTFIDF = TfidfVectorizer(analyzer='char_wb', ngram_range= 
(4,6),preprocessor=my_tokenizer, max_features=50).fit([str(text)])

从文档中可以清楚地看出,自定义 toenizer 仅适用于analyzer=word。

我明白了

Traceback (most recent call last):
  File "TfidF.py", line 95, in <module>
    singleTFIDF = TfidfVectorizer(analyzer='char_wb', ngram_range=(4,6),preprocessor=my_tokenizer, max_features=50).fit([str(text)])
  File "C:\Users\chris1\Anaconda3\envs\master\lib\site-packages\sklearn\feature_extraction\text.py", line 185, in _char_wb_ngrams
    text_document = self._white_spaces.sub(" ", text_document)
TypeError: expected string or bytes-like object

最佳答案

您必须连接单词,然后返回一个字符串。 试试这个!

return(' '.join ([stemmer.stem(t.lower()) for t in nltk.word_tokenize(doc) if 
   t.lower() not in my_stop_words]))

关于python - 带有 char_wb 的 tf-idf 会忽略自定义预处理器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54310818/

相关文章:

python - Boto 在 ec2 实例上执行 shell 命令

python - 如何正确绘制训练集和验证集的损失曲线?

python - SVM 中的随机答案

python - scikit-learn TfidfVectorizer 忽略某些单词

python - 如何在 Django 中进行 ajax 分页?

python - django app_details() 收到意外的关键字参数 'slug' 错误

python - 在python opencv中查找图像的所有X和Y坐标

python - 计算欧氏距离时sklearn.metrics.pairwise_distances_argmin_min的奇怪结果

python - TF-IDF的纯pandas实现

python - 如何获得 tf-idf 分类器的最佳特征?