二维坐标标准化
我需要实现一个标准化坐标的函数。我将标准化定义为(如果我错了,请建议一个更好的术语):
将数据集的条目从其自然范围映射到 0 到 1 之间的值。
现在这在一维中很容易:
static List<float> Normalize(float[] nums)
{
float max = Max(nums);
float min = Min(nums);
float delta = max - min;
List<float> li = new List<float>();
foreach (float i in nums)
{
li.Add((i - min) / delta);
}
return li;
}
我还需要一个 2D 版本,并且必须保持纵横比不变。但我在计算数学时遇到了一些麻烦。
尽管发布的代码是 C# 语言,但答案不一定是 C# 语言。
提前致谢。 :)
I need to implement a function which normalizes coordinates. I define normalize as (please suggest a better term if Im wrong):
Mapping entries of a data set from their natural range to values between 0 and 1.
Now this was easy in one dimension:
static List<float> Normalize(float[] nums)
{
float max = Max(nums);
float min = Min(nums);
float delta = max - min;
List<float> li = new List<float>();
foreach (float i in nums)
{
li.Add((i - min) / delta);
}
return li;
}
I need a 2D version as well and that one has to keep the aspect ratio intact. But Im having some troubles figuring out the math.
Although the code posted is in C# the answers need not to be.
Thanks in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将我的回复作为答案发布,因为我没有足够的积分来发表评论。
我对这个问题的解释:我们如何标准化二维空间中一组点的坐标?
标准化操作涉及“移位和缩放”操作。在一维空间的情况下,这是相当简单和直观的(正如@Mizipzor指出的)。
在本例中,我们首先将值移动距离 minX,然后按 ( 给出的范围)缩放 maxX-minX)。 移位操作确保最小值移动到 0,而缩放操作压缩分布,使得分布的上限为 1
对于 2d ,仅除以最大维度是不够的。为什么?
考虑只有 2 点的简化情况,如下所示。
任何维度的最大值是点B的Y值和这个10000。
X和Y值都为0和1。但是,归一化值的分布很不均匀。最小值仅为 0.5。理想情况下,该值应接近 0。
规范化 2d 坐标的首选方法
为了获得更均匀的分布,我们应该围绕所有 X 值的最小值和所有 Y 值的最小值进行“移位”操作。这也可以围绕 X 的平均值和 Y 的平均值来完成。考虑到上面的例子,
第1步 - 移位操作
第2步 - 缩放操作
缩小我们需要的值一些最大值。我们可以使用长度为 2000 的对角线 AB
当点超过 2 个时会发生什么?
该方法仍然相似。我们找到适合所有点的最小边界框的坐标。
。
如果我们有两个以上的维度,这个逻辑会如何变化?
概念保持不变。
- 我们找到每个维度(X、Y、Z)的最小值
- 我们找到每个维度(X、Y、Z)的最大值
- 计算对角线的长度作为比例因子
- 使用最小值来移动原点。
I am posting my response as an answer because I do not have enough points to make a comment.
My interpretation of the question: How do we normalize the coordinates of a set of points in 2 dimensional space?
A normalization operation involves a "shift and scale" operation. In case of 1 dimensional space this is fairly easy and intuitive (as pointed out by @Mizipzor).
In this case we are first shifing the value by a distance of minX and then scaling it by the range which is given by (maxX-minX). The shift operation ensures that the minimum moves to 0 and the scale operation squashes the distribution such that the distribution has an upper limit of 1
In case of 2d , simply dividing by the largest dimension is not enought. Why?
Consider the simplified case with just 2 points as shown below.
The maximum value of any dimension is the Y value of point B and this 10000.
The X and Y values are all with 0 and 1. However, the distribution of the normalized values is far from uniform. The minimum value is just 0.5. Ideally this should be closer to 0.
Preferred approach for normalizing 2d coordinates
To get a more even distribution we should do a "shift" operation around the minimum of all X values and minimum of all Y values. This could be done around the mean of X and mean of Y as well. Considering the above example,
Step 1 - Shift operation
Step 2 - Scale operation
To scale down the values we need some maximum. We could use the diagonal AB whose length is 2000
What happens when there are more than 2 points?
The approach remains similar. We find the coordinates of the smallest bounding box which fits all the points.
.
How does this logic change if we have more than 2 dimensions?
The concept remains the same.
- We find the minimum value in each of the dimensions (X,Y,Z)
- We find the maximum value in each of the dimensions (X,Y,Z)
- Compute the length of the diagonal as a scaling factor
- Use the minimum values to shift the origin.
看来您希望每个向量(1D、2D 或 ND)的长度
<= 1
。如果这是唯一的要求,您只需将每个向量除以最长向量的长度即可。
这将使结果列表中最长的向量的长度为 1。
但这并不等同于当前一维情况的代码,因为您无法在平面上的一组点中找到最小值或最大值。因此,没有增量。
It seems you want each vector (1D, 2D or ND) to have length
<= 1
.If that's the only requirement, you can just divide each vector by the length of the longest one.
That will make the longest vector in result list to have length 1.
But this won't be equivalent of your current code for 1-dimensional case, as you can't find minimum or maximum in a set of points on the plane. Thus, no
delta
.简单的想法:找出哪个维度更大并在这个维度上归一化。第二维可以通过使用比率来计算。这样比率就保持不变,并且您的值介于 0 和 1 之间。
Simple idea: Find out which dimension is bigger and normalize in this dimension. The second dimension can be computed by using the ratio. This way the ratio is kept and your values are between 0 and 1.