【问题标题】:Numpy array, indexing and convolving confusionNumpy数组,索引和卷积混淆
【发布时间】:2015-09-29 23:06:58
【问题描述】:

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

我认为我的错误可能来自我尝试从 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)

这说明了以前的变化和未来的权重 考虑当前帧时发生变化。

当然,我们通过二项式过滤器对所有这些总和进行加权,所以 权重 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

【问题讨论】:

  • 向我们展示更多ValueError,特别是标有----&gt; 的行。关注具有shapes (0,9) (5) 的操作数,以及试图组合它们的operation 是什么。
  • 对不起,该行是 output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5()) 我知道我不能将一个矩阵相乘大小不同,但我想不出任何其他方式来实现该功能的目的(至少根据说明)

标签: python arrays numpy


【解决方案1】:

正如我所评论的,您确实应该告诉我们产生错误消息的行。

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

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

您写道binomialFilter5() 产生了一个(5,1) 数组,但错误涉及(5,)。在这里可能无关紧要,但是您确实应该保持维数不变。有时(5,1)(5,) 有很大不同。

output 的形状为 (ssd_difference.shape[0] - 4, ssd_difference.shape[1] - 4)。但是您在 range(len(ssd_difference)) 上迭代 i,joutput[i,j] 最终会产生 index error。特别是在遍历二维数组时,最好使用正确的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)

【讨论】:

  • 这很有道理,非常感谢!很抱歉没有包含错误指向的行。仍在尝试学习正确的代码/stackoverflow 礼仪。
  • 只是对可能遇到类似问题的任何人的更新。 Numpy.transpose() 是你的朋友。
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2023-04-03
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
相关资源
最近更新 更多