比numpy更快吗?

发布于 2025-01-23 10:08:30 字数 306 浏览 2 评论 0原文

我想到的是否比NP的功能更快。 ATM我正在将两个数组与NP进行比较。之后,IM浏览列表检查值[a]的位置。我认为必须有一种更快的方法,但我目前找不到。对数组进行排序是没有选项。

代码看起来像这样:

a = np.array([1, 2, 3])
b = np.array([2, 1, 8])
over = np.less(a, b)
# over = [True, False, True]
pos = np.where(over == True)[0]
# pos = [0, 2]
x = b[pos]
# x = [2,8]

i was think about if there is a faster way than the np.less functionality. Atm i'm comparing two arrays with np.less an get something like [true, true, false], after that im checking with a=np.where(x == True) to get the position of the true. After that im going through the list checking where values[a]. In my opinion there have to be a much faster way but i currently can't find on. Sorting the array is no option.

The code is is looking like this:

a = np.array([1, 2, 3])
b = np.array([2, 1, 8])
over = np.less(a, b)
# over = [True, False, True]
pos = np.where(over == True)[0]
# pos = [0, 2]
x = b[pos]
# x = [2,8]

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

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

发布评论

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

评论(1

深者入戏 2025-01-30 10:08:30

AS ali_sh指出,只需使用条件来获取值即可。

import numpy as np

a = np.array([1, 2, 3])
b = np.array([2, 1, 8])

b[np.less(a, b)]
# array([2, 8])
list(b[np.less(a, b)])  # for a list result instead of np arr
# [2, 8]

# or just
b[a < b]
# array([2, 8])

问题不是np.less的速度,而是您获得的额外步骤。所有这些几乎是速度快速的;只需要删除不必要的步骤即可。

即使您想在上保存在中的true/false结果:

over = np.less(a, b)
b[over]
# array([2, 8])

over = a < b
b[over]
# array([2, 8])

As Ali_Sh pointed out, just use the condition to fetch the values.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([2, 1, 8])

b[np.less(a, b)]
# array([2, 8])
list(b[np.less(a, b)])  # for a list result instead of np arr
# [2, 8]

# or just
b[a < b]
# array([2, 8])

The issue is not the speed of np.less but the extra steps you've got. All of which are nearly-C-speed fast; just need to remove the unnecessary steps.

And even if you wanted to save the True/False results in over:

over = np.less(a, b)
b[over]
# array([2, 8])

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