【发布时间】:2020-01-05 06:45:38
【问题描述】:
假设您有一个形状为(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.])
要知道垫子的每个索引要除多少
【问题讨论】:
-
@Divakar 在帖子中,是的,他们都是
(s, m) -
@Divakar 形状与起始垫相同
(n, m) -
@Divakar 没有。您想从
k、(s, m)矩阵重新创建(n,m)矩阵,其中第一维中k矩阵之间存在重叠 -
通常你可能有多少个范围?如果它是一个小数字,那么在循环中切片和添加是有意义的,就像您已经拥有的那样。
-
@Divakar 我更新了问题以提供一些具体示例,
k通常为