Learn R Programming

quanteda (version 0.99)

dfm_weight: weight the feature frequencies in a dfm

Description

Returns a document by feature matrix with the feature frequencies weighted according to one of several common methods. Some shortcut functions that offer finer-grained control are:

  • tf compute term frequency weights

  • tfidf compute term frequency-inverse document frequency weights

  • docfreq compute document frequencies of features

Usage

dfm_weight(x, type = c("frequency", "relfreq", "relmaxfreq", "logfreq",
  "tfidf"), weights = NULL)

dfm_smooth(x, smoothing = 1)

Arguments

x

document-feature matrix created by dfm

type

a label of the weight type:

"frequency"

integer feature count (default when a dfm is created)

"relfreq"

the proportion of the feature counts of total feature counts (aka relative frequency)

"relmaxfreq"

the proportion of the feature counts of the highest feature count in a document

"logfreq"

take the logarithm of 1 + the feature count, for base 10

"tfidf"

Term-frequency * inverse document frequency. For a full explanation, see, for example, http://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html. This implementation will not return negative values. For finer-grained control, call tfidf directly.

weights

if type is unused, then weights can be a named numeric vector of weights to be applied to the dfm, where the names of the vector correspond to feature labels of the dfm, and the weights will be applied as multipliers to the existing feature counts for the corresponding named fatures. Any features not named will be assigned a weight of 1.0 (meaning they will be unchanged).

smoothing

constant added to the dfm cells for smoothing, default is 1

Value

dfm_weight returns the dfm with weighted values.

dfm_smooth returns a dfm whose values have been smoothed by adding the smoothing amount. Note that this effectively converts a matrix from sparse to dense format, so may exceed memory requirements depending on the size of your input matrix.

References

Manning, Christopher D., Prabhakar Raghavan, and Hinrich Schutze. Introduction to Information Retrieval. Vol. 1. Cambridge: Cambridge University Press, 2008.

See Also

tf, tfidf, docfreq

Examples

Run this code
# NOT RUN {
dtm <- dfm(data_corpus_inaugural)

x <- apply(dtm, 1, function(tf) tf/max(tf))
topfeatures(dtm)
normDtm <- dfm_weight(dtm, "relfreq")
topfeatures(normDtm)
maxTfDtm <- dfm_weight(dtm, type = "relmaxfreq")
topfeatures(maxTfDtm)
logTfDtm <- dfm_weight(dtm, type = "logfreq")
topfeatures(logTfDtm)
tfidfDtm <- dfm_weight(dtm, type = "tfidf")
topfeatures(tfidfDtm)

# combine these methods for more complex dfm_weightings, e.g. as in Section 6.4
# of Introduction to Information Retrieval
head(tfidf(dtm, scheme_tf = "log"))

#' # apply numeric weights
str <- c("apple is better than banana", "banana banana apple much better")
(mydfm <- dfm(str, remove = stopwords("english")))
dfm_weight(mydfm, weights = c(apple = 5, banana = 3, much = 0.5))

# }
# NOT RUN {
# smooth the dfm
dfm_smooth(mydfm, 0.5)
# }

Run the code above in your browser using DataLab