numpy 二维布尔数组计数连续真实大小
numpy 2d boolean array count consecutive True sizes
我有兴趣找出布尔数组中 'True' 补丁的各个大小。例如在布尔矩阵中:
[[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]]
输出将是:
[[1, 0, 0, 0],
[0, 4, 4, 0],
[0, 4, 0, 0],
[0, 4, 0, 0]]
我知道我可以递归地执行此操作,但我的印象是 python 大规模数组操作的成本很高,是否有可用的库函数?
这是一个快速简单的完整解决方案:
import numpy as np
import scipy.ndimage.measurements as mnts
A = np.array([
[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]
])
# labeled is a version of A with labeled clusters:
#
# [[1 0 0 0]
# [0 2 2 0]
# [0 2 0 0]
# [0 2 0 0]]
#
# clusters holds the number of different clusters: 2
labeled, clusters = mnts.label(A)
# sizes is an array of cluster sizes: [0, 1, 4]
sizes = mnts.sum(A, labeled, index=range(clusters + 1))
# mnts.sum always outputs a float array, so we'll convert sizes to int
sizes = sizes.astype(int)
# get an array with the same shape as labeled and the
# appropriate values from sizes by indexing one array
# with the other. See the `numpy` indexing docs for details
labeledBySize = sizes[labeled]
print(labeledBySize)
输出:
[[1 0 0 0]
[0 4 4 0]
[0 4 0 0]
[0 4 0 0]]
上面最棘手的行是 "fancy" numpy
索引:
labeledBySize = sizes[labeled]
其中一个数组用于索引另一个。请参阅 numpy
indexing docs (section "Index arrays") 以了解其工作原理的详细信息。
我还写了一个版本的上述代码作为一个单一的紧凑函数that you can try out yourself online.它包括一个基于随机数组的测试用例。
我有兴趣找出布尔数组中 'True' 补丁的各个大小。例如在布尔矩阵中:
[[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]]
输出将是:
[[1, 0, 0, 0],
[0, 4, 4, 0],
[0, 4, 0, 0],
[0, 4, 0, 0]]
我知道我可以递归地执行此操作,但我的印象是 python 大规模数组操作的成本很高,是否有可用的库函数?
这是一个快速简单的完整解决方案:
import numpy as np
import scipy.ndimage.measurements as mnts
A = np.array([
[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]
])
# labeled is a version of A with labeled clusters:
#
# [[1 0 0 0]
# [0 2 2 0]
# [0 2 0 0]
# [0 2 0 0]]
#
# clusters holds the number of different clusters: 2
labeled, clusters = mnts.label(A)
# sizes is an array of cluster sizes: [0, 1, 4]
sizes = mnts.sum(A, labeled, index=range(clusters + 1))
# mnts.sum always outputs a float array, so we'll convert sizes to int
sizes = sizes.astype(int)
# get an array with the same shape as labeled and the
# appropriate values from sizes by indexing one array
# with the other. See the `numpy` indexing docs for details
labeledBySize = sizes[labeled]
print(labeledBySize)
输出:
[[1 0 0 0]
[0 4 4 0]
[0 4 0 0]
[0 4 0 0]]
上面最棘手的行是 "fancy" numpy
索引:
labeledBySize = sizes[labeled]
其中一个数组用于索引另一个。请参阅 numpy
indexing docs (section "Index arrays") 以了解其工作原理的详细信息。
我还写了一个版本的上述代码作为一个单一的紧凑函数that you can try out yourself online.它包括一个基于随机数组的测试用例。