understanding numpy's dstack function

前端 未结 4 1914
梦毁少年i
梦毁少年i 2020-12-22 19:40

I have some trouble understanding what numpy\'s dstack function is actually doing. The documentation is rather sparse and just says:

Sta

4条回答
  •  天涯浪人
    2020-12-22 20:03

    Let x == dstack([a, b]). Then x[:, :, 0] is identical to a, and x[:, :, 1] is identical to b. In general, when dstacking 2D arrays, dstack produces an output such that output[:, :, n] is identical to the nth input array.

    If we stack 3D arrays rather than 2D:

    x = numpy.zeros([2, 2, 3])
    y = numpy.ones([2, 2, 4])
    z = numpy.dstack([x, y])
    

    then z[:, :, :3] would be identical to x, and z[:, :, 3:7] would be identical to y.

    As you can see, we have to take slices along the third axis to recover the inputs to dstack. That's why dstack behaves the way it does.

提交回复
热议问题