假设你有一个形状为(n, m)
的矩阵。你知道吗
此外,还有k
较小的形状矩阵(s, m)
。你知道吗
这些k
矩阵可能是通过将较大的矩阵分片生成的,例如:
stride = z
ranges = [] # will contain sub lists of start / end positions
for i in range(0, n, stride):
if i + s > n:
ranges.append([n-s, n]) #<-- if not evenly divisible include last ragged bit
break
else:
ranges.append([i, i+s])
# k = len(ranges)
for a, b in ranges:
submat = mat[a:b] # <--- produces submats of shape (s, m)
# not necessarily where submats come from, just for
# simple example purpose, feel free to add random noise to each submat
在numpy中,如何连接这些k
重叠的子矩阵,平均重叠区域?你知道吗
我们的目标是,然后采取这些潜艇和改革原来的垫子,例如:
blank = np.zeros((n, m))
for i in range(len(submats)):
a, b = ranges[i]
blank[a:b] += submats[i] #<--- doesn't account for different amounts of overlapping regions
混凝土编号:
n = 693
m = 10
# so mat has shape (693, 10)
s = 500
stride = 50
ranges = [[0, 500], [50, 550], [100, 600], [150, 650], [193, 693]]
# notice that the range (0,50) doesn't need to be averaged
k = 5 # len(ranges)
# so we have k submats of shape (500, 10)
我目前正在做:
def count_overlap(max_len, ranges): # from example 693, and [[0, 500], ...]
tally = np.zeros(max_len)
for i in range(max_len):
for a, b in ranges:
if a <= i and i < b:
tally[i] += 1
return tally
olap = count_overlap(693, ranges)
olap[:55]
# ([1., 1., ..., 1., 2., 2., 2., 2., 2.])
olap[-50:]
# ([2., 2., 2., 2., 2., 2., 2., 2., 1., 1., ..., 1., 1., 1.])
知道如何划分垫子的每个索引
在我自己的问题中,我提供了另一种(不那么优雅的)方法,但我并没有试图回答我自己的问题。相反,我只是简单地包装了相关的功能和解决方案,供其他人使用:
助手
解决方案
由@Paul Panzer提供
试验
下面是使用bincount+cumsum逐行计算重叠的方法:
更新:添加了另一个只使用切片的方法。我希望这会更快。你知道吗
相关问题 更多 >
编程相关推荐