在C#中生成整数数据的简单直方图

Jon*_*age 8 c# histogram

作为我正在构建的测试平台的一部分,我正在寻找一个简单的类来计算整数值的直方图(算法解决问题的迭代次数).答案应该是这样的:

Histogram my_hist = new Histogram();

for( uint i = 0; i < NUMBER_OF_RESULTS; i++ )
{

    myHist.AddValue( some_result );
}

for( uint j = 0; j < myHist.NumOfBins; j++ )
{
     Console.WriteLine( "{0} occurred {1} times", myHist.BinValues[j], myHist.BinCounts[j] );
}
Run Code Online (Sandbox Code Playgroud)

我很惊讶有点谷歌搜索没有找到一个简洁的解决方案,但也许我没有找到正确的事情.那里有通用的解决方案还是值得我自己推出?

Ste*_*eef 17

你可以使用SortedDictionary

uint[] items = new uint[] {5, 6, 1, 2, 3, 1, 5, 2}; // sample data
SortedDictionary<uint, int> histogram = new SortedDictionary<uint, int>();
foreach (uint item in items) {
    if (histogram.ContainsKey(item)) {
        histogram[item]++;
    } else {
        histogram[item] = 1;
    }
}
foreach (KeyValuePair<uint, int> pair in histogram) {
    Console.WriteLine("{0} occurred {1} times", pair.Key, pair.Value);
}
Run Code Online (Sandbox Code Playgroud)

但是,这会留下空箱子


Jon*_*age 6

基于BastardSaint的建议,我提出了一个整洁且相当通用的包装器:

public class Histogram<TVal> : SortedDictionary<TVal, uint>
{
    public void IncrementCount(TVal binToIncrement)
    {
        if (ContainsKey(binToIncrement))
        {
            this[binToIncrement]++;
        }
        else
        {
            Add(binToIncrement, 1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以现在我能做到:

const uint numOfInputDataPoints = 5;
Histogram<uint> hist = new Histogram<uint>();

// Fill the histogram with data
for (uint i = 0; i < numOfInputDataPoints; i++)
{
    // Grab a result from my algorithm
    uint numOfIterationsForSolution = MyAlorithm.Run();

    // Add the number to the histogram
    hist.IncrementCount( numOfIterationsForSolution );
}

// Report the results
foreach (KeyValuePair<uint, uint> histEntry in hist.AsEnumerable())
{
    Console.WriteLine("{0} occurred {1} times", histEntry.Key, histEntry.Value);
}
Run Code Online (Sandbox Code Playgroud)

花了一些时间来研究如何使它成为通用的(首先我只是覆盖了SortedDictionary构造函数,这意味着你只能将它用于uint键).

  • 你能想出一种扩展这种方法来处理大于1的垃圾箱的好方法吗? (2认同)

ken*_*ken 5

您可以使用 Linq:

var items = new[] {5, 6, 1, 2, 3, 1, 5, 2};
items
    .GroupBy(i => i)
    .Select(g => new {
        Item = g.Key,
        Count = g.Count()
    })
    .OrderBy(g => g.Item)
    .ToList()
    .ForEach(g => {
        Console.WriteLine("{0} occurred {1} times", g.Item, g.Count);
    });
Run Code Online (Sandbox Code Playgroud)