Compute multiple centroids in a single call, either from a list of anchor
term sets (word-vector mode) or from a feature matrix with a grouping
vector (feature-matrix mode). The plural counterpart to
get_centroid().
Arguments
- fv
Matrix or sparse
Matrix::dgCMatrixof feature vectors. In word-vector mode this is the word embedding matrix (rows are terms); in feature-matrix mode this is a document-by-feature matrix (rows are documents).- anchors
List of anchor term vectors. Each element is passed to
get_centroid()along withfvandmissing. If the list is unnamed, integer indices are used as the rownames of the result.- groups
Vector of group labels of the same length as
nrow(fv). A centroid is computed for each level viacolMeans(). Character vectors are preferred over factors; empty factor levels are dropped with a message.- missing
Character scalar. Either
"stop"or"remove". Inherited fromget_centroid()and applies only to theanchors(word-vector) branch. Seeget_centroid().
Value
Matrix with one row per centroid and ncol(fv) columns.
Rownames are the names of anchors (or the levels of groups).
Examples
data(ft_wv_sample)
## word-vector mode: named list of anchor sets
anchors <- list(
space = c("spacecraft", "rocket", "moon"),
water = c("ocean", "sea")
)
cens <- get_centroids(ft_wv_sample, anchors = anchors)
## feature-matrix mode: group rows of a matrix by a label vector
mat <- ft_wv_sample[1:10, , drop = FALSE]
labs <- rep(c("a", "b"), each = 5)
cens <- get_centroids(mat, groups = labs)
