weighted_group_means: Compute Weighted Group Means

View source: R/soft_lda.R

weighted_group_meansR Documentation

Compute Weighted Group Means

Description

Given a data matrix X (size n x d) and a class-weight matrix F (size c x n), this function computes the weighted means of X for each class. Unlike hard (0/1) assignments, each sample contributes partially to each class's mean according to its weight.

Usage

weighted_group_means(X, F)

Arguments

X

A numeric matrix of shape n x d, where n is the number of samples (rows) and d is the number of features (columns).

F

A numeric matrix of shape c x n, where c is the number of classes and n is the number of samples. F[i, j] is the weight of sample j for class i, and each row must have a positive sum of weights.

Value

A numeric matrix of shape c x d, where each row is the weighted mean of X for one class.

Examples

# Suppose we have 5 samples (rows), 2 features (cols),
# and 3 classes. F has shape (3 x 5).
X <- matrix(1:10, nrow=5, ncol=2)
F <- matrix(runif(3*5, min=0, max=1), nrow=3)
# Ensure each row of F sums to something > 0
res_means <- weighted_group_means(X, F)


bbuchsbaum/discursive documentation built on April 14, 2025, 4:57 p.m.