1. 程式人生 > 其它 >ML .NET 異常情況檢測

ML .NET 異常情況檢測

場景

商品的價格會隨時間發生變化,可以通過ML .NET異常情況檢測功能獲得價格的拐點峰值,為決策做參考

資料集

資料來自Every Cryptocurrency Daily Market Price
原始資料包含幣種symbol、時間date、開盤價open、最高價high、最低價low、收盤價close等多種資料
這裡只取Bitcoin在2013年4月28日至2018年11月29日的開盤價格,如圖所示

訓練

自動化ML尚不支援異常情況檢測,所以這裡需要正常的寫程式碼
根據教程:使用 ML.NET 檢測產品銷售中的異常的步驟,可以很簡單的進行coding

峰值檢測

關鍵程式碼如下

var iidSpikeEstimator = mlContext.Transforms.DetectIidSpike(
                outputColumnName: nameof(CommodityPricePrediction.Prediction),
                inputColumnName: nameof(CommodityPriceModel.open),
                confidence: 95,
                pvalueHistoryLength: _docsize/31);

檢測峰值的關鍵方法是DetectIidSpike
有一個輸出列outputColumnName,一個輸入列inputColumnName
confidencepvalueHistoryLength引數影響峰值的最終結果
confidence越低,演算法檢測到“較小”峰值的可能性就大
pvalueHistoryLength越低,模型忘記之前的較大峰值的速度就越快,這裡因為資料是按天遞進的,_docsize為總資料量2042條,pvalueHistoryLength取每個月,所以以_docsize除以31
詳細資料見IidSpikeEstimator 類

更改點檢測

關鍵程式碼如下

var iidChangePointEstimator = mlContext.Transforms.DetectIidChangePoint(
            outputColumnName: nameof(CommodityPricePrediction.Prediction),
            inputColumnName: nameof(CommodityPriceModel.open),
            confidence: 95,
            changeHistoryLength: _docsize/31);

檢測峰值的關鍵方法是DetectIidChangePoint
引數與DetectIidSpike類似
詳細資料見IidChangePointEstimator 類

根據當前訓練資料和設定的引數,可以得到以下結果

峰值檢測
Alert   Score   P-Value
0       135.30  0.50
0       134.44  0.19
0       144.00  0.00
0       139.00  0.41
......
1       270.91  0.02 <-- Spike detected 檢測到尖峰
0       261.86  0.07
0       263.57  0.06
1       269.31  0.03 <-- Spike detected 檢測到尖峰
1       266.50  0.05 <-- Spike detected 檢測到尖峰
1       273.65  0.03 <-- Spike detected 檢測到尖峰
1       276.50  0.03 <-- Spike detected 檢測到尖峰
1       281.45  0.02 <-- Spike detected 檢測到尖峰
1       283.63  0.03 <-- Spike detected 檢測到尖峰
1       285.18  0.03 <-- Spike detected 檢測到尖峰
1       293.70  0.02 <-- Spike detected 檢測到尖峰
1       304.32  0.01 <-- Spike detected 檢測到尖峰
1       313.94  0.01 <-- Spike detected 檢測到尖峰
1       328.51  0.01 <-- Spike detected 檢測到尖峰
1       315.01  0.03 <-- Spike detected 檢測到尖峰
1       325.94  0.02 <-- Spike detected 檢測到尖峰
1       361.87  0.00 <-- Spike detected 檢測到尖峰
1       403.66  0.00 <-- Spike detected 檢測到尖峰
1       408.08  0.01 <-- Spike detected 檢測到尖峰
1       388.05  0.03 <-- Spike detected 檢測到尖峰
1       374.27  0.05 <-- Spike detected 檢測到尖峰
1       384.28  0.05 <-- Spike detected 檢測到尖峰
......

更改點檢測
Alert   Score   P-Value Martingale value
0       135.30  0.50    0.00
0       134.44  0.19    0.00
0       144.00  0.00    0.00
0       139.00  0.41    0.00
0       116.38  0.00    0.00
0       106.25  0.03    0.00
0       98.10   0.06    0.00
......
0       419.41  0.02    93.13
0       417.28  0.03    289.14
0       440.96  0.03    1230.73
0       496.58  0.02    7860.61
0       712.76  0.00    3417895.47
0       577.98  0.02    20823140.67
0       594.32  0.03    104358445.48
0       724.07  0.01    1135503399.54
0       771.70  0.01    11511784560.11
0       795.63  0.02    101146411707.08
1       773.02  0.03    661588163070.51 <-- alert is on, predicted changepoint  警報開啟,預測變化點
0       805.73  0.04    4050159523330.93
0       923.85  0.02    38568890913474.40
0       1003.38 0.02    499206959027848.12
0       1042.01 0.02    5059346677869355.00
0       1129.37 0.02    55327355244724032.00
0       1128.92 0.03    407440713595917504.00
0       951.42  0.08    916461290575384448.00
......

示例程式碼

CommodityPrice

參考資料

教程:使用 ML.NET 檢測產品銷售中的異常
IidSpikeEstimator 類
IidChangePointEstimator 類