Weight the feature frequencies in a dfm
dfm_weight(
x,
scheme = c("count", "prop", "propmax", "logcount", "boolean", "augmented", "logave"),
weights = NULL,
base = 10,
k = 0.5,
smoothing = 0.5,
force = FALSE
)
dfm_smooth(x, smoothing = 1)
document-feature matrix created by dfm
a label of the weight type:
count
\(tf_{ij}\), an integer feature count (default when a dfm is created)
prop
the proportion of the feature counts of total feature counts (aka relative frequency), calculated as \(tf_{ij} / \sum_j tf_{ij}\)
propmax
the proportion of the feature counts of the highest feature count in a document, \(tf_{ij} / \textrm{max}_j tf_{ij}\)
logcount
take the 1 + the logarithm of each count, for the given base, or 0 if the count was zero: \(1 + \textrm{log}_{base}(tf_{ij})\) if \(tf_{ij} > 0\), or 0 otherwise.
boolean
recode all non-zero counts as 1
augmented
equivalent to \(k + (1 - k) *\) dfm_weight(x, "propmax")
logave
(1 + the log of the counts) / (1 + log of the average count within document), or $$\frac{1 + \textrm{log}_{base} tf_{ij}}{1 + \textrm{log}_{base}(\sum_j tf_{ij} / N_i)}$$
logsmooth
log of the counts + smooth
, or \(tf_{ij} + s\)
if scheme
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 features. Any features not named will be assigned a weight of 1.0
(meaning they will be unchanged).
base for the logarithm when scheme
is "logcount"
or
logave
the k for the augmentation when scheme = "augmented"
constant added to the dfm cells for smoothing, default is 1
for dfm_smooth()
and 0.5 for dfm_weight()
logical; if TRUE
, apply weighting scheme even if the dfm
has been weighted before. This can result in invalid weights, such as as
weighting by "prop"
after applying "logcount"
, or after
having grouped a dfm using dfm_group()
.
dfm_weight
returns the dfm with weighted values. Note the
because the default weighting scheme is "count"
, simply calling this
function on an unweighted dfm will return the same object. Many users will
want the normalized dfm consisting of the proportions of the feature counts
within each document, which requires setting scheme = "prop"
.
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.
Manning, C.D., Raghavan, P., & Schütze, H. (2008). An Introduction to Information Retrieval. Cambridge: Cambridge University Press. https://nlp.stanford.edu/IR-book/pdf/irbookonlinereading.pdf
dfmat1 <- dfm(tokens(data_corpus_inaugural))
dfmat2 <- dfm_weight(dfmat1, scheme = "prop")
topfeatures(dfmat2)
#> the , of and . to in our
#> 3.8746040 2.8791122 2.7457270 2.1535669 2.1184249 1.8164093 1.1034320 0.9072259
#> a we
#> 0.8956451 0.8399955
dfmat3 <- dfm_weight(dfmat1)
topfeatures(dfmat3)
#> the , of and . to in a our we
#> 10284 7320 7257 5502 5365 4656 2869 2338 2267 1915
dfmat4 <- dfm_weight(dfmat1, scheme = "logcount")
topfeatures(dfmat4)
#> the , of and . to in a
#> 188.1942 180.6528 179.1567 173.1428 171.6389 168.8410 155.6535 148.9287
#> our that
#> 146.0093 143.7543
dfmat5 <- dfm_weight(dfmat1, scheme = "logave")
topfeatures(dfmat5)
#> the , of and . to in a
#> 125.91859 120.77997 119.84709 115.72921 114.67245 112.93123 103.98982 99.22964
#> our that
#> 97.25317 96.00096
# combine these methods for more complex dfm_weightings, e.g. as in Section 6.4
# of Introduction to Information Retrieval
head(dfm_tfidf(dfmat1, scheme_tf = "logcount"))
#> Document-feature matrix of: 6 documents, 9,437 features (93.83% sparse) and 4 docvars.
#> features
#> docs fellow-citizens of the senate and house representatives
#> 1789-Washington 0.4993976 0 0 0.8239087 0 1.138481 0.8222812
#> 1793-Washington 0 0 0 0 0 0 0
#> 1797-Adams 0.7376709 0 0 0.8239087 0 0 0.8222812
#> 1801-Jefferson 0.6497313 0 0 0 0 0 0
#> 1805-Jefferson 0 0 0 0 0 0 0
#> 1809-Madison 0.4993976 0 0 0 0 0 0
#> features
#> docs : among vicissitudes
#> 1789-Washington 0.1983677 0.1446828 1.079181
#> 1793-Washington 0.1983677 0 0
#> 1797-Adams 0 0.2317905 0
#> 1801-Jefferson 0.1983677 0.1446828 0
#> 1805-Jefferson 0 0.2669539 0
#> 1809-Madison 0 0 0
#> [ reached max_nfeat ... 9,427 more features ]
# smooth the dfm
dfmat <- dfm(tokens(data_corpus_inaugural))
dfm_smooth(dfmat, 0.5)
#> Document-feature matrix of: 60 documents, 9,437 features (0.00% sparse) and 4 docvars.
#> features
#> docs fellow-citizens of the senate and house
#> 1789-Washington 1.5 71.5 116.5 1.5 48.5 2.5
#> 1793-Washington 0.5 11.5 13.5 0.5 2.5 0.5
#> 1797-Adams 3.5 140.5 163.5 1.5 130.5 0.5
#> 1801-Jefferson 2.5 104.5 130.5 0.5 81.5 0.5
#> 1805-Jefferson 0.5 101.5 143.5 0.5 93.5 0.5
#> 1809-Madison 1.5 69.5 104.5 0.5 43.5 0.5
#> features
#> docs representatives : among vicissitudes
#> 1789-Washington 2.5 1.5 1.5 1.5
#> 1793-Washington 0.5 1.5 0.5 0.5
#> 1797-Adams 2.5 0.5 4.5 0.5
#> 1801-Jefferson 0.5 1.5 1.5 0.5
#> 1805-Jefferson 0.5 0.5 7.5 0.5
#> 1809-Madison 0.5 0.5 0.5 0.5
#> [ reached max_ndoc ... 54 more documents, reached max_nfeat ... 9,427 more features ]