给定1和0的二维矩阵,例如-
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 1, 0, 1, 0, 1],
[1, 1, 0, 0, 1, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 0, 0],
[1, 0, 0, 0, 1, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 1, 0, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 1, 0, 1, 1, 0]])
我想计算一些统计数字:
1. Number of holes (no. of 0s with at least one 1 above): 12
2. Sum of hole depths (no. of 1s above holes, summed across columns): 0+3+(1+1)+1+0+3+(2+8)+(2+1)+(1+1)+3 = 27
3. Number of rows with at least one hole: 7
我可以通过使用scipy.ndimage.measurements.label计算0的连续组来完成1
。你知道吗
In[2]: scipy.ndimage.measurements.label(arr == 0,
structure=[[0,1,0],
[0,0,0],
[0,1,0]])[1] - arr.shape[1]
Out[2]: 12
我怎样才能找到2
和3
?我想避免使用循环。你知道吗
这里有一种使用xor和
np.where
的方法:相关问题 更多 >
编程相关推荐