Result of cross-validated penalizer selection for CoxNet.
CoxNetCVResult(
penalizers,
mean_scores,
std_scores,
n_nonzero,
metric,
l1_ratio,
k,
)
Returned by cv_coxnet(). Stores the cross-validation scores at every tested penalizer value, identifies the best penalizer (highest concordance or lowest Brier score), and applies the one-standard-error rule for a more parsimonious alternative.
Penalizers are ordered from largest (most regularized, sparsest model) to smallest (least regularized, densest model).
Attributes
penalizers_
-
Penalizer values tested, sorted descending.
mean_scores_
-
Mean cross-validation score at each penalizer.
std_scores_
-
Standard deviation of per-fold scores at each penalizer.
n_nonzero_
-
Average number of non-zero coefficients at each penalizer (averaged over cross-validation folds).
metric_
-
Metric used: "concordance" or "brier".
l1_ratio_
-
Elastic-net mixing parameter fixed during the path search.
k_
-
Number of folds used.
best_penalizer_
-
Penalizer with the best mean cross-validation score.
best_score_
-
Mean score at best_penalizer_.
penalizer_1se_
-
Largest penalizer (most regularized) whose mean score is within one standard error of best_score_. Prefer this when parsimony matters.
score_1se_
-
Mean score at
penalizer_1se_.
Examples
Run cross-validated penalizer selection and inspect the result:
import greenwood as gw
# Load data and write the model formula
lung = gw.load_dataset("lung", backend="polars")
formula = "Surv(time, status) ~ age + sex + ph.ecog + ph.karno + wt.loss"
# Run cross-validated penalizer selection
cv_result = gw.cv_coxnet(formula, data=lung, seed=23)
cv_result
CoxNetCV (concordance, ↑ higher is better, l1_ratio=1.0, 5-fold)
best penalizer : 0.10767 (mean concordance: 0.6405)
1-SE penalizer : 0.18923 (mean concordance: 0.6193)
50 penalizers tested, range [0.000218, 0.218]
The best penalizer and the more conservative one-standard-error penalizer are available as attributes:
# Retrieve the best penalizer value
cv_result.best_penalizer_
# Retrieve the more conservative one-standard-error penalizer
cv_result.penalizer_1se_
Methods
| to_frame() |
Return the CV path as a tidy DataFrame, one row per penalizer. |
to_frame()
Return the CV path as a tidy DataFrame, one row per penalizer.
to_frame(
*,
format=None,
)
Parameters
format: str | None = None
-
Output format:
None (default), "pandas", "polars", or "pyarrow". When None, a backend is auto-detected.
Returns
DataFrame
-
Columns:
penalizer, mean_score, std_score, n_nonzero. Rows are ordered from largest to smallest penalizer.
Examples
Export the full CV path as a Polars DataFrame for plotting or further analysis:
import greenwood as gw
# Load data and run cross-validated penalizer selection
lung = gw.load_dataset("lung", backend="polars")
formula = "Surv(time, status) ~ age + sex + ph.ecog + ph.karno + wt.loss"
result = gw.cv_coxnet(formula, data=lung, seed=23)
# Export the CV path as a Polars DataFrame
result.to_frame(format="polars")
shape: (50, 4)| penalizer | mean_score | std_score | n_nonzero |
|---|
| f64 | f64 | f64 | f64 |
| 0.217882 | 0.510069 | 0.022515 | 0.2 |
| 0.189233 | 0.619313 | 0.051879 | 1.6 |
| 0.164351 | 0.618485 | 0.052703 | 1.8 |
| 0.142741 | 0.63301 | 0.06026 | 2.2 |
| 0.123972 | 0.637076 | 0.063636 | 2.4 |
| … | … | … | … |
| 0.000383 | 0.625341 | 0.061453 | 5.0 |
| 0.000333 | 0.62533 | 0.061629 | 5.0 |
| 0.000289 | 0.62533 | 0.061629 | 5.0 |
| 0.000251 | 0.62533 | 0.061629 | 5.0 |
| 0.000218 | 0.62533 | 0.061629 | 5.0 |