Skip to main content
 首页 » 编程设计

python之Numpy数组、索引和卷积混淆

2025年05月04日8pengyingh

我正在尝试完成以下功能,但我一直遇到索引问题,导致“ValueError:操作数无法与形状 (0,9) (5) 一起广播”。

我认为我的错误可能来 self 尝试从 ssd_difference[] 调用值的方式,但我不完全确定。

此外,根据下面给出的提示,我将如何使用 convolve2d?我知道 numpy 有一个功能,但我不知道我需要输入什么才能让它工作。

附加信息:binomialFilter5() 返回表示二项式过滤器的 dtype float 的 5x1 numpy 数组。我还假设“weights[]”是 ssd_difference[] 值。

def transitionDifference(ssd_difference): 

"""计算帧之间的转换成本,将动态纳入 帐户。

说明: 1.遍历ssd差异的行和列,忽略 前两个值和后两个值。
1a.对于 i、j 处的每个值,乘以长度的二项式滤波器 五(稍后在代码中实现)由两个开始的权重 之前的帧到之后的两帧,然后将它们相加 产品。

            i.e. Your weights for frame i are: 
                 [weight[i - 2, j - 2], 
                  weight[i - 1, j - 1], 
                  weight[i, j], 
                  weight[i + 1, j + 1], 
                  weight[i + 2, j + 2]] 

将其乘以每个 i, j 处的二项式滤波器权重得到 你的输出。

可能需要一点点理解才能明白我们为什么 计算这个,简单的解释是从 第 4 到 5 帧,我们称其为 ch(4, 5),我们将其设为权重:

ch(4, 5) = ch(2, 3) + ch(3, 4) + ch(4, 5) + ch(5, 6) + ch(6, 7)

这考虑了之前变化和 future 变化的权重 在考虑当前帧时发生变化。

当然,我们通过二项式过滤器对所有这些和进行权衡,所以 权重 ch(4, 5) 仍然是最重要的,但是 希望这能让您更好地理解。

Args: 
    ssd_difference (numpy.ndarray): A difference matrix as produced by your 
                                    ssd function. 
 
Returns: 
    output (numpy.ndarray): A difference matrix that takes preceding and 
                            following frames into account. The output 
                            difference matrix should have the same dtype as 
                            the input, but be 4 rows and columns smaller, 
                            corresponding to only the frames that have valid 
                            dynamics. 
 
Hint: There is an efficient way to do this with 2d convolution. Think about 
      the coordinates you are using as you consider the preceding and 
      following frame pairings. 
""" 
 
output = np.zeros((ssd_difference.shape[0] - 4, 
                   ssd_difference.shape[1] - 4), dtype=ssd_difference.dtype) 
# WRITE YOUR CODE HERE. 
for i in range(len(ssd_difference)): 
    for j in range(len(ssd_difference)): 
        if i == 0: 
            if j > 1: 
                output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5()) 
        elif i == ssd_difference.shape[0] - 1: 
            if j < ssd_difference.shape[1] - 2: 
                output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5()) 
        else: 
            output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5()) 
# END OF FUNCTION. 
return output 

请您参考如下方法:

正如我所说,您真的应该告诉我们产生错误消息的行。

但我可以猜到,因为只有几行代码执行涉及广播的操作。很可能是:

output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5()) 

您编写 binomialFilter5() 生成一个 (5,1) 数组,但错误谈论的是 (5,)。在这里可能无关紧要,但你真的应该保持维度的数量。有时 (5,1) 与 (5,) 明显不同。

输出 具有形状(ssd_difference.shape[0] - 4, ssd_difference.shape[1] - 4)。但是您正在 range(len(ssd_difference)) 上迭代 i,j。 output[i,j] 最终会导致索引错误。尤其是在对二维数组进行迭代时,最好使用正确的 shape 元素,而不是 len()。

但我怀疑直接错误是由 ssd_difference[i-2:i+2] 引起的。当 i==0 时,这是 ssd_difference[-2:2]。这将生成 (0,9) 数组,因为 -2 索引表示倒数第二个,它大于 2。

我认为您打算从此数组中提取 5 行,以匹配另一个数组中的 5 个值。我认为正确的迭代是:

for i in range(output.shape[0]): 
    for j in range(output.shape[1]): 
       .... 
       output[i,j] = np.sum(ssd_difference[i:i+5, :] * binomialFilter5()) 
       ... 

您应该在交互式 shell 中使用选定的 i 值单独测试表达式。 ssd_difference[i:i+5, :] 应该有形状 (5,9),binomialFilter5() 应该是 ( 5,1)。