本文介绍了scipy中的简单计算:最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我要计算最小值.用星号显示的五个元素中的一个最小值,依此类推.因此,将存在25个最小值,这将导致5 * 5数组大小.我尝试如下:

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

但是,出现以下错误.任何想法都可以.

IndexError: arrays used as indices must be of integer (or boolean) type
解决方案

注意:我的python现在处于炸状态,因此我无法对其进行测试.如果我做错了事,请告诉我.

您的数据在重整为(5,25)时,都沿轴0增加.这意味着每个argrelmin(data,axis = 0)都没有相对最小值,除非您指定mode ='wrap'.在这种情况下,每个向量的第一个元素将是最小值.但是,当向量中的所有元素均为NaN(在数据集中确实存在)或最后一个元素为NaN时,不一定是正确的.由于argrelmin使用np.less进行比较(对于与NaN进行的所有比较均返回False),所以我猜想无论如何修改函数调用,这些行中都没有最小值(尽管我没有尝试过). /p>

总结:argrelmin返回空数组的原因是因为沿数据集的第一个轴没有相对最小值.另外请注意,除非可以做一些非常具体的假设(例如,数据在增加,使用自动换行且不使用nans),否则我们不保证您的数据集中有25个最小值.

这也是为什么在沿轴1施加argrelmin时会收到27个元素的原因-有27个相对最小值.

例如

常规:

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.

自动换行:

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.

注意:看起来您可以使自己的比较器处理NaN并将其传递给argrelextrema,但是您仍然必须处理所有NaN情况,等等.

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.

这篇关于scipy中的简单计算:最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:07