检查numpy数组行是否小于下一个 [英] Check whether numpy array row is smaller than the next
本文介绍了检查numpy数组行是否小于下一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下数组:
In [1]: k
Out[1]:
array([[0],
[1],
[2],
[7],
[4],
[5],
[6]])
我想检查k的每一行是否小于下一行.
I would like to check whether each row of k is smaller than the next.
我尝试了以下操作,但没有用:
I tried the following but it did not work:
In [2]: k[:,0]<k[:+1,0]
Out[2]: array([False, False, False, False, False, False, False], dtype=bool)
我在这里想念什么?
推荐答案
您还可以沿轴0使用np.diff
并检查结果是否大于0.
You can also use np.diff
along axis 0 and check whether the result is greater than 0.
arr = np.array([[0],
[1],
[2],
[7],
[4],
[5],
[6]])
np.diff(arr, axis=0) > 0
array([[ True], 1 > 0
[ True], 2 > 1
[ True], 7 > 2
[False], 4 > 7
[ True], 5 > 4
[ True]]) 6 > 5
在[6]
之后没有行,因此结果短了一行.
There is no row follow [6]
and thus the result is one row shorter.
这篇关于检查numpy数组行是否小于下一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文