C#中整数数据的简单直方图生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/926067/
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
Simple histogram generation of integer data in C#
提问by Jon Cage
As part of a test bench I'm building, I'm looking for a simple class to calculate a histogram of integer values (number of iterations taken for an algorithm to solve a problem). The answer should be called something like this:
作为我正在构建的测试平台的一部分,我正在寻找一个简单的类来计算整数值的直方图(为解决问题的算法所采用的迭代次数)。答案应该是这样的:
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] );
}
I was suprised a bit of googling didn't turn up a neat solution but maybe I didn't search for the right things. Is there a generic solution out there or is it worth rolling my own?
我很惊讶谷歌搜索没有找到一个巧妙的解决方案,但也许我没有搜索正确的东西。有没有通用的解决方案,还是值得我自己推出?
采纳答案by Steef
You could use SortedDictionary
你可以使用 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);
}
This will leave out empty bins, though
不过,这会留下空的垃圾箱
回答by Jon Cage
Based on BastardSaint's suggestion I came up with a neat and fairly generic wrapper:
根据 BastardSaint 的建议,我想出了一个简洁且相当通用的包装器:
public class Histogram<TVal> : SortedDictionary<TVal, uint>
{
public void IncrementCount(TVal binToIncrement)
{
if (ContainsKey(binToIncrement))
{
this[binToIncrement]++;
}
else
{
Add(binToIncrement, 1);
}
}
}
So now I can do:
所以现在我可以这样做:
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);
}
Took me a while to work out how to make it generic (to begin with I just overrode the SortedDictionary
constructor which meant you could only use it for uint
keys).
我花了一段时间才弄清楚如何使它通用(首先我只是覆盖了SortedDictionary
构造函数,这意味着您只能将它用于uint
键)。
回答by ken
You can use Linq:
您可以使用 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);
});
回答by Mugen
My implementation of a simple extension method to create a histogram:
我实现了一个简单的扩展方法来创建直方图:
public static IReadOnlyDictionary<T, int> ToHistogram<T>(this IEnumerable<T> enumerable)
=> enumerable.GroupBy(item => item).ToDictionary(grouping => grouping.Key, grouping => grouping.Count());