与精确的两个numpy矩阵相交的浮子行
我正在寻找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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想过滤到
a
接近 元素中的元素,则可以使用b
中的任何元素,则可以使用broadcast
和瓷砖
进行详尽的检查:If you are looking to filter to elements in
A
which are close to any element inB
, you can usebroadcast
andtile
to do an exhaustive check:我同意pantless教授的观点'评论< /a>并将在此处扩展其使用:
您的数组看起来像:
您所需的结果(按OP)匹配到
a [0]
或1.22 1.22 1.33
。这表明您希望返回数组中的行数组 a ,其中一行中的所有元素均为&lt; EPS
与布尔索引使用:
分解这一行:
(np.abs(ab)(ab)(ab)&lt; eps)
返回一系列新阵列布尔值,以及.all(axis = 1)
沿每一行的第一个轴(SO列)检查,以查看每行的所有元素是否true
。由于数组的第一行c
全部true
,因此它返回true
;由于其false true
,因此第二行不保留这一点,因此它返回false
。您剩下的是形状(n,)
的数组。因此,现在最后的细分是:
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:
Your desired result (per your OP) matches to
A[0]
, or1.22 1.33
. This indicates that you wish to return an array of the rows inside arrayA
in which all elements in a row are< eps
when compared to the same indexed row of array 'B':This is achievable with a boolean index achieved using:
Breaking down this line:
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 areTrue
. Since the first row of arrayC
is allTrue
, it returnsTrue
; this does not hold for the second row as itsFalse True
so it returnsFalse
. What you're left with is an array of shape(N, )
.So now the final breakdown is:
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 a3x2
because the inner dimensions don't work.