scipy中2d数组的最小值计算

时间:2014-05-07 03:36:34

标签: python numpy scipy

import numpy as np
from scipy import signal

data = np.array([[[13, 2, 1, np.nan, np.nan],
              [22, 1, 1, 4, 4],
              [4, 2, 3, 3, 4],
              [1, 1, 4, 1, 5],
              [2, 4, 5, 2, 1]],

         [[17, 7, 10, 6, np.nan],
          [np.nan, 7, 8, 6, 9],
          [6, 10, 9, 8, 10],
          [6, 8, 7, 10, 8],
          [10, 9, 9, 10, 8]],

         [[16, 7, 10, np.nan, np.nan],
          [19, 19, 8, 6, 9],
          [6, 10, 9, 8, 10],
          [6, 8, 7, 10, 8],
          [10, 9, 9, 10, 8]],

         [[61, 7, 10, 6, np.nan],
          [19, 21, 8, 6, 9],
          [6, 10, 9, 8, 10],
          [6, 8, 7, 10, 8],
          [10, 9, 9, 10, 8]],

        [[51, 7, 10, 6, np.nan],
          [19, 21, 8, 6, 9],
          [6, 10, 9, 8, 10],
          [6, 8, 7, 10, 8],
          [10, 9, 9, 10, 8]],

        [[34, 7, 10, 6, np.nan],
          [19, 21, 8, 6, 9],
          [6, 10, 9, 8, 10],
          [6, 8, 7, 10, 8],
          [10, 9, 9, 10, 8]],

         [[12, 14, 12, 15, np.nan],
          [19, 11, 14, 14, 11],
          [13, 13, 16, 15, 11],
          [14, 15, 14, 16, 14],
          [13, 15, 11, 11, 14]]])

data = data.reshape(7,25)
minima = data[signal.argrelmin(data,axis=0,order=1)]
print minima

但它只产生了一个与:

相同的结果
test = np.array([13,17,16,61,51,34,12])
print test[signal.argrelmin(test)]

因此,上述方法只能为每列产生第一个元素的结果。 如何获得其他24个元素的结果?

1 个答案:

答案 0 :(得分:1)

这里的问题几乎与我在my answer to your previous post中描述的完全相同。问题是数据数组中的轴0没有其他相对最小值。您已从函数中收到正确的输出。

例如,在第二栏中:

2,7,7,7,7,7,14

argrelmin了解它没有相对最小值。如果您希望2为相对最小值,则可以将mode ='wrap'参数添加到函数调用中。但是,要小心NaN(再次,请参阅我对原始问题的回答)。