推荐一个开源的.NET统计库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/139384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:05:50  来源:igfitidea点击:

Recommend an Open Source .NET Statistics Library

.netmathopen-sourcestatistics

提问by Mark Heath

I need to calculate averages, standard deviations, medians etc for a bunch of numerical data. Is there a good open source .NET library I can use? I have found NMath but it is not free and may be overkill for my needs.

我需要计算一堆数值数据的平均值、标准偏差、中位数等。有我可以使用的好的开源 .NET 库吗?我找到了 NMath,但它不是免费的,可能对我的需求来说太过分了。

采纳答案by mistrmark

I found this on the CodeProject website. It looks like a good C# class for handling most of the basic statistical functions.

我在 CodeProject 网站上找到了这个。它看起来是一个很好的 C# 类,用于处理大多数基本统计函数。

回答by John D. Cook

You have to be careful. There are several ways to compute standard deviation that would give the same answer if floating point arithmetic were perfect. They're all accurate for some data sets, but some are far better than others under some circumstances.

你必须要小心。如果浮点运算是完美的,那么有几种计算标准偏差的方法会给出相同的答案。对于某些数据集,它们都是准确的,但在某些情况下,有些比其他数据要好得多。

The method I've seen proposed here is the one that is most likely to give bad answers. I used it myself until it crashed on me.

我在这里看到的方法是最有可能给出错误答案的方法。我自己使用它,直到它撞到我身上。

See Comparing three methods of computing standard deviation.

请参阅比较计算标准偏差的三种方法

回答by Benoit

Have a look at MathNetit is not specifically for statistics, but there might be useful functionality for what you want

看看MathNet,它不是专门用于统计的,但可能有您想要的有用功能

回答by Jafin

Apache Maths.Commonand run it through IKVM.

Apache Maths.Common并通过IKVM运行它。

回答by Mark Heath

I decided it was quicker to write my own, that just did what I needed. Here's the code...

我决定写我自己的更快,这正好满足了我的需要。这是代码...

/// <summary>
/// Very basic statistical analysis routines
/// </summary>
public class Statistics
{
    List<double> numbers;
    public double Sum { get; private set; }
    public double Min { get; private set; }
    public double Max { get; private set; }
    double sumOfSquares;

    public Statistics()
    {
        numbers = new List<double>();
    }

    public int Count
    {
        get { return numbers.Count; }
    }

    public void Add(double number)
    {
        if(Count == 0)
        {
            Min = Max = number;
        }
        numbers.Add(number);
        Sum += number;
        sumOfSquares += number * number;
        Min = Math.Min(Min,number);
        Max = Math.Max(Max,number);            
    }

    public double Average
    {
        get { return Sum / Count; }
    }

    public double StandardDeviation
    {
        get { return Math.Sqrt(sumOfSquares / Count - (Average * Average)); }
    }

    /// <summary>
    /// A simplistic implementation of Median
    /// Returns the middle number if there is an odd number of elements (correct)
    /// Returns the number after the midpoint if there is an even number of elements
    /// Sorts the list on every call, so should be optimised for performance if planning
    /// to call lots of times
    /// </summary>
    public double Median
    {
        get
        {
            if (numbers.Count == 0)
                throw new InvalidOperationException("Can't calculate the median with no data");
            numbers.Sort();
            int middleIndex = (Count) / 2;
            return numbers[middleIndex];
        }
    }
}

回答by CharlesB

AForge.NEThas AForge.Math namespace, providing some basic statistics functions: Histogram, mean, median, stddev, entropy.

AForge.NET具有 AForge.Math 命名空间,提供一些基本的统计函数:直方图、均值、中值、标准差、熵。

回答by David Pope

If you just need to do some one-off number crunching, a spreadsheet is far and away your best tool. It's trivial to spit out a simple CSV file from C#, which you can then load up in Excel (or whatever):

如果您只需要进行一些一次性的数字运算,那么电子表格绝对是您最好的工具。从 C# 吐出一个简单的 CSV 文件是微不足道的,然后您可以将其加载到 Excel(或其他)中:

class Program
{
    static void Main(string[] args)
    {
        using (StreamWriter sw = new StreamWriter("output.csv", false, Encoding.ASCII))
        {
            WriteCsvLine(sw, new List<string>() { "Name", "Length", "LastWrite" });

            DirectoryInfo di = new DirectoryInfo(".");
            foreach (FileInfo fi in di.GetFiles("*.mp3", SearchOption.AllDirectories))
            {
                List<string> columns = new List<string>();
                columns.Add(fi.Name.Replace(",", "<comma>"));
                columns.Add(fi.Length.ToString());
                columns.Add(fi.LastWriteTime.Ticks.ToString());

                WriteCsvLine(sw, columns);
            }
        }
    }

    static void WriteCsvLine(StreamWriter sw, List<string> columns)
    {
        sw.WriteLine(string.Join(",", columns.ToArray()));
    }
}

Then you can just 'start excel output.csv' and use functions like "=MEDIAN(B:B)", "=AVERAGE(B:B)", "=STDEV(B:B)". You get charts, histograms (if you install the analysis pack), etc.

然后你可以'启动 excel output.csv' 并使用诸如“=MEDIAN(B:B)”、“=AVERAGE(B:B)”、“=STDEV(B:B)”之类的函数。你会得到图表、直方图(如果你安装了分析包)等。

The above doesn't handle everything; generalized CSV files are more complex than you might think. But it's "good enough" for much of the analysis I do.

以上并不能处理所有事情;通用 CSV 文件比您想象的要复杂。但对于我所做的大部分分析来说,它已经“足够好”了。