gpt4 book ai didi

python - Scipy:Argrelmax 在 3d 数组中沿维度查找 Maxmima

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:00 24 4
gpt4 key购买 nike

我想在 3D 数组 (100,1000,1000) 中沿第一维找到相对局部最大值。我正在使用 scipy.signal 中的 argrelmax 函数

result = argrelmax(data, axis = 0, order = 20)

我无法真正理解输出,我希望我的数据量中每个一维切片的相对最大值。相反,我得到 3 个具有 1653179 个值的元组。我怎样才能将它们恢复到我原来的形状?

最佳答案

argrelmax 的返回值是相对最大值的数组索引。例如,

In [47]: np.random.seed(12345)

In [48]: x = np.random.randint(0, 10, size=(10, 3))

In [49]: x
Out[49]:
array([[2, 5, 1],
[4, 9, 5],
[2, 1, 6],
[1, 9, 7],
[6, 0, 2],
[9, 1, 2],
[6, 7, 7],
[7, 8, 7],
[1, 7, 4],
[0, 3, 5]])

In [50]: i, j = argrelmax(x, axis=0)

In [51]: i
Out[51]: array([1, 1, 3, 3, 5, 7, 7])

In [52]: j
Out[52]: array([0, 1, 1, 2, 0, 0, 1])

i 包含行,j 包含相对最大值的列。例如。 x[1, 0] 保存值 4,这是第一列中的相对最大值,x[1, 1] 保存值 9,这是第二列中的相对最大值。

要逐列处理局部最大值,您可以这样做:

In [56]: for col in range(x.shape[1]):
....: mask = j == col
....: print("Column:", col, " Position of local max:", i[mask])
....:
Column: 0 Position of local max: [1 5 7]
Column: 1 Position of local max: [1 3 7]
Column: 2 Position of local max: [3]

这同样适用于您的 3D 阵列。下面以一个小得多的 3D 数组为例:

In [73]: np.random.seed(12345)

In [74]: data = np.random.randint(0, 10, size=(10, 3, 2))

In [75]: i, j, k = argrelmax(data, axis=0)

要获取切片 data[:, 0, 0] 中的相对最大值的位置,您可以这样做:

In [76]: mask00 = (j == 0) & (k == 0)

In [77]: i[mask00]
Out[77]: array([5, 8])

检查这些是局部最大值的索引:

In [78]: data[:, 0, 0]
Out[78]: array([2, 2, 6, 6, 1, 7, 3, 0, 8, 7])

关于python - Scipy:Argrelmax 在 3d 数组中沿维度查找 Maxmima,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36335846/

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