How to Use YAML Configuration¶
This guide shows you how to save, load, and validate estimator configurations as YAML files using EstimatorConfig. Use this when you want to manage estimator parameters as declarative config files rather than Python code.
Interactive notebook available
Try this guide as an interactive notebook: How to Configure Estimators with YAML
Prerequisites¶
Install the config extra:
Build an Estimator from a Config¶
Create an EstimatorConfig with a dotted import path and constructor parameters, then call .build():
from sklearn_wrap.config import EstimatorConfig
config = EstimatorConfig(
estimator_class="sklearn.linear_model.Ridge",
params={"alpha": 2.0, "fit_intercept": True},
)
estimator = config.build()
Capture a Config from an Existing Estimator¶
Use EstimatorConfig.from_estimator() to snapshot any sklearn-compatible estimator:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Ridge
pipe = Pipeline([("scaler", StandardScaler()), ("ridge", Ridge(alpha=0.5))])
config = EstimatorConfig.from_estimator(pipe)
Save and Load YAML¶
# Save
config.to_yaml("pipeline.yaml")
# Load
loaded = EstimatorConfig.from_yaml("pipeline.yaml")
estimator = loaded.build()
See Also¶
- Advanced YAML Patterns: YAML anchors,
!includecomposition, multi-file configs - YAML Configuration example: interactive walkthrough
- Configuration Reference:
EstimatorConfigAPI and config functions - About YAML Configuration Design: why declarative config and the trusted modules security model