Python 查找numpy数组元素的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12701659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding range of a numpy array elements
提问by khan
I have a NumPy array of size 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 a numpy.rangefunction, which I don't think exists. If this can be done through a loop, that's also fine.
我想计算每一行的范围,以便在结果中得到 94 个范围。我试图寻找一个numpy.range我认为不存在的函数。如果这可以通过循环来完成,那也很好。
I'm looking for something like numpy.mean, which, if we set the axis parameter to 1, returns the mean for each row in the N-dimensional array.
我正在寻找类似的东西numpy.mean,如果我们将轴参数设置为 1,则返回 N 维数组中每一行的平均值。
采纳答案by JoshAdel
I think np.ptpmight 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 ris your range array.
r你的范围数组在哪里。
回答by Austin Waters
Try this:
尝试这个:
def range(x, axis=0):
return np.max(x, axis=axis) - np.min(x, axis=axis)

