Skip to content

Commit

Permalink
option to center sparse X
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasmueboe committed Jun 10, 2024
1 parent df00a02 commit 354bd98
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions multispaeti/_multispati_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class MultispatiPCA(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstim
c\ :sub:`ij` should be larger if i and j are close.
A distance matrix should be transformed to connectivities by e.g.
calculating :math:`1-d/d_{max}` beforehand.
center_sparse : bool
Whether to center `X` if it is a sparse array. By default sparse `X` will not be
centered as this requires transforming it to a dense array, potentially raising
out-of-memory errors.
Attributes
----------
Expand Down Expand Up @@ -94,9 +98,11 @@ def __init__(
n_components: int | tuple[int, int] | None = None,
*,
connectivity: _Connectivity | None = None,
center_sparse: bool = False,
):
self.n_components = n_components
self.connectivity = connectivity
self.center_sparse = center_sparse

@staticmethod
def _validate_connectivity(W: _Connectivity, n: int):
Expand Down Expand Up @@ -180,6 +186,10 @@ def _fit(self, X: _X, *, return_transform: bool = False) -> np.ndarray | None:

self.W_ = normalize(W, norm="l1")

if self.center_sparse and issparse(X):
assert isinstance(X, (csr_array, csr_matrix, csc_array, csc_matrix))
X = X.toarray()

if issparse(X):
self.mean_ = None
X_centered = X
Expand Down

0 comments on commit 354bd98

Please sign in to comment.