Skip to content

improve dfm_trim() options #1254

Description

@kbenoit

dfm_trim() currently accepts two sets of min/max arguments, for count and docfreq respectively. Each also accepts a fractional value, currently interpreted as a percentile of the features by their total frequencies. We want to make this interpretation both more consistent and more flexible by adding arguments to dfm_trim().

Proposal

dfm_trim(x,
    min_count = 1, max_count = NULL, count_fun = c("sum", "prop", "pctile", "invrank")
    min_docfreq = 1, max_docfreq = NULL, docfreq_fun = c("boolean", "prop" "pctile", "invrank"),
    sparsity = NULL,
    verbose = quanteda_options("verbose"))

where

  • count_fun refers to the aggregation across documents for the feature count, whether this is weighted or not. Applied to the global set of feature counts, the values mean the total, the proportion, the percentile, and the inverse rank of each feature, respectively. (Inverse rank means selecting the top relative frequency min_count features.)
  • docfreq_fun refers to the method of applying the document frequency threshold across documents. boolean is the default, which means count the document frequency of a feature as the number of documents in which it has some weighted value > 0. The other values are the same as applied for counts.

Examples

counts

# remove any features occurring fewer than 5 times in total
dfm_trim(x, min_count = 5)

# remove all but the top 200 most frequent features
dfm_trim(x, min_count = 200, count_fun = "invrank")

# remove any features not occurring at a rate of 10% of total feature count
dfm_trim(x, min_count = .10, count_fun = "prop")

# keep only features occurring at the median total frequency or above
dfm_trim(x, min_count = .50, count_fun = "pctile")

document frequency

# remove any features occurring in fewer than 5 documents each
dfm_trim(x, min_docfreq = 5)

# keep only the most frequent 200 features in terms of document frequency
dfm_trim(x, min_docfreq = 200, docfreq_fun = "invrank")

# remove any features not occurring at least 5% of the total document frequency
dfm_trim(x, min_count = .05, docfreq_fun = "prop")

# keep only features with the median document frequency or higher
dfm_trim(x, min_count = .50, docfreq_fun = "pctile")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions