Skip to content

Commit 737bfc6

Browse files
luisquintanillaCESARDELATORRE
authored andcommitted
Update README (dotnet#729)
Thanks! 👍
1 parent 44d77ae commit 737bfc6

File tree

1 file changed

+83
-0
lines changed
  • samples/csharp/getting-started/Forecasting_BikeSharingDemand

1 file changed

+83
-0
lines changed

samples/csharp/getting-started/Forecasting_BikeSharingDemand/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,89 @@ To solve this problem, you build and train an ML model on existing training data
7272

7373
![Build -> Train -> Evaluate -> Consume](../shared_content/modelpipeline.png)
7474

75+
## Training pipeline
76+
77+
A time series training pipeline can be defined by using `ForecastBySsa` transform.
78+
79+
```csharp
80+
var forecastingPipeline = mlContext.Forecasting.ForecastBySsa(
81+
outputColumnName: "ForecastedRentals",
82+
inputColumnName: "TotalRentals",
83+
windowSize: 7,
84+
seriesLength: 30,
85+
trainSize: 365,
86+
horizon: 7,
87+
confidenceLevel: 0.95f,
88+
confidenceLowerBoundColumn: "LowerBoundRentals",
89+
confidenceUpperBoundColumn: "UpperBoundRentals");
90+
```
91+
92+
The `forecastingPipeline` takes 365 data points for the first year and samples or splits the time series dataset into 30-day (monthly) intervals as specified by the `seriesLength` parameter. Each of these samples is analyzed through weekly or 7-day window. When determining what the forecasted value for the next period(s) is, the values from previous seven days are used to make a prediction. The model is set to forecast seven periods into the future as defined by the `horizon` parameter. Because a forecast is an informed guess, it's not always 100% accurate. Therefore, it's good to know the range of values in the best and worst-case scenarios as defined by the upper and lower bounds. In this case, the level of confidence for the lower and upper bounds is set to 95%. The confidence level can be increased or decreased accordingly. The higher the value, the wider the range is between the upper and lower bounds to achieve the desired level of confidence.
93+
94+
Then, to train the model, use the `Fit` method.
95+
96+
```csharp
97+
SsaForecastingTransformer forecaster = forecastingPipeline.Fit(firstYearData);
98+
```
99+
100+
## Evaluate the model
101+
102+
To evaluate the model, compare use the `Transform` method to forecast future values. Then, compare them against the actual values and calculate metrics like *Mean Absolute Error* and *Root Mean Squared Error*.
103+
104+
```csharp
105+
static void Evaluate(IDataView testData, ITransformer model, MLContext mlContext)
106+
{
107+
// Make predictions
108+
IDataView predictions = model.Transform(testData);
109+
110+
// Actual values
111+
IEnumerable<float> actual =
112+
mlContext.Data.CreateEnumerable<ModelInput>(testData, true)
113+
.Select(observed => observed.TotalRentals);
114+
115+
// Predicted values
116+
IEnumerable<float> forecast =
117+
mlContext.Data.CreateEnumerable<ModelOutput>(predictions, true)
118+
.Select(prediction => prediction.ForecastedRentals[0]);
119+
120+
// Calculate error (actual - forecast)
121+
var metrics = actual.Zip(forecast, (actualValue, forecastValue) => actualValue - forecastValue);
122+
123+
// Get metric averages
124+
var MAE = metrics.Average(error => Math.Abs(error)); // Mean Absolute Error
125+
var RMSE = Math.Sqrt(metrics.Average(error => Math.Pow(error, 2))); // Root Mean Squared Error
126+
127+
// Output metrics
128+
Console.WriteLine("Evaluation Metrics");
129+
Console.WriteLine("---------------------");
130+
Console.WriteLine($"Mean Absolute Error: {MAE:F3}");
131+
Console.WriteLine($"Root Mean Squared Error: {RMSE:F3}\n");
132+
}
133+
```
134+
135+
**Mean Absolute Error**: Measures how close predictions are to the actual value. This value ranges between 0 and infinity. The closer to 0, the better the quality of the model.
136+
137+
**Root Mean Squared Error**: Summarizes the error in the model. This value ranges between 0 and infinity. The closer to 0, the better the quality of the model.
138+
139+
## Forecasting values
140+
141+
To forecast values, create a `TimeSeriesPredictionEngine`, a convenience API to make single predictions.
142+
143+
```csharp
144+
var forecastEngine = forecaster.CreateTimeSeriesEngine<ModelInput, ModelOutput>(mlContext);
145+
```
146+
147+
Then, use the `Predict` method to generate a single forecast for the number of periods specified by the `horizon`.
148+
149+
```csharp
150+
static void Forecast(IDataView testData, int horizon, TimeSeriesPredictionEngine<ModelInput, ModelOutput> forecaster, MLContext mlContext)
151+
{
152+
ModelOutput forecast = forecaster.Predict();
153+
154+
//... additional code
155+
}
156+
```
157+
75158
## Sample Output
76159

77160
When you run the application, you should see output similar to the following:

0 commit comments

Comments
 (0)