Skip to content

Support for 1d weights (e.g. sample-weighting) #4

Description

@aryamccarthy

PCA weighted by observations (samples) is described in this exchange on Cross Validated.

Feasibly, it can be performed by taking a weight vector of shape (n_rows, 1) and copying it into multiple columns. What was originally, say,

0.25
0.25
0.5

is now

0.25    0.25    0.25
0.25    0.25    0.25
0.5     0.5     0.5

using the following function:

def make_weights_matrix(weights, X):
    if weights.shape[0] != X.shape[0]:
        raise ValueError("weights {} and X {} must have same length.".format(weights.shape, X.shape))
    w_new = np.empty_like(X)
    w_new[:] = weights
    return w_new
w = make_weights_matrix(weights, df)

This could be avoided if the package supported broadcasting, rather than having utils perform numerous checks to assure that weights and X have the same shape. Additionally, the einsum logic would have to be circumvented since the broadcasting should be built-in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions