我正在使用Microsoft.ML
和Microsoft.ML.TimeSeries
来预测一组数字中的异常值。我很难理解我看到的结果。
这是我的代码:
var counts = new[] { 1, 3, 0, 4, 5, 5, 4, 3, 3, 0, 13, 8, 1, 61, 21, 40, 7, 7, 5, 6, 8, 33, 11, 5, 2, 10, 11, 18, 14, 23, 8, 17, 15, 13, 24, 29, 15, 20, 29, 19, 18, 17, 23, 47, 7, 14, 26, 28, 5, 22, 47, 22, 20, 9, 40, 6, 8, 4, 10, 10, 1, 4, 27, 3, 3, 7, 6, 12, 8, 3, 1, 2, 0, 0, 2, 0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 2 };var input = counts.Select(x => new Input { Count = x });var mlContext = new MLContext();IDataView data = mlContext.Data.LoadFromEnumerable(input);var iidSpikeEstimator = mlContext.Transforms.DetectIidSpike(nameof(Output.Prediction), nameof(Input.Count), 95, counts.Length / 4);var empty = mlContext.Data.LoadFromEnumerable(new List<Input>());ITransformer iidSpikeTransform = iidSpikeEstimator.Fit(empty);IDataView transformedData = iidSpikeTransform.Transform(data);var predictions = mlContext.Data.CreateEnumerable<Output>(transformedData, false);foreach (var prediction in predictions){ Console.WriteLine($"{prediction.Prediction[0]}\t{prediction.Prediction[1]}\t{prediction.Prediction[2]}");}
我使用了以下输入和预测类:
class Input{ public float Count { get; set; }}class Output{ [VectorType(3)] public double[] Prediction { get; set; }}
预测的输出看起来像这样:
0 1 0,50 3 0,02275006288725640 0 0,08000261555683920 4 0,07336260731420350 5 0,09324626262574680 5 0,1908715427888270 4 0,3793658939070110 3 0,4522491125423570 3 0,4543375551070540 0 0,09656461688070730 13 7,23183654849358E-070 8 0,1627962255087860 1 0,268664450559490 61 1E-08 <-- why not a spike0 21 0,1953218123519450 40 0,07628985932177510 7 0,4814184562065970 7 0,4835626729623250 5 0,4415853920142990 6 0,4673004289508560 8 0,4842919988909460 33 0,1118562122161610 11 0,4415187627738490 5 0,4219793790334210 2 0,3484508097567360 10 0,4668402175020560 11 0,4543559222018260 18 0,3164680969641880 14 0,4109081873786850 23 0,2520484578843710 8 0,4222073383577720 17 0,3828167783958440 15 0,4397732642572550 13 0,4993290937260860 24 0,2699835173116370 29 0,20867537259730 15 0,4965672805299240 20 0,3270200816948740 29 0,1473695174898640 19 0,3341649373312340 18 0,3817593360276710 17 0,4308978322639090 23 0,257780908642751 47 0,00250415226768458 <-- agree0 7 0,2007971628631480 14 0,4210368502711460 26 0,2301527260210950 28 0,2022632996292370 5 0,1073627589739730 22 0,3857319924985471 47 0,0251788165486866 <-- agree0 22 0,4409302492986290 20 0,4890122679710930 9 0,1981735571998130 40 0,1005776204358930 6 0,1404066873511990 8 0,1939191656301750 4 0,1361138508480660 10 0,2734719069067760 10 0,2931343130531930 1 0,1346654588452830 4 0,2099066817733120 27 0,2857415571452360 3 0,2115261450852490 3 0,2309729799393450 7 0,3261266795010190 6 0,3222834867615390 12 0,4806556834989340 8 0,3768850913341820 3 0,2688715022005230 1 0,2438802184970840 2 0,2868176993040780 0 0,2456583263150340 0 0,2663082131333360 2 0,3174902167572220 0 0,2701808354616690 2 0,3577618045455980 0 0,2997594328854540 0 0,2632048452583110 0 0,2802578223395880 4 0,4867523768151130 0 0,298996001689680 0 0,317857688429590 0 0,3362697869785280 0 0,339621605914990 0 0,351302744059660 0 0,3181969197351710 1 0,4310709601859830 1 0,441314458853670 0 0,3540637983389610 0 0,3722266491930850 2 0,300492332964686
如内联所示,识别出了两个异常值。对我来说,这些预测看起来相当好。我不理解的是,为什么第14行的计数没有被标记为异常值。在第14个数字之前,计数在0和13之间波动。然后突然跳到61。当我在图表中查看数据时,这个跳跃对我来说确实看起来像是一个异常值。
谁能帮我理解这里发生了什么?
回答:
问题似乎是您设置的95%置信度在p值变得太小时无法正常工作。当您在Prediction[2]中添加’0.000’时,可以看到小于0.000的值不会显示为异常值。
Console.WriteLine($"{prediction.Prediction[0]}\t{prediction.Prediction[1]}\t{prediction.Prediction[2:0.000]}");0 13 0.0000 8 0.1630 23 0.0000 23 0.2581 47 0.0030 7 0.201
我不确定为什么它不起作用,但一个解决方法是通过检查小于0.05的置信度来手动检测异常值。例如:
if (prediction.Prediction[2] < (1 - 0.95)){ prediction.Prediction[0] = 1;}Console.WriteLine($"{prediction.Prediction[0]}\t{prediction.Prediction[1]}\t{prediction.Prediction[2]:0.000}");
将得到以下结果:
0 1 0.5001 3 0.0230 0 0.0800 4 0.0730 5 0.0930 5 0.1910 4 0.3790 3 0.4520 3 0.4540 0 0.0971 13 0.0000 8 0.1631 23 0.0001 61 0.0000 51 0.0570 6 0.4460 7 0.4680 7 0.4710 5 0.4330 6 0.4570 8 0.4980 33 0.1330 11 0.4560 5 0.4160 2 0.3470 10 0.4800 11 0.4680 18 0.3360 14 0.4260 23 0.2720 8 0.4160 17 0.3980 15 0.4530 13 0.4890 24 0.2890 29 0.2280 15 0.4910 20 0.3250 29 0.0930 19 0.3340 18 0.3820 17 0.4310 23 0.2581 47 0.0030 7 0.2010 14 0.4210 26 0.2300 28 0.2020 5 0.1070 22 0.3861 47 0.0250 22 0.4410 20 0.4890 9 0.1980 40 0.1010 6 0.1400 8 0.1940 4 0.1360 10 0.2730 10 0.2930 1 0.1350 4 0.2100 27 0.2860 3 0.2120 3 0.2310 7 0.3260 6 0.3220 12 0.4810 8 0.3770 3 0.2690 1 0.2440 2 0.2870 0 0.2460 0 0.2660 2 0.3170 0 0.2700 2 0.3580 0 0.3000 0 0.2630 0 0.2800 4 0.4870 0 0.2990 0 0.3180 0 0.3360 0 0.3400 0 0.3510 0 0.3180 1 0.4310 1 0.4410 0 0.3540 0 0.3720 2 0.300