gpt4 book ai didi

python - 如何让数组中的所有值都落入一个范围内?

转载 作者:行者123 更新时间:2023-12-03 07:51:28 25 4
gpt4 key购买 nike

假设我有一个 NumPy 数组 float s,有正值和负值。我有两个号码,假设它们是 ab , a <= b[a, b]是一个(封闭的)数字范围。

我想让所有数组都落入 [a, b] 范围内,更具体地说,我想用相应的最终值替换范围之外的所有值。

我并不是试图缩放值以使数字适合一个范围,在Python中,这将是:

mina = min(arr)
scale = (b - a) / (max(arr) - mina)
[a + (e - mina) * scale for e in arr]

或者在 NumPy 中:

mina = arr.min()
scale = (b - a) / (arr.max() - mina)
a + (arr - mina) * scale

我正在尝试替换所有低于 a 的值与 a以及所有高于 b 的值与 b ,在保持所有其他值不变的情况下,我可以在 Python 中的单个列表理解中完成此操作:

[e if a <= e <= b else (a if e < a else b) for e in arr]

我可以对两个广播执行相同的操作:

arr[arr < a] = a
arr[arr > b] = b

尽管 NumPy 比 Python 快得多,但上面是两个循环,而不是一个,该方法效率低下,但可以编译。

什么是更快的方法?


我测量过多次,Python确实比预期慢很多:

In [1]: import numpy as np

In [2]: numbers = np.random.random(4096) * 1024

In [3]: %timeit numbers[numbers < 256]
16.1 µs ± 219 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [4]: %timeit numbers[numbers > 512]
20.9 µs ± 526 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [5]: %timeit [e if 256 <= e <= 512 else (256 if e < 256 else 512) for e in numbers]
927 µs ± 101 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [6]: %timeit [e if 256 <= e <= 512 else (256 if e < 256 else 512) for e in numbers.tolist()]
684 µs ± 38.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

编辑

修复了将一系列数字缩放到另一个范围的代码。我问这个问题的时候并没有想太多,因为这不相关,但现在我再想一想,这显然是错误的,所以我改正了。


再次编辑

再想一想,我原来的Python代码效率并不高,只需要两次比较就可以完成,而原来使用了三个:

[a if e < a else (b if e > b else e) for e in arr]

最佳答案

您可以使用np.clip

Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

比广播方式更快。<​​/p>

代码示例:

import numpy as np

arr = np.array([-3, 5, 10, -7, 2, 8, -12, 15])

a = 0
b = 10

new_arr = np.clip(arr, a, b)
print(new_arr)

性能基准

For Array size of 1000
Method 1 (List comprehension) time: 0.0115 seconds
Method 2 (NumPy broadcasts) time: 0.0009 seconds
Method 3 (np.clip()) time: 0.0009 seconds

-----------------------------------------------------------------

For Array size of 10000
Method 1 (List comprehension) time: 0.1137 seconds
Method 2 (NumPy broadcasts) time: 0.0069 seconds
Method 3 (np.clip()) time: 0.0017 seconds

-----------------------------------------------------------------

For Array size of 100000
Method 1 (List comprehension) time: 1.3205 seconds
Method 2 (NumPy broadcasts) time: 0.1152 seconds
Method 3 (np.clip()) time: 0.0107 seconds

-----------------------------------------------------------------

For Array size of 1000000
Method 1 (List comprehension) time: 13.8250 seconds
Method 2 (NumPy broadcasts) time: 1.0064 seconds
Method 3 (np.clip()) time: 0.1973 seconds

enter image description here

关于python - 如何让数组中的所有值都落入一个范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77006611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com