【问题标题】:R How to use maxCount scheme in Quanteda packageR如何在Quanteda包中使用maxCount方案
【发布时间】:2017-02-23 08:40:21
【问题描述】:

我的问题很简单,R 中的 Quanteda 包具有计算文档频率矩阵 (dfm) 的词频 (tf) 的功能。当你用 ?tf 查看 tf 函数的描述时,它说它有四个参数。我的问题是关于“方案”的论点。我不明白如何使用 maxCount 选项,即使用每个文档的最大特征数作为 tf.当您查看“用法”时,方案参数的唯一选项是“count”、“prop”、“propmax”、“boolean”、“log”、“augmented”和“logave”,那么,我该如何使用maxCount 选项?

【问题讨论】:

    标签: r tf-idf quanteda


    【解决方案1】:

    简短的回答是这是文档中的一个“错误”(对于 quanteda 0.9.8.0-0.9.8.2),因为该选项已从函数中删除,但未从文档中删除。新语法是 propMax 参数,例如:

    txt <- c(doc1 = "This is a simple, simple, simple document.",
             doc2 = "This document is a second document.")
    (myDfm <- dfm(txt, verbose = FALSE))
    ## Document-feature matrix of: 2 documents, 6 features.
    ## 2 x 6 sparse Matrix of class "dfmSparse"
    ##       features
    ## docs   this is a simple document second
    ##   doc1    1  1 1      3        1      0
    ##   doc2    1  1 1      0        2      1
    

    应用权重:

    tf(myDfm, scheme = "prop")
    ## Document-feature matrix of: 2 documents, 6 features.
    ## 2 x 6 sparse Matrix of class "dfmSparse"
    ##       features
    ## docs        this        is         a    simple  document    second
    ##   doc1 0.1428571 0.1428571 0.1428571 0.4285714 0.1428571 0        
    ##   doc2 0.1666667 0.1666667 0.1666667 0         0.3333333 0.1666667
    

    propmax 应该计算每个计数相对于文档中最频繁计数的比例。例如,对于 doc1,最大特征数为 3,因此该文档中的每个术语都将除以 3。但是在 quanteda 错误 导致它错误地计算:

    tf(myDfm, scheme = "propmax")
    ## Document-feature matrix of: 2 documents, 6 features.
    ## 2 x 6 sparse Matrix of class "dfmSparse"
    ##       features
    ## docs        this        is         a simple  document    second
    ##   doc1 1.0000000 1.0000000 1.0000000      3 1.0000000 0        
    ##   doc2 0.3333333 0.3333333 0.3333333      0 0.6666667 0.3333333
    

    在 quanteda v0.9.8.3 中,这是固定的:

    tf(myDfm, scheme = "propmax")
    ## Document-feature matrix of: 2 documents, 6 features.
    ## 2 x 6 sparse Matrix of class "dfmSparse"
    ##       features
    ## docs        this        is         a simple  document second
    ##   doc1 0.3333333 0.3333333 0.3333333      1 0.3333333    0  
    ##   doc2 0.5000000 0.5000000 0.5000000      0 1.0000000    0.5
    

    注意:已在 0.9.8.3 中修复。

    【讨论】:

    • 感谢您的快速回答!不过,我还有另一个问题,有没有办法计算 quanteda 中的“idf”?我只看到 'tf' 和 tfidf' 函数,但没有看到 'idf'。
    • 参见 ?docfreq。您始终可以将其(对数、逆等)转换为 idf。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多