2016-03-31 27 views
0

我想在第一维中找到三维数组(100,1000,1000)中的相对局部最大值。我使用的argrelmax功能从scipy.signalScipy:Argrelmax在3D阵列中沿维度查找Maxmima

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

我真的不能使输出的感觉,我希望像每个1D切片相对最大值通过我的数据量。相反,我得到3个元组值为1653179。我如何将它们与我原来的形状联系起来?

回答

1

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阵列。下面以更小的三维阵列为例:

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]) 
+0

啊,现在完全有意义了。在我的情况下,他们只是返回每个最大值的三维坐标。 – Raubtaube