【问题标题】:Filtering tokens by frequency using filter_extremes in Gensim使用 Gensim 中的 filter_extremes 按频率过滤令牌
【发布时间】:2018-08-01 13:31:40
【问题描述】:

我正在尝试使用 Gensim (https://radimrehurek.com/gensim/corpora/dictionary.html) 中的 filter_extremes 函数按频率过滤掉令牌。具体来说,我有兴趣过滤掉出现在“比 no_below 文档少”和“比 no_above 文档更频繁”中出现的单词。

id2word_ = corpora.Dictionary(texts)
print(len(id2word_))
id2word_.filter_extremes(no_above = 0.600)
print(len(id2word_))

第一个打印语句给出 11918,第二个打印语句给出 3567。但是,如果我执行以下操作:

id2word_ = corpora.Dictionary(texts)
print(len(id2word_))
id2word_.filter_extremes(no_below = 0.599)
print(len(id2word_))

第一个打印语句给出 11918(如预期的那样),第二个给出 11406。id2word_.filter_extremes(no_below = 0.599)id2word_.filter_extremes(no_above = 0.600) 不应该加起来总字数吗?但是,11406 + 3567 > 11918,那么这个和怎么会超过语料库的单词数呢?这是没有意义的,因为根据文档中的解释,过滤器应该涵盖不重叠的单词。

如果您有任何想法,我将非常感谢您的意见!谢谢!

【问题讨论】:

    标签: python dictionary text-processing gensim corpus


    【解决方案1】:

    根据定义:

    no_below (int, optional) – Keep tokens which are contained in at least no_below 
    documents.
    
    no_above (float, optional) – Keep tokens which are contained in no more than 
    no_above documents (fraction of total corpus size, not an absolute number).
    

    no_below 是一个 int 值,它表示一个阈值,用于过滤掉文档中高于特定数量的标记的出现次数。例如使用 no_below 过滤掉出现次数少于 10 次的单词。

    相反,no_above 不是一个 int 而是一个浮点数,表示总语料库大小的派别。例如使用 no_above 过滤掉出现在所有文档中超过 10% 的单词。

    有点奇怪,no_below 和 no_above 不代表同一个单位,因此会造成混淆。

    希望这能回答你的问题。

    【讨论】:

    • 是否有任何特定的方式或逻辑来设置 no_below 和 no_above 的最佳值
    【解决方案2】:

    关于 Gensim 中的 filter_extremes,“no_above”和“no_below”参数的单位实际上是不同的。老实说,这有点奇怪。

    对于“no_above”,您想在此处放置一个介于 0 和 1 之间的数字(浮点数)。它应该是一个百分比,代表一个词在整个语料库大小中所占的比例。

    对于“no_below”,您需要一个整数。它应该是一个单词在语料库中出现的次数。这是一个门槛。

    希望它能澄清你的问题。

    【讨论】:

      【解决方案3】:

      我写这篇文章是为了扩展其他用户的答案。是的,这两个参数不同,控制不同种类的令牌频率。另外注意以下错误:filter_extremes assignments default values to no_above 和 no_below,所以如果你写:

      id2word_.filter_extremes(no_below = 0.599)
      

      确实有效

      id2word_.filter_extremes(no_below = 0.599, no_above=0.5)
      

      【讨论】:

        【解决方案4】:

        希望您已经找到了您的回复的答案。我一直在涉足 gensim 库,发现这两个参数 'no_below' 和 'no_above' 可能最好一起使用。 TS 所说的 no_below 很简单,如果频率不小于参数,则返回令牌。

        一些代码示例,我不确定是否有最佳实践,但我通常从 1(或语料库的 100%)开始修改 no_above,这使得过滤排除整个语料库.

        # filter if frequency not less than 2
        # and tokens contained not more than 90% of corpus
        dictionary.filter_extremes(no_below=2, no_above=0.9) 
        print(len(dictionary))
        
        # filter if frequency not less than 3 
        # and tokens contained not more than 100% of corpus
        dictionary.filter_extremes(no_below=3, no_above=1) 
        print(len(dictionary))
        

        一般来说,从 no_above 语料库 100% 开始,然后再进行调整。

        【讨论】:

        • 另一个想法是使用 save() 函数将字典结果保存为 .txt。它将以良好的干净格式加载为数据框/表。这可能是操纵和呈现结果的另一种方式。
        • *上面的代码 sn-p。 dictionary.save_as_text("dict_text.txt") data = pd.read_csv('dict_text.txt', sep="\t", header=None) data.columns = ["token_id", "token_name", "token_frequency"]数据头()
        猜你喜欢
        • 2021-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多