Consensus API¶
SRF is a non-convex method, so different random initializations can land on slightly different embeddings. The consensus transformers run several random starts and aggregate them into a single, stable embedding. They follow the scikit-learn transformer API (fit/transform), so you can drop them into a Pipeline in place of a single SRF. See examples for how to use them in a workflow.
EnsembleFit ¶
EnsembleFit(
base_estimator: BaseEstimator,
n_runs: int = 50,
random_state: int = 0,
n_jobs: int = -1,
threads_per_worker: int | str | None = None,
)
Bases: BaseEstimator, TransformerMixin
Fit the base estimator multiple times and stack the embeddings.
Each run uses a different random seed, producing embeddings that differ only in column permutation. The stacked output has shape (n_samples, rank * n_runs) and can be passed to a consensus method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_estimator
|
BaseEstimator
|
Estimator with fit/transform (e.g. SRF) |
required |
n_runs
|
int
|
Number of independent runs |
50
|
random_state
|
int
|
Base random seed (run i uses random_state + i) |
0
|
n_jobs
|
int
|
Number of parallel jobs |
-1
|
Attributes:
| Name | Type | Description |
|---|---|---|
embeddings_ |
ndarray of shape (n_samples, rank * n_runs)
|
Horizontally stacked embeddings from all runs |
estimators_ |
list of BaseEstimator
|
Fitted estimators from each run |
Source code in pysrf/consensus.py
fit ¶
Fit n_runs independent estimators in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray of shape (n_samples, n_samples)
|
Symmetric similarity matrix |
required |
y
|
Ignored
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
EnsembleFit
|
|
Source code in pysrf/consensus.py
transform ¶
Return the stacked embeddings (x is ignored).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Ignored
|
|
required |
y
|
Ignored
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
embeddings |
ndarray of shape (n_samples, rank * n_runs)
|
|
Source code in pysrf/consensus.py
AlignedConsensus ¶
Bases: BaseEstimator, TransformerMixin
Consensus via Hungarian alignment for symmetric NMF.
Symmetric NMF produces the same dimensions across runs but in different order (permutation ambiguity, no rotation). This class aligns dimensions via the Hungarian algorithm and returns the most central run, the one closest to the element-wise median across all aligned runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
int
|
Number of dimensions in each embedding |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
aligned_embeddings_ |
ndarray of shape (n_runs, n_samples, rank)
|
Embeddings after Hungarian alignment |
consensus_median_ |
ndarray of shape (n_samples, rank)
|
Element-wise median across aligned runs |
centrality_scores_ |
ndarray of shape (n_runs,)
|
Frobenius distance of each run to the consensus median |
agreement_scores_ |
ndarray of shape (n_runs,)
|
Mean pairwise cosine similarity per run (higher = more stable) |
selected_run_idx_ |
int
|
Index of the most central run |
Examples:
>>> from sklearn.pipeline import Pipeline
>>> from pysrf import SRF
>>> from pysrf.consensus import EnsembleFit, AlignedConsensus
>>>
>>> pipeline = Pipeline([
... ('ensemble', EnsembleFit(SRF(rank=10), n_runs=50)),
... ('consensus', AlignedConsensus(rank=10))
... ])
>>> embedding = pipeline.fit_transform(similarity_matrix)
fit ¶
Align embeddings and select the most central run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray of shape (n_samples, n_runs * rank)
|
Stacked embeddings from EnsembleFit |
required |
y
|
Ignored
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
AlignedConsensus
|
|
Source code in pysrf/consensus.py
transform ¶
Return the most central run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Ignored
|
|
required |
y
|
Ignored
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
w |
ndarray of shape (n_samples, rank)
|
|