【问题标题】:Tf-idf vectorizer has whitespaces in feature words with char_wb?Tf-idf 矢量化器在带有 char_wb 的特征词中有空格?
【发布时间】:2019-06-15 23:03:08
【问题描述】:

我用

singleTFIDF = TfidfVectorizer(analyzer='char_wb', ngram_range= 
(4,6),stop_words=my_stop_words, max_features=50).fit([text])

并想知道为什么我的功能中有空格,例如“chaft”

我怎样才能避免这种情况?我需要自己进行 tekenize 和预处理吗?

【问题讨论】:

    标签: python scikit-learn tfidfvectorizer


    【解决方案1】:

    使用analyzer='word'。

    当我们使用char_wb 时,矢量化器会填充空白,因为它不会对使用character_n_grams 进行检查的单词进行标记。

    根据Documentation:

    分析器:字符串、{'word'、'char'、'char_wb'} 或可调用的

    特征是否应该由单词或字符 n-gram 组成。选项‘char_wb’ 仅从单词边界内的文本创建字符 n-gram; 单词边缘的 n-gram 用空格填充。

    看下面的例子,了解

    的用法
    from sklearn.feature_extraction.text import TfidfVectorizer
    corpus = [
        'This is the first document.',
        'This document is the second document.',
        'And this is the third one.',
        'Is this the first document?',
    ]
    vectorizer = TfidfVectorizer(analyzer='char_wb', ngram_range= (4,6))
    X = vectorizer.fit_transform(corpus)
    print([(len(w),w) for w in vectorizer.get_feature_names()])
    

    输出:

    [(4, 'and'), (5, 'and'), (4, 'doc'), (5, 'docu'), (6, 'docum'), (4, '冷杉'), (5, '冷杉'), (6, '第一'), (4, '是'), (4, '一'), (5, '一.'), (6, '一.'), (4, 'sec'), (5, 'seco'), (6, 'secon'), (4, 'the'), (5, 'the'), (4, 'thi'), (5, 'thir'), (6, '第三'), (5, 'this'), (6, 'this'), (4, 'and'), (4, 'cond'), (5, 'cond'), (4, 'cume'), (5, 'cumen'), (6, 'cument'), (4, 'docu'), (5, 'docum'), (6, 'docume'), (4, 'econ'), (5, 'econd'), (6, 'econd'), (4, 'ent'), (4, 'ent.'), (5, 'ent.'), (4, 'ent?'), (5, 'ent?'), (4, 'firs'), (5, 'first'), (6, 'first'), (4, 'hird'), (5, 'hird'), (4, 'his'), (4, 'ird'), (4, 'irst'), (5, 'irst'), (4, 'ment'), (5, 'ment'), (5, 'ment.'), (6, 'ment.'), (5, 'ment?'), (6, 'ment?'), (4, 'ne.'), (4, 'nt。 '), (4, 'nt?'), (4, 'ocum'), (5, 'ocume'), (6, 'ocumen'), (4, 'ond'), (4, 'one.'), (5, 'one.'), (4, 'rst'), (4, 'seco'), (5, 'secon'), (6, 'second'), (4, 'the'), (4, 'thir'), (5, 'third'), (6, '第三'), (4, 'this'), (5, 'this'), (4, 'umen'), (5, 'ument'), (6, 'ument'), (6, 'ument.'), (6, 'ument?')]

    【讨论】:

    • Isee。但我想使用 ngrams。我也不知道分析器 = char 的预处理(如标记化)的默认值如何?我也得到标点符号。我可以对analyzer=char_wb 使用自定义预处理吗?
    • 我明白了。模型从 char 编码中学习不是很难吗?你能在你的问题中添加一些使用的上下文吗?我们仍然可以使用带有单词标记的 ngram。当然我们可以自定义预处理器,然后使用 char_wb 进行分词
    • 如果我们删除空格,它会丢失单词结尾。好的方法可能是删除标点符号。
    • 谢谢。我觉得使用 ngrams 你需要对你的韩元进行大部分预处理,而使用“word”大多数事情都是默认完成的。之后预处理和使用 char_wb 的正确方法是什么?我应该使用自定义标记器还是在自定义预处理中执行此操作?我想做词干提取,需要自己删除停用词,因为 char_wb 默认情况下不会删除它们!
    • 好的。但我认为你可以在用空格删除那些“重复”时增加结果?此外,使用 ngrams(4,6) 可以获得很多类似的标记,例如:['inl'、'inla'、'inlan'、'vor'、'aft'、'anwa'、'eis'、'haft' ,'haft','inla','inlan','inland','land','nlan','nland','nwar','reis','reis','rtsc','schaf']
    猜你喜欢
    • 2020-08-25
    • 2022-12-05
    • 2023-03-17
    • 2023-03-19
    • 2021-02-10
    • 2020-09-27
    • 2020-01-05
    • 1970-01-01
    • 2020-01-18
    相关资源
    最近更新 更多