使用 ML.NET 進(jìn)行電力消耗時(shí)序預(yù)測(cè):基于家庭用電數(shù)據(jù)的實(shí)戰(zhàn)分析
準(zhǔn)確預(yù)測(cè)電力消耗對(duì)于電力系統(tǒng)的規(guī)劃和運(yùn)營(yíng)至關(guān)重要。本文將詳細(xì)介紹如何使用 ML.NET 構(gòu)建時(shí)序預(yù)測(cè)模型,以預(yù)測(cè)全局有功功率(Global_active_power)的變化。
項(xiàng)目概述
- 目標(biāo):預(yù)測(cè)未來24小時(shí)的電力消耗
- 技術(shù)棧:ML.NET、C#
- 算法:?jiǎn)巫兞繒r(shí)序分析(SSA)
- 數(shù)據(jù)源:家庭用電量數(shù)據(jù)集
環(huán)境準(zhǔn)備
- 創(chuàng)建新的 C# 控制臺(tái)應(yīng)用程序
- 安裝必要的 NuGet 包:
<PackageReference Include="Microsoft.ML" Version="2.0.0" />
<PackageReference Include="Microsoft.ML.TimeSeries" Version="2.0.0" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
完整代碼實(shí)現(xiàn)
1. 定義數(shù)據(jù)模型
// 原始數(shù)據(jù)模型
public class PowerConsumptionRawData
{
[LoadColumn(0)]
public string Date { get; set; }
[LoadColumn(1)]
public string Time { get; set; }
[LoadColumn(2)]
public float Global_active_power { get; set; }
}
public class PowerConsumptionData
{
public DateTime Timestamp { get; set; }
public float Global_active_power { get; set; }
}
public class PowerPrediction
{
public float[] ForecastedPower { get; set; }
public float[] LowerBoundPower { get; set; }
public float[] UpperBoundPower { get; set; }
}
2. 主程序?qū)崿F(xiàn)
using Microsoft.ML;
using Microsoft.ML.Transforms.TimeSeries;
using System.Globalization;
namespace App11
{
internal class Program
{
static void Main(string[] args)
{
// 初始化 ML.NET 上下文
MLContext mlContext = new MLContext(seed: 0);
// 加載數(shù)據(jù)
// 加載原始數(shù)據(jù)
IDataView rawDataView = mlContext.Data.LoadFromTextFile<PowerConsumptionRawData>(
path: "household_power_consumption.txt",
hasHeader: true,
separatorChar: ';'
);
// 轉(zhuǎn)換數(shù)據(jù):合并日期時(shí)間并處理格式
var transformedData = mlContext.Data.CreateEnumerable<PowerConsumptionRawData>(rawDataView, reuseRowObject: false)
.Select(row => new PowerConsumptionData
{
Timestamp = ParseDateTime(row.Date + " " + row.Time),
Global_active_power = row.Global_active_power
})
.OrderBy(x => x.Timestamp)
.ToList();
// 將處理后的數(shù)據(jù)轉(zhuǎn)換回 IDataView
IDataView dataView = mlContext.Data.LoadFromEnumerable(transformedData);
// 定義預(yù)測(cè)管道
var pipeline = mlContext.Forecasting.ForecastBySsa(
outputColumnName: "ForecastedPower",
inputColumnName: nameof(PowerConsumptionData.Global_active_power),
windowSize: 24, // 24小時(shí)窗口
seriesLength: 72, // 使用3天的數(shù)據(jù)進(jìn)行分析
trainSize: 8760, // 使用一年的數(shù)據(jù)訓(xùn)練
horizon: 24, // 預(yù)測(cè)未來24小時(shí)
confidenceLevel: 0.95f,
confidenceLowerBoundColumn: "LowerBoundPower",
confidenceUpperBoundColumn: "UpperBoundPower"
);
// 訓(xùn)練模型
var model = pipeline.Fit(dataView);
Console.WriteLine("模型訓(xùn)練完成!");
// 評(píng)估模型
// 評(píng)估模型
IDataView predictions = model.Transform(dataView);
// 獲取預(yù)測(cè)值和實(shí)際值
var forecastingEngine = model.CreateTimeSeriesEngine<PowerConsumptionData, PowerPrediction>(mlContext);
var forecast = forecastingEngine.Predict();
// 手動(dòng)計(jì)算評(píng)估指標(biāo)
IEnumerable<float> actualValues = mlContext.Data.CreateEnumerable<PowerConsumptionData>(dataView,true)
.Select(x => x.Global_active_power);
IEnumerable<float> predictedValues = mlContext.Data.CreateEnumerable<PowerPrediction>(predictions, true)
.Select(x => x.ForecastedPower[0]);
// 首先打印數(shù)據(jù)數(shù)量
Console.WriteLine($"實(shí)際值數(shù)量: {actualValues.Count()}");
Console.WriteLine($"預(yù)測(cè)值數(shù)量: {predictedValues.Count()}");
// 檢查是否有無效值
var hasInvalidActual = actualValues.Any(x => float.IsNaN(x) || float.IsInfinity(x));
var hasInvalidPredicted = predictedValues.Any(x => float.IsNaN(x) || float.IsInfinity(x));
Console.WriteLine($"實(shí)際值中包含無效值: {hasInvalidActual}");
Console.WriteLine($"預(yù)測(cè)值中包含無效值: {hasInvalidPredicted}");
// 計(jì)算差異時(shí)過濾掉無效值
var metrics = actualValues.Zip(predictedValues, (actual, predicted) => new { Actual = actual, Predicted = predicted })
.Where(pair => !float.IsNaN(pair.Actual) && !float.IsInfinity(pair.Actual) &&
!float.IsNaN(pair.Predicted) && !float.IsInfinity(pair.Predicted))
.Select(pair => (double)pair.Actual - (double)pair.Predicted)
.ToList();
// 計(jì)算評(píng)估指標(biāo)
double mse = metrics.Select(x => x * x).Average();
double rmse = Math.Sqrt(mse);
double mae = metrics.Select(x => Math.Abs(x)).Average();
// 輸出評(píng)估指標(biāo)
Console.WriteLine($"均方誤差 (MSE): {mse:F3}");
Console.WriteLine($"均方根誤差 (RMSE): {rmse:F3}");
Console.WriteLine($"平均絕對(duì)誤差 (MAE): {mae:F3}");
// 保存模型
string modelPath = "PowerPredictionModel.zip";
mlContext.Model.Save(model, dataView.Schema, modelPath);
// 加載模型并預(yù)測(cè)
var loadedModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
var forecastEngine = loadedModel.CreateTimeSeriesEngine<PowerConsumptionData, PowerPrediction>(mlContext);
var fforecast = forecastEngine.Predict();
// 輸出預(yù)測(cè)結(jié)果
Console.WriteLine("\n未來24小時(shí)的電力消耗預(yù)測(cè):");
for (int i = 0; i < fforecast.ForecastedPower.Length; i++)
{
Console.WriteLine($"第 {i + 1} 小時(shí): {fforecast.ForecastedPower[i]:F3} kW " +
$"(置信區(qū)間: {fforecast.LowerBoundPower[i]:F3} - {fforecast.UpperBoundPower[i]:F3} kW)");
}
}
static DateTime ParseDateTime(string dateString)
{
string[] formats = { "d/M/yyyy H:mm:ss", "dd/MM/yyyy H:mm:ss" }; // 支持多種格式
DateTime date;
DateTime.TryParseExact(dateString, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
return date;
}
}
}
`MLContext.Forecasting.ForecastBySsa` 方法的所有參數(shù):
必需參數(shù)
outputColumnName: string
// 預(yù)測(cè)結(jié)果輸出列的名稱
// 例如: "ForecastedPower"
inputColumnName: string
// 輸入數(shù)據(jù)列的名稱,用于預(yù)測(cè)的源數(shù)據(jù)列
// 例如: nameof(PowerConsumptionData.Global_active_power)
windowSize: int
// SSA(奇異譜分析)的窗口大小
// - 必須大于0且小于seriesLength
// - 影響模型捕捉的季節(jié)性模式
// - 推薦設(shè)置為預(yù)期周期長(zhǎng)度的1/2到1/3
可選但重要的參數(shù)
seriesLength: int
// 用于訓(xùn)練的時(shí)間序列片段長(zhǎng)度
// - 默認(rèn)值:windowSize * 2
// - 必須大于windowSize
// - 建議值:windowSize的2-3倍
trainSize: int
// 用于訓(xùn)練的數(shù)據(jù)點(diǎn)數(shù)量
// - 默認(rèn)值:整個(gè)數(shù)據(jù)集大小
// - 確定模型訓(xùn)練使用的歷史數(shù)據(jù)量
horizon: int
// 預(yù)測(cè)的未來時(shí)間點(diǎn)數(shù)量
// - 默認(rèn)值:1
// - 指定要預(yù)測(cè)多少個(gè)未來時(shí)間點(diǎn)
置信區(qū)間相關(guān)參數(shù)
confidenceLevel: float
// 預(yù)測(cè)的置信水平
// - 取值范圍:0到1之間
// - 常用值:0.95(95%置信度)
// - 默認(rèn)值:0.95
confidenceLowerBoundColumn: string
// 置信區(qū)間下界的輸出列名
// - 可選參數(shù)
// - 例如: "LowerBoundPower"
confidenceUpperBoundColumn: string
// 置信區(qū)間上界的輸出列名
// - 可選參數(shù)
// - 例如: "UpperBoundPower"
最佳實(shí)踐
// 對(duì)于每小時(shí)數(shù)據(jù)的典型設(shè)置
var pipeline = mlContext.Forecasting.ForecastBySsa(
outputColumnName: "Forecast",
inputColumnName: "Value",
windowSize: 24, // 一天
seriesLength: 72, // 三天
trainSize: 8760, // 一年
horizon: 24, // 預(yù)測(cè)一天
confidenceLevel: 0.95f
);
// 對(duì)于每日數(shù)據(jù)的典型設(shè)置
var pipeline = mlContext.Forecasting.ForecastBySsa(
outputColumnName: "Forecast",
inputColumnName: "Value",
windowSize: 7, // 一周
seriesLength: 21, // 三周
trainSize: 365, // 一年
horizon: 7, // 預(yù)測(cè)一周
confidenceLevel: 0.95f
);
這些參數(shù)的正確設(shè)置對(duì)模型的預(yù)測(cè)性能至關(guān)重要,建議根據(jù)實(shí)際數(shù)據(jù)特征和業(yè)務(wù)需求進(jìn)行調(diào)整和實(shí)驗(yàn)。
模型評(píng)估
使用兩個(gè)主要指標(biāo)評(píng)估模型性能:
- 平均絕對(duì)誤差 (MAE)
a.MAE 直接反映預(yù)測(cè)值與實(shí)際值的平均偏差
b.數(shù)值越小表示預(yù)測(cè)越準(zhǔn)確
c.單位與原始數(shù)據(jù)相同,便于理解
- 均方根誤差 (RMSE)
- RMSE 對(duì)大誤差更敏感
- 通過平方放大了大誤差的影響
- 最終開方使單位與原始數(shù)據(jù)相同
總結(jié)
本文展示了如何使用 ML.NET 構(gòu)建電力消耗預(yù)測(cè)模型的完整過程。通過合理配置和訓(xùn)練,可以得到一個(gè)可靠的預(yù)測(cè)模型,幫助優(yōu)化電力系統(tǒng)運(yùn)營(yíng)。這個(gè)方法不僅適用于家庭用電預(yù)測(cè),還可以擴(kuò)展到工業(yè)用電預(yù)測(cè)、智能電網(wǎng)管理等領(lǐng)域。