与精确的两个numpy矩阵相交的浮子行

发布于 2025-01-28 21:53:33 字数 171 浏览 2 评论 0原文

我正在寻找B中的行,该行与一个示例中的任何行都接近

: EPS = 0.1

a = [[1.22,1.33],[1.45,1.66]]

b = [[1.25,1.34],[1.77,1.66],[1.44,1.67]]

结果:[[1.22,1.33] ,1.66]]]

I'm looking for rows in B which are close to any of the rows in A

example:
eps = 0.1

A = [[1.22, 1.33], [1.45, 1.66]]

B = [[1.25, 1.34], [1.77, 1.66], [1.44, 1.67]]

Result: [[1.22, 1.33], [1.45, 1.66]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

何其悲哀 2025-02-04 21:53:33

如果您想过滤到a接近 元素中的元素,则可以使用b中的任何元素,则可以使用broadcast瓷砖进行详尽的检查:

import numpy as np

eps = .1
A = np.array([[1.22, 1.33], [1.45, 1.66]])
B = np.array([[1.25, 1.34], [1.77, 1.66], [1.44, 1.67]])


# broadcast A based on the shape of B
A_ext = np.broadcast_to(A, (B.shape[0],) + A.shape)

# tile B and reshape, this will allow comparison of all elements in A to all elements in B
B_ext = np.tile(B, A.shape[0]).reshape(A_ext.shape)

# create the boolean array
A_bool = np.abs(A_ext - B_ext) < eps

# reduce array to match number of elements in A
# .all() will result in an array representing which elements in A are close to each element in B
# .any() represents if A is close to any of the elements in B
A_mask = A_bool.all(axis = -1).any(axis = 0)

# final result
A[A_mask]

If you are looking to filter to elements in A which are close to any element in B, you can use broadcast and tile to do an exhaustive check:

import numpy as np

eps = .1
A = np.array([[1.22, 1.33], [1.45, 1.66]])
B = np.array([[1.25, 1.34], [1.77, 1.66], [1.44, 1.67]])


# broadcast A based on the shape of B
A_ext = np.broadcast_to(A, (B.shape[0],) + A.shape)

# tile B and reshape, this will allow comparison of all elements in A to all elements in B
B_ext = np.tile(B, A.shape[0]).reshape(A_ext.shape)

# create the boolean array
A_bool = np.abs(A_ext - B_ext) < eps

# reduce array to match number of elements in A
# .all() will result in an array representing which elements in A are close to each element in B
# .any() represents if A is close to any of the elements in B
A_mask = A_bool.all(axis = -1).any(axis = 0)

# final result
A[A_mask]
初熏 2025-02-04 21:53:33

我同意pantless教授的观点'评论< /a>并将在此处扩展其使用:

您的数组看起来像:

# array A
1.22 1.33
1.45 1.66

# array B
1.25 1.34
1.77 1.66

您所需的结果(按OP)匹配到a [0]1.22 1.22 1.33。这表明您希望返回数组中的行数组 a ,其中一行中的所有元素均为&lt; EPS

# array C
np.abs(A[0] - B[0]) # --> True  True
np.abs(A[1] - B[1]) # --> False True

布尔索引使用:

>>> A[(np.abs(A-B) < eps).all(axis = 1)]

分解这一行:

>>> np.abs(A-B)
0.03 0.01
0.32 0.00
>>> np.abs(A-B) < eps
True  True
False True
# notice this matches the comments above
>>> (np.abs(A-B) < eps).all(axis = 1)
True False

(np.abs(ab)(ab)(ab)&lt; eps)返回一系列新阵列布尔值,以及.all(axis = 1)沿每一行的第一个轴(SO列)检查,以查看每行的所有元素是否true。由于数组的第一行c全部true,因此它返回true;由于其false true,因此第二行不保留这一点,因此它返回false。您剩下的是形状(n,)的数组。

因此,现在最后的细分是:

>>> A[(np.abs(A-B) < eps).all(axis = 1)]
1.22 .133
# since this is access A like A[(True, False)]

WRT您的最新评论,您不能这样做,因为numpy的 broadcasting 规则。 These rules are pretty similar to standard 矩阵乘法规则。因此,您不能服用2x2矩阵,并乘以3x2,因为内部尺寸不起作用。

I agree with Professor Pantsless' comment and would expand on it's use here:

Your arrays look like so:

# array A
1.22 1.33
1.45 1.66

# array B
1.25 1.34
1.77 1.66

Your desired result (per your OP) matches to A[0], or 1.22 1.33. This indicates that you wish to return an array of the rows inside array A in which all elements in a row are < eps when compared to the same indexed row of array 'B':

# array C
np.abs(A[0] - B[0]) # --> True  True
np.abs(A[1] - B[1]) # --> False True

This is achievable with a boolean index achieved using:

>>> A[(np.abs(A-B) < eps).all(axis = 1)]

Breaking down this line:

>>> np.abs(A-B)
0.03 0.01
0.32 0.00
>>> np.abs(A-B) < eps
True  True
False True
# notice this matches the comments above
>>> (np.abs(A-B) < eps).all(axis = 1)
True False

The (np.abs(A-B) < eps) returns a new array of booleans, and the .all(axis = 1) checks along the first axis (so column wise) of each row to see if all elements per row are True. Since the first row of array C is all True, it returns True; this does not hold for the second row as its False True so it returns False. What you're left with is an array of shape (N, ).

So now the final breakdown is:

>>> A[(np.abs(A-B) < eps).all(axis = 1)]
1.22 .133
# since this is access A like A[(True, False)]

w.r.t. your latest comment, you cannot do that because of NumPy's broadcasting rules. These rules are pretty similar to standard matrix multiplication rules. So you cannot take a 2x2 matrix and multiply by a 3x2 because the inner dimensions don't work.

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