Model API¶
SRF is the main estimator in PySRF. Given a symmetric similarity matrix \(S\), it learns a sparse, non-negative embedding \(W\) such that \(S \approx WW^\top\), where each row of \(W\) holds an item's loadings on revealed dimensions and small loadings mark dimensions that are irrelevant to that item. It works directly on matrices with missing entries, so no imputation is needed. Reach for SRF whenever you want interpretable, parts-based dimensions out of a similarity matrix; see quickstart for an end-to-end walkthrough.
SRF Class¶
SRF ¶
SRF(
rank: int = 10,
rho: float = 3.0,
max_outer: int = 100,
max_inner: int = 10,
tol: float = 0.001,
verbose: int = 0,
init: str = "random_sqrt",
random_state: int | None = None,
missing_values: float | None = np.nan,
bounds: tuple[float, float] | None = (None, None),
check_input: bool = True,
)
Bases: TransformerMixin, BaseEstimator
Similarity-based Representation Factorization.
Factorizes a symmetric similarity matrix S into WW' where W >= 0. Handles missing entries and can enforce bounds on the reconstructed similarities.
The objective is: min_{W>=0, V} ||M * (S - V)||^2_F + rho/2 * ||V - WW'||^2_F where M is the observation mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
int
|
Number of dimensions in the embedding |
10
|
rho
|
float
|
Penalty weight controlling how closely WW' must match V |
3.0
|
max_outer
|
int
|
Maximum number of outer optimization iterations; the stall rule usually stops far earlier |
100
|
max_inner
|
int
|
Maximum iterations for the W update per outer iteration |
10
|
tol
|
float
|
Relative convergence tolerance. Fitting stops when the relative fit ||X - WW'||_F / ||X||_F over observed entries falls below tol or improves by less than tol across the last 10 iterations (Xu et al., 2012). With missing data, the ADMM splitting must additionally agree: ||V - WW'||_F <= tol * max(||V||_F, ||WW'||_F). |
1e-3
|
verbose
|
int
|
Whether to print optimization progress |
0
|
init
|
str
|
Embedding initialization method ('random', 'random_sqrt') |
'random_sqrt'
|
random_state
|
int or None
|
Random seed for reproducible initialization |
None
|
missing_values
|
float or None
|
Sentinel value marking missing entries in the similarity matrix |
np.nan
|
bounds
|
tuple of (float, float) or None
|
(lower, upper) bounds on the reconstructed similarities. For example, (0, 1) for similarities normalized to [0, 1]. |
(None, None)
|
check_input
|
bool
|
Whether to verify that the input matrix and missing mask are symmetric. Set to False for large matrices where the input is known to be valid, to avoid allocating temporary arrays during the symmetry check. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
w_ |
ndarray of shape (n_samples, rank)
|
Learned embedding matrix |
components_ |
ndarray of shape (n_samples, rank)
|
Alias for w_ (sklearn compatibility) |
n_iter_ |
int
|
Number of outer iterations performed |
history_ |
dict
|
Optimization metrics per iteration |
Examples:
>>> from pysrf import SRF
>>> model = SRF(rank=10, random_state=42)
>>> w = model.fit_transform(similarity_matrix)
>>> reconstruction = w @ w.T
>>> # With missing data
>>> similarity_matrix[mask] = np.nan
>>> model = SRF(rank=10, missing_values=np.nan)
>>> w = model.fit_transform(similarity_matrix)
Source code in pysrf/model.py
_fit_loop ¶
Drive the outer iterations; steps yields one measurement each.
Source code in pysrf/model.py
_validate_input ¶
Validate the similarity matrix and derive the observation stats.
Allocates only the one contract copy of x and the observation mask; all statistics are computed without materializing the observed entries.
Source code in pysrf/model.py
fit ¶
Fit the model to a similarity matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (n_samples, n_samples)
|
Symmetric similarity matrix. Missing values should be marked
according to the |
required |
y
|
Ignored
|
Not used, present for sklearn API compatibility. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
SRF
|
Fitted estimator. |
Source code in pysrf/model.py
fit_transform ¶
Fit the model and return the learned embedding matrix W.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (n_samples, n_samples)
|
Symmetric similarity matrix |
required |
y
|
Ignored
|
Not used, present for sklearn API compatibility. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
w |
ndarray of shape (n_samples, rank)
|
Embedding matrix |
Source code in pysrf/model.py
reconstruct ¶
Reconstruct the similarity matrix as WW'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w
|
ndarray of shape (n_samples, rank) or None
|
Embedding matrix. If None, uses the fitted embedding. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
s_hat |
ndarray of shape (n_samples, n_samples)
|
Reconstructed similarity matrix |
Source code in pysrf/model.py
score ¶
Negative MSE on observed entries (higher is better).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (n_samples, n_samples)
|
Symmetric similarity matrix. Missing values should be marked
according to the |
required |
y
|
Ignored
|
Not used, present for sklearn API compatibility. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
score |
float
|
Negative mean squared reconstruction error on observed entries. |
Source code in pysrf/model.py
transform ¶
Return the learned embedding matrix W.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array-like of shape (n_samples, n_samples)
|
Ignored, present for sklearn API compatibility. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
w |
ndarray of shape (n_samples, rank)
|
Embedding matrix |