查找numpy数组元素的范围

时间:2022-08-09 21:23:30

This is a very simple question:

这是一个非常简单的问题:

I have a numpy array of 94 x 155:

我有一个94 x 155的numpy数组:

a = [1  2  20  68  210  290..
     2  33 34  55  230  340..
     .. .. ... .. ... .....]

I want to calculate the range of each row..so that I get 94 ranges in a result. I tried looking for numpy.range function, which I don't think exists. If this can be done through a loop then its also good.

我想计算每一行的范围。因此我得到了94个范围。我试着寻找numpy.range函数,我觉得不存在。如果这可以通过循环完成,那么它也很好。

For example: Like we have numpy.mean() function, in which, if we set the axis parameter as 1, then it returns the mean for each row in the Nd array..

例如:就像我们有numpy.mean()函数一样,如果我们将axis参数设置为1,那么它返回Nd数组中每一行的平均值。

2 个解决方案

#1


24  

I think np.ptp might do what you want:

我认为np.ptp可能会做你想要的:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ptp.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ptp.html

r = np.ptp(a,axis=1)

where r is your range array.

其中r是你的范围数组。

#2


4  

Try this:

尝试这个:

def range(x, axis=0):
    return np.max(x, axis=axis) - np.min(x, axis=axis)

#1


24  

I think np.ptp might do what you want:

我认为np.ptp可能会做你想要的:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ptp.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ptp.html

r = np.ptp(a,axis=1)

where r is your range array.

其中r是你的范围数组。

#2


4  

Try this:

尝试这个:

def range(x, axis=0):
    return np.max(x, axis=axis) - np.min(x, axis=axis)