二维坐标标准化

发布于 2024-09-26 02:59:16 字数 596 浏览 2 评论 0原文

我需要实现一个标准化坐标的函数。我将标准化定义为(如果我错了,请建议一个更好的术语):

将数据集的条目从其自然范围映射到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮世清欢 2024-10-03 02:59:16

我将我的回复作为答案发布,因为我没有足够的积分来发表评论。

我对这个问题的解释:我们如何标准化二维空间中一组点的坐标?

标准化操作涉及“移位和缩放”操作。在一维空间的情况下,这是相当简单和直观的(正如@Mizipzor指出的)。

normalizedX=(originalX-minX)/(maxX-minX)

在本例中,我们首先将值移动距离 minX,然后按 ( 给出的范围)缩放 maxX-minX)移位操作确保最小值移动到 0,而缩放操作压缩分布,使得分布的上限为 1

对于 2d ,仅除以最大维度是不够的。为什么?

考虑只有 2 点的简化情况,如下所示。
输入图像描述这里
任何维度的最大值是点BY值和这个10000

Coordinates of normalized A=>5000/10000,8000/10000 ,i.e 0.5,0.8
Coordinates of normalized A=>7000/10000,10000/10000 ,i.e 0.7,1.0

X和Y值都为0和1。但是,归一化值的分布很不均匀。最小值仅为 0.5。理想情况下,该值应接近 0。

规范化 2d 坐标的首选方法

为了获得更均匀的分布,我们应该围绕所有 X 值的最小值和所有 Y 值的最小值进行“移位”操作。这也可以围绕 X 的平均值和 Y 的平均值来完成。考虑到上面的例子,

  • 所有X的最小值是5000,
  • 所有Y的最小值是8000

第1步 - 移位操作

A=>(5000-5000,8000-8000), i.e (0,0)
B=>(7000-5000,10000-8000), i.e. (2000,2000)

第2步 - 缩放操作

缩小我们需要的值一些最大值。我们可以使用长度为 2000 的对角线 AB

A=>(0/2000,0/2000),     i.e. (0,0)
B=>(2000/2000,2000/2000)i.e. (1,1)

当点超过 2 个时会发生什么?
输入图像描述这里
该方法仍然相似。我们找到适合所有点的最小边界框的坐标。

  • 我们从所有点中找到X的最小值(MinX)和Y的最小值(MinY)并进行移位操作。这会将原点更改为边界框的左下角。
  • 我们从所有点中找到 X 的最大值 (MaxX) 和 Y 的最大值 (MaxY)。
  • 我们计算连接 (MinX,MinY) 和 (MaxX,MaxY) 的对角线长度,并使用该值进行缩放操作。

length of diagonal=sqrt((maxX-minX)*(maxX-minX) + (maxY-minY)*(maxY-minY))   

normalized X = (originalX - minX)/(length of diagonal) 
normalized Y = (originalY - minY)/(length of diagonal)

如果我们有两个以上的维度,这个逻辑会如何变化?

概念保持不变。
- 我们找到每个维度(X、Y、Z)的最小值
- 我们找到每个维度(X、Y、Z)的最大值
- 计算对角线的长度作为比例因子
- 使用最小值来移动原点。

length of diagonal=sqrt((maxX-minX)*(maxX-minX)+(maxY-minY)*(maxY-minY)+(maxZ-minZ)*(maxZ-minZ))

normalized X = (originalX - minX)/(length of diagonal) 
normalized Y = (originalY - minY)/(length of diagonal)
normalized Z = (originalZ - minZ)/(length of diagonal)

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).

normalizedX=(originalX-minX)/(maxX-minX)

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.
enter image description here
The maximum value of any dimension is the Y value of point B and this 10000.

Coordinates of normalized A=>5000/10000,8000/10000 ,i.e 0.5,0.8
Coordinates of normalized A=>7000/10000,10000/10000 ,i.e 0.7,1.0

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,

  • the minimum of all X is 5000
  • the minimum of all Y is 8000

Step 1 - Shift operation

A=>(5000-5000,8000-8000), i.e (0,0)
B=>(7000-5000,10000-8000), i.e. (2000,2000)

Step 2 - Scale operation

To scale down the values we need some maximum. We could use the diagonal AB whose length is 2000

A=>(0/2000,0/2000),     i.e. (0,0)
B=>(2000/2000,2000/2000)i.e. (1,1)

What happens when there are more than 2 points?
enter image description here
The approach remains similar. We find the coordinates of the smallest bounding box which fits all the points.

  • We find the minimum value of X (MinX) and minimum value of Y (MinY) from all the points and do a shift operation. This changes the origin to the lower left corner of the bounding box.
  • We find the maximum value of X (MaxX) and maximum value of Y (MaxY) from all the points.
  • We calculate the length of the diagonal connecting (MinX,MinY) and (MaxX,MaxY) and use this value to do a scale operation.

.

length of diagonal=sqrt((maxX-minX)*(maxX-minX) + (maxY-minY)*(maxY-minY))   

normalized X = (originalX - minX)/(length of diagonal) 
normalized Y = (originalY - minY)/(length of diagonal)

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.

length of diagonal=sqrt((maxX-minX)*(maxX-minX)+(maxY-minY)*(maxY-minY)+(maxZ-minZ)*(maxZ-minZ))

normalized X = (originalX - minX)/(length of diagonal) 
normalized Y = (originalY - minY)/(length of diagonal)
normalized Z = (originalZ - minZ)/(length of diagonal)
司马昭之心 2024-10-03 02:59:16

看来您希望每个向量(1D、2D 或 ND)的长度 <= 1
如果这是唯一的要求,您只需将每个向量除以最长向量的长度即可。

double max = maximum (|vector| for each vector in 'data');
foreach (Vector v : data) {
    li.add(v / max);
}

这将使结果列表中最长的向量的长度为 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.

double max = maximum (|vector| for each vector in 'data');
foreach (Vector v : data) {
    li.add(v / max);
}

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.

霞映澄塘 2024-10-03 02:59:16

简单的想法:找出哪个维度更大并在这个维度上归一化。第二维可以通过使用比率来计算。这样比率就保持不变,并且您的值介于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文