Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计数“;“洞”;在numpy 2D矩阵中_Python_Numpy_Vectorization - Fatal编程技术网

Python 计数“;“洞”;在numpy 2D矩阵中

Python 计数“;“洞”;在numpy 2D矩阵中,python,numpy,vectorization,Python,Numpy,Vectorization,给定1和0的2D矩阵,例如- 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],

给定1和0的2D矩阵,例如-

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  
我能够通过使用计算0的连续组来完成
1

[2]中的
:scipy.ndimage.measures.label(arr==0,
结构=[[0,1,0],
[0,0,0],
[0,1,0]])[1]-arr.shape[1]
Out[2]:12

如何查找
2
3
?我想避免使用循环。

这里有一种使用xor和
np的方法。其中

# mark all the places where A changes in vertical direction
# pad in such a way that the first change in each column is up and the last down
B = np.empty(np.array([1,0])+A.shape, int)
B[:-1] = A
B[1:-1] ^= A[:-1]
B[-1] = A[-1]

# retrieve coordinates of switch points
x, y = np.where(B.T)
# group in pairs, the differences in y are the hole depths
x = x[::2]
d = np.subtract(*y.reshape(-1, 2).T[::-1])

# exclude holes that were introduced by padding
x, d = x[y[1::2]!=len(A)], d[y[1::2]!=len(A)]

# now we have the column numbers and hole depths
x
# array([1, 2, 2, 3, 5, 6, 6, 7, 7, 8, 8, 9])
d
# array([3, 1, 1, 1, 3, 8, 2, 1, 2, 1, 1, 3])

# the sum of the depths
d.sum()
# 27

# and the rows with holes
unq = np.unique(y[1::2])
# make sure not to count padded holes
unq.size - (unq[-1] == len(A))
# 7

这里有一种使用xor和
np的方法。其中

# mark all the places where A changes in vertical direction
# pad in such a way that the first change in each column is up and the last down
B = np.empty(np.array([1,0])+A.shape, int)
B[:-1] = A
B[1:-1] ^= A[:-1]
B[-1] = A[-1]

# retrieve coordinates of switch points
x, y = np.where(B.T)
# group in pairs, the differences in y are the hole depths
x = x[::2]
d = np.subtract(*y.reshape(-1, 2).T[::-1])

# exclude holes that were introduced by padding
x, d = x[y[1::2]!=len(A)], d[y[1::2]!=len(A)]

# now we have the column numbers and hole depths
x
# array([1, 2, 2, 3, 5, 6, 6, 7, 7, 8, 8, 9])
d
# array([3, 1, 1, 1, 3, 8, 2, 1, 2, 1, 1, 3])

# the sum of the depths
d.sum()
# 27

# and the rows with holes
unq = np.unique(y[1::2])
# make sure not to count padded holes
unq.size - (unq[-1] == len(A))
# 7