|
| 1 | +--- |
| 2 | +title: CsvExporter |
| 3 | +description: Export results to a wide-format CSV |
| 4 | +--- |
| 5 | + |
| 6 | +# CsvExporter |
| 7 | + |
| 8 | +Exports detailed prediction results to a wide-format CSV file. Each row represents a single forecast point, with per-model prediction and error columns. |
| 9 | + |
| 10 | +## Parameters |
| 11 | + |
| 12 | +| Parameter | Type | Default | Description | |
| 13 | +|-----------|------|---------|-------------| |
| 14 | +| `path` | str | Required | Output CSV file path | |
| 15 | +| `extra_columns` | List[str] | `[]` | Columns from the source dataset to include | |
| 16 | + |
| 17 | +## Basic Usage |
| 18 | + |
| 19 | +```python |
| 20 | +from epftoolbox2.exporters import CsvExporter |
| 21 | + |
| 22 | +exporter = CsvExporter("results.csv") |
| 23 | +``` |
| 24 | + |
| 25 | +## Output Format |
| 26 | + |
| 27 | +The CSV contains the following columns: |
| 28 | + |
| 29 | +**Base columns:** |
| 30 | +- `run_date` — date the forecast was made |
| 31 | +- `target_date` — date being forecasted |
| 32 | +- `hour` — hour of day (0-23) |
| 33 | +- `horizon` — forecast horizon (1 to max) |
| 34 | +- `day_in_test` — day index in the test period |
| 35 | +- `actual` — actual observed value |
| 36 | + |
| 37 | +**Per-model columns:** |
| 38 | +- `{model}_prediction` — model's prediction |
| 39 | +- `{model}_error` — residual (prediction - actual) |
| 40 | + |
| 41 | +**Extra columns** (optional): |
| 42 | +- Any columns from the source dataset, joined by `target_date` + `hour` |
| 43 | + |
| 44 | +### Example Output |
| 45 | + |
| 46 | +For a pipeline with models `OLS` and `LassoCV`, and `extra_columns=["is_holiday"]`: |
| 47 | + |
| 48 | +| run_date | target_date | hour | horizon | actual | OLS_prediction | OLS_error | LassoCV_prediction | LassoCV_error | is_holiday | |
| 49 | +|----------|-------------|------|---------|--------|---------------|-----------|-------------------|---------------|------------| |
| 50 | +| 2024-02-01 | 2024-02-02 | 0 | 1 | 48.50 | 45.23 | -3.27 | 46.10 | -2.40 | 0 | |
| 51 | + |
| 52 | +## Extra Columns |
| 53 | + |
| 54 | +Use `extra_columns` to include columns from the source dataset (e.g., calendar features, weather data). Columns are joined by matching `target_date` and `hour` from the results to the source dataset's DatetimeIndex. |
| 55 | + |
| 56 | +```python |
| 57 | +exporter = CsvExporter( |
| 58 | + "results.csv", |
| 59 | + extra_columns=["is_holiday", "load_forecast", "warsaw_temperature_2m"], |
| 60 | +) |
| 61 | +``` |
| 62 | + |
| 63 | +If a requested column does not exist in the source dataset, it is silently skipped. |
| 64 | + |
| 65 | +## In Pipeline |
| 66 | + |
| 67 | +```python |
| 68 | +from epftoolbox2.pipelines import ModelPipeline |
| 69 | +from epftoolbox2.models import OLSModel, LassoCVModel |
| 70 | +from epftoolbox2.evaluators import MAEEvaluator, RMSEEvaluator |
| 71 | +from epftoolbox2.exporters import CsvExporter |
| 72 | + |
| 73 | +pipeline = ( |
| 74 | + ModelPipeline() |
| 75 | + .add_model(OLSModel(predictors=predictors, name="OLS")) |
| 76 | + .add_model(LassoCVModel(predictors=predictors, cv=7, name="LassoCV")) |
| 77 | + .add_evaluator(MAEEvaluator()) |
| 78 | + .add_evaluator(RMSEEvaluator()) |
| 79 | + .add_exporter(CsvExporter( |
| 80 | + "results.csv", |
| 81 | + extra_columns=["is_holiday", "load_forecast"], |
| 82 | + )) |
| 83 | +) |
| 84 | + |
| 85 | +report = pipeline.run(data=df, test_start="2024-02-01", test_end="2024-03-01", target="price", horizon=7) |
| 86 | +# Results saved to results.csv |
| 87 | +``` |
0 commit comments