質問

import numpy as np
from scipy import signal

data = np.array([[[*3, 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]],

             [[*6, 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]],

             [[*6, 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]],

             [[*6, 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]]])

I want to calculate minima. One minima from five elements which are shown with asterix, and so on. Hence, there will be 25 minima values which are to resulted in 5*5 array size. I tried as follows:

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

But, following error. Any idea please.

IndexError: arrays used as indices must be of integer (or boolean) type
役に立ちましたか?

解決

NOTE: My python is on the fritz right now, so I haven't been able to test this. Let me know if I've got something wrong.

Your data, when reshaped to (5,25), is all increasing along axis 0. This means that there are NO relative minima per argrelmin(data,axis=0) unless you specify mode='wrap'. In that case, the first element of each vector would be the minimum. However, this is not necessarily true when all elements in a vector are NaNs (which does occur in your dataset) or if the last element is a NaN. Since argrelmin uses np.less for comparison (which returns False for all comparisons with NaNs), I'm guessing you will find no minima in those rows regardless of how you modify the function call (although I haven't tried it).

To summarize: the reason argrelmin returns an empty array is because there are no relative minima along the first axis of your dataset. Also note that you are NOT guaranteed to have 25 minima values in your dataset unless can make some pretty specific assumptions (e.g., data is increasing, using wrap, and no nans).

This is also why you are receiving 27 elements back when applying argrelmin along axis 1 - there are 27 relative minima.

e.g.,

regular:

3., 2., 1., nan, nan, 22., 1., 1., 4., 4., 4., 2., 3., 3., 4., 1., 1., 4., 1., 5., 2., 4., 5., 2., 1.

with wrap:

3., 2., 1., nan, nan, 22., 1., 1., 4., 4., 4., 2., 3., 3., 4., 1., 1., 4., 1., 5., 2., 4., 5., 2., 1.

Note: It looks like you could make your own comparitor to handle NaNs and pass that to argrelextrema, but you'd still have to deal with all NaN cases, etc.

他のヒント

Did you check what signal.argrelmin() actually returned for your (reshaped) data? You need to choose the axis properly. For a simple example,

data = np.array([[19, 11, 14, 14, 10],[19,21,12,14,11]])
scipy.signal.argrelmin(data)

returns

(array([], dtype=float64), array([], dtype=float64))

which then gives the IndexError you reference.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top