zoukankan      html  css  js  c++  java
  • 拉依达准则 剔除异常点

    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;
    		}
    	}
    }
    

      

  • 相关阅读:
    MyISAM 和InnoDB 区别 转
    beautifulsoup
    爬虫学习
    python操作数据库
    爬虫
    python爬虫
    PHP中“简单工厂模式”实例讲解
    PERL 实现微信登录
    PERL 实现微信登录
    NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 和american_america.AL32UTF8
  • 原文地址:https://www.cnblogs.com/jiamiemie/p/10559609.html
Copyright © 2011-2022 走看看