C# 如何规范化 int 值列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1226587/
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
How to normalize a list of int values
提问by hbruce
I have a list of int values:
我有一个 int 值列表:
List<int> histogram;
List<int> histogram;
How do I normalize all values so that the max value in the list is always 100?
如何标准化所有值,以便列表中的最大值始终为 100?
采纳答案by Amber
Iterate though, find the maximum value (call it MAXVAL
), then iterate through once more and multiply every value in the list by (100/MAXVAL)
.
迭代,找到最大值(称为MAXVAL
),然后再次迭代并将列表中的每个值乘以(100/MAXVAL)
。
var ratio = 100.0 / list.Max();
var normalizedList = list.Select(i => i * ratio).ToList();
回答by Bevan
If you have a list of strictly positive numbers, then Dav's answer will suit you fine.
如果你有一个严格的正数列表,那么 Dav 的答案会很适合你。
If the list can be any numbers at all, then you need to also normalise to a lowerbound.
如果列表可以是任何数字,那么您还需要标准化为下限。
Assuming an upper bound of 100 and a lower bound of 0, you'll want something like this ...
假设上限为 100,下限为 0,你会想要这样的东西......
var max = list.Max();
var min = list.Min();
var range = (double)(max - min);
var normalised
= list.Select( i => 100 * (i - min)/range)
.ToList();
Handling the case where min == max
is left as an exercise for the reader ...
处理min == max
留给读者练习的案例......
回答by zomf
To normalize a set of numbers that may contain negative values,
and to define the normalized scale's range:
对可能包含负值的一组数字
进行归一化,并定义归一化尺度的范围:
List<int> list = new List<int>{-5,-4,-3,-2,-1,0,1,2,3,4,5};
double scaleMin = -1; //the normalized minimum desired
double scaleMax = 1; //the normalized maximum desired
double valueMax = list.Max();
double valueMin = list.Min();
double valueRange = valueMax - valueMin;
double scaleRange = scaleMax - scaleMin;
IEnumerable<double> normalized =
list.Select (i =>
((scaleRange * (i - valueMin))
/ valueRange)
+ scaleMin);