有没有办法用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); }更多推荐
发布评论