C#使用拉依達準則(3σ準則)剔除異常數據(.Net剔除一組數據中的奇異值)

原文: C#使用拉依達準則(3σ準則)剔除異常數據(.Net剔除一組數據中的奇異值)

一、問題的提出:html

電池生產中,遇到一批電池的測量結果數據:this

電壓值 電池個數
電壓值 電池個數
電壓值 電池個數
電壓值 電池個數
0.056 1   4.09 1   4.146 17   4.174 13434
0.321 1   4.094 1   4.147 17   4.175 13973
0.767 1   4.099 2   4.148 19   4.176 13339
0.972 1   4.112 1   4.149 23   4.177 12275
3.098 1   4.119 3   4.15 26   4.178 10309
3.187 1   4.12 1   4.151 40   4.179 8376
3.319 1   4.121 1   4.152 50   4.18 6324
3.526 1   4.122 3   4.153 75   4.181 4667
3.53 1   4.125 3   4.154 84   4.182 3340
3.532 1   4.126 2   4.155 100   4.183 2358
3.54 1   4.127 1   4.156 118   4.184 1719
3.541 1   4.128 2   4.157 153   4.185 1199
3.544 1   4.129 3   4.158 173   4.186 839
3.545 2   4.13 2   4.159 248   4.187 622
3.832 1   4.132 2   4.16 335   4.188 417
3.928 1   4.133 2   4.161 419   4.189 304
3.93 1   4.134 4   4.162 540   4.19 170
3.951 1   4.135 1   4.163 731   4.191 124
3.963 1   4.136 5   4.164 962   4.192 77
3.972 1   4.137 4   4.165 1359   4.193 43
3.973 2   4.138 6   4.166 1846   4.194 44
4.045 1   4.139 9   4.167 2621   4.195 25
4.046 1   4.14 2   4.168 3728   4.196 20
4.079 1   4.141 6   4.169 5086   4.197 8
4.085 1   4.142 4   4.17 6822   4.198 9
4.087 1   4.143 6   4.171 8649   4.199 5
4.088 1   4.144 13   4.172 10210   4.2 3
4.089 1   4.145 14   4.173 12072      

其中,有一部分電池的電壓出現太低和太高的狀況,並不符合正態分佈。spa

如今須要剔除這些異常的電池數據。.net

二、方法原理:htm

3σ準則又稱爲拉依達準則,它是先假設一組檢測數據只含有隨機偏差,對其進行計算處理獲得標準誤差,按必定機率肯定一個區間,認爲凡超過這個區間的偏差,就不屬於隨機偏差而是粗大偏差,含有該偏差的數據應予以剔除。
在正態分佈中σ表明標準差,μ表明均值。x=μ即爲圖像的對稱軸
3σ原則:
數值分佈在(μ-σ,μ+σ)中的機率爲0.6827
數值分佈在(μ-2σ,μ+2σ)中的機率爲0.9544
數值分佈在(μ-3σ,μ+3σ)中的機率爲0.9974
能夠認爲,Y 的取值幾乎所有集中在(μ-3σ,μ+3σ)區間內,超出這個範圍的可能性僅佔不到0.3%。blog


三、C#的具體實現:
ci

//定義電壓-數量關係的類
    public class VoltageCount
    {
        public Double Voltage { get; set; }
        public int CountV { get; set; }
        public VoltageCount()
        {
        }

        public VoltageCount(Double voltage, int countV)
        {
            this.Voltage = voltage;
            this.CountV = countV;
        }
    }get


//關鍵類使用拉依達準則(3σ準則)剔除數據異常
it

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Photo.QQAA.Net.Helper
{
    /// <summary>
    /// 使用拉依達準則(3σ準則)剔除數據異常
    /// </summary>
    public class ExceptionVoltageHelper
    {
        List<VoltageCount> listVoltageCount;
        double average = 0.0;
        int _badDataCount = -1;//奇異值個數

        /// <summary>
        /// 獲取奇異值個數
        /// </summary>
        public int BadDataCount
        {
            get { return _badDataCount; }
        }

        public ExceptionVoltageHelper(List<VoltageCount> list)
        {
            this.listVoltageCount = list;
            SetAverage();
        }

        /// <summary>
        /// 取得平均電壓值
        /// </summary>
        /// <returns></returns>
        protected double GetAvgVoltage()
        {
            double avg = 0;
            double total = 0;
            int allCount = 0;
            foreach (VoltageCount vc in listVoltageCount)
            {
                double v = vc.Voltage;
                int c = vc.CountV;
                total += v * c;
                allCount += c;
            }
            avg = total / (allCount * 1.0);

            return Math.Round(avg, 3, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 平均值
        /// </summary>
        /// <returns></returns>
        void SetAverage()
        {
            this.average = GetAvgVoltage();
        }

        /// <summary>
        /// 標準差
        /// </summary>
        /// <returns></returns>
        double StandardDeviation()
        {
            List<double> listDataV = new List<double>();
            foreach (VoltageCount vc in this.listVoltageCount)
            {
                double v = vc.Voltage;
                int countV = vc.CountV;
                for (int i = 0; i < countV; i++ )
                {
                    listDataV.Add((v - this.average) * (v - this.average));
                }
            }
            double sumDataV = listDataV.Sum();
            double std = Math.Sqrt(sumDataV / (listDataV.Count - 1));

            return std;
        }

        public List<VoltageCount> GetGoodList()
        {
            _badDataCount = 0;
            double sd3 = StandardDeviation() * 3;//3倍標準差
            List<VoltageCount> listVC = new List<VoltageCount>();
            foreach (VoltageCount vc in this.listVoltageCount)
            {
                if (Math.Abs(vc.Voltage - this.average) <= sd3)
                {
                    listVC.Add(vc);
                }
                else
                {
                    _badDataCount += vc.CountV;
                }
            }

            return listVC;
        }

    }
}

四、侷限性及注意事項:io

本3σ法則僅侷限於對正態或近似正態分佈的樣本數據處理,且適用於有較多組數據的時候。這種判別處理原理及方法是以測量次數充分大爲前提的,當測量次數的情形用準則剔除粗大偏差是不夠可靠的。所以,在測量次數較少的狀況下,最好不要選用準則,而用其餘準則。

相關文章
相關標籤/搜索