Cross-Validation API¶
cross_val_score ¶
cross_val_score(
similarity_matrix: ndarray,
estimator: BaseEstimator | None = None,
param_grid: dict[str, list] | None = None,
n_repeats: int = 5,
sampling_fraction: float = 0.8,
estimate_sampling_fraction: bool | dict = False,
sampling_selection: str = "mean",
random_state: int = 0,
verbose: int = 1,
n_jobs: int = -1,
missing_values: float | None = np.nan,
fit_final_estimator: bool = False,
) -> GridSearchCV
Cross-validate any estimator for matrix completion.
Generic cross-validation function that works with SRF or any sklearn-compatible estimator with a .reconstruct() method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
similarity_matrix
|
ndarray
|
Symmetric similarity matrix to cross-validate |
required |
estimator
|
BaseEstimator or None
|
Estimator to cross-validate. If None, uses SRF(random_state=random_state). Can be a single estimator or a Pipeline. Must have a .reconstruct() method. |
None
|
param_grid
|
dict or None
|
Dictionary with parameter names (str) as keys and lists of values to try as values. If None, uses default {'rank': [5, 10, 15, 20]} for SRF. |
None
|
n_repeats
|
int
|
Number of times to repeat the cross-validation |
5
|
sampling_fraction
|
float
|
Fraction of eligible entries to use for training in each split; must be in (0, 1). The remaining (1 - sampling_fraction) becomes validation. Note: Constant diagonal entries are excluded from both train and validation. Ignored when estimate_sampling_fraction is True or a dict; if both are provided, estimate_sampling_fraction takes precedence. |
0.8
|
estimate_sampling_fraction
|
bool or dict
|
If True, automatically estimate optimal sampling fraction using sampling bound estimation from Random Matrix Theory. If dict, passed as kwargs to estimate_sampling_bounds_fast(). When enabled, overrides sampling_fraction. |
False
|
sampling_selection
|
str
|
Selection method for the estimated sampling fraction; one of {"mean", "min", "max"}. |
"mean"
|
random_state
|
int
|
Random seed for reproducibility |
0
|
verbose
|
int
|
Verbosity level |
1
|
n_jobs
|
int
|
Number of jobs to run in parallel (-1 uses all processors) |
-1
|
missing_values
|
float or None
|
Value to consider as missing in original data |
np.nan
|
fit_final_estimator
|
bool
|
Whether to fit the final estimator on the best parameters |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
grid |
GridSearchCV
|
Fitted GridSearchCV object with best parameters and scores |
Examples:
>>> from pysrf.cross_validation import cross_val_score
>>> result = cross_val_score(similarity_matrix, param_grid={'rank': [5, 10, 15]})
Source code in pysrf/cross_validation.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |
Grid Search¶
GridSearchCV ¶
GridSearchCV(
estimator: BaseEstimator,
param_grid: dict[str, list],
cv: EntryMaskSplit,
n_jobs: int = -1,
verbose: int = 0,
fit_final_estimator: bool = False,
)
Grid search cross-validation for matrix completion.
Performs exhaustive grid search over specified parameter values with entry-wise cross-validation for symmetric matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
BaseEstimator
|
Model instance to optimize |
required |
param_grid
|
dict
|
Dictionary with parameter names as keys and lists of values to try |
required |
cv
|
EntryMaskSplit
|
Cross-validation splitter |
required |
n_jobs
|
int
|
Number of parallel jobs (-1 uses all processors) |
-1
|
verbose
|
int
|
Verbosity level |
0
|
fit_final_estimator
|
bool
|
Whether to fit the model on full data with best parameters |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
best_params_ |
dict
|
Parameters that gave the best score |
best_score_ |
float
|
Best validation score achieved |
cv_results_ |
DataFrame
|
Detailed results for all parameter combinations |
best_estimator_ |
estimator
|
Fitted estimator with best parameters (if fit_final_estimator=True) |
Source code in pysrf/cross_validation.py
CV Strategy¶
EntryMaskSplit ¶
EntryMaskSplit(
n_repeats: int = 5,
sampling_fraction: float = 0.8,
random_state: int | None = None,
missing_values: float | None = np.nan,
)
Bases: BaseCrossValidator
Cross-validator for symmetric matrices using entry-wise splits.
Generates multiple random train/validation splits by masking entries in a symmetric matrix while preserving symmetry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_repeats
|
int
|
Number of random splits to generate |
5
|
sampling_fraction
|
float
|
Fraction of eligible entries kept for training; must be in (0, 1). Remaining (1 - sampling_fraction) becomes validation. Note: Constant diagonal entries are excluded from both. |
0.8
|
random_state
|
int or None
|
Random seed for reproducibility |
None
|
missing_values
|
float or None
|
Value that marks missing entries in original data |
np.nan
|
Source code in pysrf/cross_validation.py
split ¶
split(
x: ndarray, y: ndarray = None, groups: ndarray = None
) -> Generator[Tuple[np.ndarray, np.ndarray], None, None]
Generate train/validation splits.
Yields:
| Name | Type | Description |
|---|---|---|
train_mask |
ndarray of bool
|
Training entries (True = use for training) |
validation_mask |
ndarray of bool
|
Validation entries (True = use for evaluation) |
Source code in pysrf/cross_validation.py
Scoring¶
fit_and_score ¶
fit_and_score(
estimator: BaseEstimator,
x: ndarray,
train_mask: ndarray,
validation_mask: ndarray,
fit_params: dict,
split_idx: int | None = None,
) -> dict
Fit estimator with parameters and return validation score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
BaseEstimator
|
Model instance to fit (works with SRF or any estimator with .reconstruct()) |
required |
x
|
ndarray
|
Full data matrix |
required |
train_mask
|
ndarray of bool
|
Boolean mask where True = training entry |
required |
validation_mask
|
ndarray of bool
|
Boolean mask where True = validation entry |
required |
fit_params
|
dict
|
Parameters to set on the estimator |
required |
split_idx
|
int or None
|
Index of the CV split |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
result |
dict
|
Dictionary with score, parameters, and fitted estimator |
Source code in pysrf/cross_validation.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |