C#使用Linq和Loop计算集合的平均值、方差【标准差】

方差【标准差】

标准差公式是一种数学公式。标准差也被称为标准偏差,或者实验标准差,公式如下所示:

样本标准差=方差的算术平方根=s=sqrt(((x1-x)^2 +(x2-x)^2 +......(xn-x)^2)/n)

总体标准差=σ=sqrt(((x1-x)^2 +(x2-x)^2 +......(xn-x)^2)/n )

注解:上述两个标准差公式里的x为一组数(n个数据)的算术平均值。当所有数(个数为n)概率性地出现时(对应的n个概率数值和为1),则x为该组数的数学期望

公式意义

所有数(个数为n)记为一个数组[n]。将数组的所有数求和后除以n得到算术平均值。数组的所有数分别减去平均值,得到的n个差值分别取平方,再将得到的所有平方数求和,然后除以数的个数或个数减一若所求为总体标准差则除以n,若所求为样本标准差则除以(n-1),最后把得到的商取算术平方根,就是取1/2次方,得到的结果就是这组数(n个数据)的标准差。

 新建窗体应用程序AverageAndDeviationDemo,将默认的Form1重命名为FormAverageAndDeviation。

窗体FormAverageAndDeviation的视图编辑器源程序如下:

FormAverageAndDeviation.Designer.cs


namespace AverageAndDeviationDemo
{
    partial class FormAverageAndDeviation
    {
        /// 
        /// 必需的设计器变量。
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// 清理所有正在使用的资源。
        /// 
        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// 
        private void InitializeComponent()
        {
            this.rtxtMessage = new System.Windows.Forms.RichTextBox();
            this.btnGenerate = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // rtxtMessage
            // 
            this.rtxtMessage.Location = new System.Drawing.Point(217, 95);
            this.rtxtMessage.Name = "rtxtMessage";
            this.rtxtMessage.Size = new System.Drawing.Size(687, 550);
            this.rtxtMessage.TabIndex = 0;
            this.rtxtMessage.Text = "";
            // 
            // btnGenerate
            // 
            this.btnGenerate.Location = new System.Drawing.Point(38, 93);
            this.btnGenerate.Name = "btnGenerate";
            this.btnGenerate.Size = new System.Drawing.Size(149, 23);
            this.btnGenerate.TabIndex = 1;
            this.btnGenerate.Text = "生成随机集合后开始比较";
            this.btnGenerate.UseVisualStyleBackColor = true;
            this.btnGenerate.Click += new System.EventHandler(this.btnGenerate_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(215, 64);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(143, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "迭代Loop与Linq性能比较:";
            // 
            // FormAverageAndDeviation
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(981, 714);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnGenerate);
            this.Controls.Add(this.rtxtMessage);
            this.Name = "FormAverageAndDeviation";
            this.Text = "计算集合平均值和标准差【方差】的示例";
            this.Load += new System.EventHandler(this.FormAverageAndDeviation_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.RichTextBox rtxtMessage;
        private System.Windows.Forms.Button btnGenerate;
        private System.Windows.Forms.Label label1;
    }
}

窗体代码如下:

FormAverageAndDeviation.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AverageAndDeviationDemo
{
    public partial class FormAverageAndDeviation : Form
    {
        public FormAverageAndDeviation()
        {
            InitializeComponent();
            rtxtMessage.ReadOnly = true;
        }

        /// 
        /// 显示操作内容
        /// 
        /// 
        private void DisplayMessage(string text) 
        {
            this.BeginInvoke(new Action(() => 
            {
                if (rtxtMessage.TextLength > 20480) 
                {
                    rtxtMessage.Clear();
                }
                rtxtMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss.fff")}->{text}\n");
                rtxtMessage.ScrollToCaret();
            }));
        }

        private void FormAverageAndDeviation_Load(object sender, EventArgs e)
        {
            List array = new List() { 1, 2, 3, 4, 5, 6 };
            CalculateAverageAndDeviation(array);
            CalculateAverageAndDeviationUseLoop(array);
        }

        /// 
        /// 计算 平均值和 标准差【方差】
        /// 使用Linq迭代查询
        /// 
        /// 
        private void CalculateAverageAndDeviation(IList array) 
        {
            if (array == null || array.Count == 0) 
            {
                throw new ArgumentException($"无法对空集合求取平均值");
            }
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //获取 平均值
            double average = array.Average();
            //获取方差之和
            double sumDeviation = array.Select(x => Math.Pow(x - average, 2)).Sum();
            //目标 标准差
            double standardDeviation = Math.Sqrt(sumDeviation / array.Count);
            stopwatch.Stop();
            double elapsedTime = stopwatch.Elapsed.TotalMilliseconds;//耗时ms
            DisplayMessage($"【Linq】average:{average}\nsumDeviation:{sumDeviation}\nstandardDeviation:{standardDeviation}\nElapsedTime:{elapsedTime}ms");
        }

        /// 
        /// 计算 平均值和 标准差【方差】
        /// 使用循环迭代器
        /// 
        /// 
        private void CalculateAverageAndDeviationUseLoop(IList array)
        {
            if (array == null || array.Count == 0)
            {
                throw new ArgumentException($"无法对空集合求取平均值");
            }
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            double sum = 0;
            for (int i = 0; i < array.Count; i++)
            {
                sum += array[i];
            }
            //获取 平均值
            double average = sum / array.Count;
            sum = 0;
            for (int i = 0; i < array.Count; i++)
            {
                sum += Math.Pow(array[i] - average, 2);
            }
            //获取方差之和
            double sumDeviation = sum;
            //目标 标准差
            double standardDeviation = Math.Sqrt(sumDeviation / array.Count);
            stopwatch.Stop();
            double elapsedTime = stopwatch.Elapsed.TotalMilliseconds;//耗时ms
            DisplayMessage($"【Loop】average:{average}\nsumDeviation:{sumDeviation}\nstandardDeviation:{standardDeviation}\nElapsedTime:{elapsedTime}ms");
        }

        /// 
        /// 生成10.0~99.9之间的随机集合
        /// 
        /// 
        /// 
        private double[] GenerateRandomArray(int count) 
        {
            double[] array = new double[count];
            for (int i = 0; i < count; i++)
            {
                array[i] = new Random(i + 1).Next(100, 1000) * 0.1;
            }
            return array;
        }

        private async void btnGenerate_Click(object sender, EventArgs e)
        {
            double[] array = GenerateRandomArray(500);
            DisplayMessage($"打印源数组【{string.Join(",", array)}】");
            Task linqTask = Task.Run(() => CalculateAverageAndDeviation(array));
            Task loopTask = Task.Run(() => CalculateAverageAndDeviationUseLoop(array));
            Task.WaitAll(linqTask, loopTask);
            await loopTask;
        }
    }
}

测试运行如图:

C#使用Linq和Loop计算集合的平均值、方差【标准差】_第1张图片

 

你可能感兴趣的:(C#,算法与数据结构,GDI,c#,开发语言,方差,标准差)