拉依达准则 剔除异常点
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Tool { /// <summary> /// 拉依达准则 剔除异常数据 /// </summary> public class Laida { /// <summary> /// 初始化? /// </summary> public Laida() { } /// <summary> /// 平均值 /// </summary> /// <returns></returns> static double SetMean<T>(IList<T> list, double sum) { return sum / list.Count; } /// <summary> /// 标准差 /// </summary> /// <returns></returns> static double StandardDeviation<T>(IList<T> list, double mean, Func<T, double> func) { IList<double> listXi = new List<double>(); foreach (var kv in list) { listXi.Add((func(kv) - mean) * (func(kv) - mean)); } double sumXi = listXi.Sum(); double d2 = sumXi / (list.Count - 1); double d = Math.Sqrt(d2); return d; } static IList<T> GetGood<T>(IList<T> list, double mean, Func<T, double> func) { double sd3 = StandardDeviation(list, mean, func) * 3;//3倍标准差 IList<T> resList = new List<T>(); foreach (var kv in list) { if (Math.Abs(func(kv) - mean) < sd3) { resList.Add(kv); } } return resList; } /// <summary> /// 拉依达剔除异常数据 /// </summary> /// <param name="list">List<T>数组</param> /// <param name="sum">需要剔除字段的总值</param> /// <param name="func">需要剔除的字段</param> /// <returns></returns> public static IList<T> GetGoodList<T>(IList<T> list, double sum, Func<T, double> func) { //平均值 var mean = SetMean(list, sum); bool isRun = true; IList<T> reslist = list; while (isRun) { int num = reslist.Count; reslist = GetGood(list, mean, func); if (reslist.Count == num) { isRun = false; } } return reslist; } } }
咩咩咩