与Linq的Histrogram(Histrogram with Linq)

编程入门 行业动态 更新时间:2024-10-28 04:20:56
Linq的Histrogram(Histrogram with Linq)

有没有办法用linq做一个分段的histrogram? 我已经看过几个例子,你可以计算特定对象的出现次数。 是否有可能创建一个基于linq的历史图,它计算两个值之间一系列对象的出现次数?

我不知道如何按一系列项目分组来创建直方图所需的存储桶? 假设使用起始边界和宽度来创建范围。

您需要遍历数字数组并按每个数字分组<=上限和>下限。 然后你只需要对每个组进行求和。 我不知道如何完成这个小组

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

最满意答案

像这样的东西?

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); }

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); }

更多推荐

本文发布于:2023-08-04 05:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Linq   Histrogram

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!