Is there a way to do a segmented histrogram with linq? I've seen several examples where you can count the number of occurances of a particular object. Is it possible to create a linq based historgram that counts the number of occurances of a series of objects between two values?
I don't know how you would group by a range of items to create the buckets neccesary for a histogram? Suppose a start bound and a width are used to create the range.
You would need to iterate through the number array and group each number by whether it was <= Upper Bound and > Lower Bound. Then you would just sum each group. I have no idea how to accomplish the group by part
Something like this?
Random randF = new Random();
List<double> nums = new List<double>();
for (int i = 0; i < 100000; i++)
{
nums.Add(randF.NextDouble()*100);
}
double fromXF = 30;
double toXF = 80;
int groupCount = 10; // number of groups between values
var histF = from i in nums
let groupKeyF = ((i-fromXF)/(toXF-fromXF)*groupCount) // even distribution of "groupCount" groups between fromXF and toXF, simple math, really
where groupKeyF >= 0 && groupKeyF < groupCount // only groups we want
let groupKey = (int)groupKeyF // clamp it to only group number
group i by groupKey into gr // group it according to group number
orderby gr.Key
select new { Value = gr.Key, Count = gr.Count() };
foreach (var item in histF)
{
Console.WriteLine("Group number: " + item.Value + ", Count: " + item.Count);
}
You could do something like:
var groups = input.GroupBy(x => (int)((x.value - start)/width));
which creates an integer value for each bar and groups by that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With