Skip to contents

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().

Usage

get_centroids(fv, anchors = NULL, groups = NULL, missing = "stop")

Arguments

fv

Matrix or sparse Matrix::dgCMatrix of 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 with fv and missing. 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 via colMeans(). Character vectors are preferred over factors; empty factor levels are dropped with a message.

missing

Character scalar. Either "stop" or "remove". Inherited from get_centroid() and applies only to the anchors (word-vector) branch. See get_centroid().

Value

Matrix with one row per centroid and ncol(fv) columns. Rownames are the names of anchors (or the levels of groups).

See also

Author

Dustin Stoltz

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)