了解numpy的dstack函数 [英] understanding numpy's dstack function

查看:150
本文介绍了了解numpy的dstack函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解numpy的dstack函数的实际作用.该文档相当稀疏,只是说:

按深度顺序(沿第三轴)堆叠数组.

获取一个数组序列,并沿第三轴堆叠它们 制作单个数组.重建除以dsplit的数组. 这是将2D阵列(图像)堆叠为单个的简单方法 3D阵列进行处理.

所以我或者我真的很愚蠢,这很明显,或者我似乎对堆叠",按顺序",深度方向"或沿轴"这两个术语有一些误解.但是,我的印象是,我在vstackhstack的上下文中理解这些术语.

让我们举个例子:

In [193]: a
Out[193]: 
array([[0, 3],
       [1, 4],
       [2, 5]])
In [194]: b
Out[194]: 
array([[ 6,  9],
       [ 7, 10],
       [ 8, 11]])
In [195]: dstack([a,b])
Out[195]: 
array([[[ 0,  6],
        [ 3,  9]],

       [[ 1,  7],
        [ 4, 10]],

       [[ 2,  8],
        [ 5, 11]]])

首先,ab没有第三个轴,那么我将如何沿着" the 第三个​​轴"堆叠它们呢?其次,假设ab是2D图像的表示,为什么我在结果中以三个 2D数组结束,而不是两个按顺序"排列的2D数组?

解决方案

通过查看输出数组的.shape属性,更容易理解np.vstacknp.hstacknp.dstack *的作用.

使用两个示例数组:

print(a.shape, b.shape)
# (3, 2) (3, 2)

  • np.vstack 串联在一起第一维...

    print(np.vstack((a, b)).shape)
    # (6, 2)
    

  • np.hstack 串联在一起第二个维度...

    print(np.hstack((a, b)).shape)
    # (3, 4)
    

  • np.dstack 串联沿着三维.

    print(np.dstack((a, b)).shape)
    # (3, 2, 2)
    

由于ab都是二维的,所以np.dstack通过插入尺寸为1的第三个尺寸来扩展它们.这等效于用np.newaxis(或None在第三个尺寸中对它们进行索引) >)像这样:

print(a[:, :, np.newaxis].shape)
# (3, 2, 1)

如果为c = np.dstack((a, b)),则为c[:, :, 0] == ac[:, :, 1] == b.

您可以使用 np.concatenate更明确地执行相同的操作像这样:

print(np.concatenate((a[..., None], b[..., None]), axis=2).shape)
# (3, 2, 2)


*使用import *将模块的全部内容导入您的全局名称空间是出于多种原因考虑的不良做法 .惯用的方法是import numpy as np.

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

Stack arrays in sequence depth wise (along third axis).

Takes a sequence of arrays and stack them along the third axis to make a single array. Rebuilds arrays divided by dsplit. This is a simple way to stack 2D arrays (images) into a single 3D array for processing.

So either I am really stupid and the meaning of this is obvious or I seem to have some misconception about the terms 'stacking', 'in sequence', 'depth wise' or 'along an axis'. However, I was of the impression that I understood these terms in the context of vstack and hstack just fine.

Let's take this example:

In [193]: a
Out[193]: 
array([[0, 3],
       [1, 4],
       [2, 5]])
In [194]: b
Out[194]: 
array([[ 6,  9],
       [ 7, 10],
       [ 8, 11]])
In [195]: dstack([a,b])
Out[195]: 
array([[[ 0,  6],
        [ 3,  9]],

       [[ 1,  7],
        [ 4, 10]],

       [[ 2,  8],
        [ 5, 11]]])

First of all, a and b don't have a third axis so how would I stack them along 'the third axis' to begin with? Second of all, assuming a and b are representations of 2D-images, why do I end up with three 2D arrays in the result as opposed to two 2D-arrays 'in sequence'?

解决方案

It's easier to understand what np.vstack, np.hstack and np.dstack* do by looking at the .shape attribute of the output array.

Using your two example arrays:

print(a.shape, b.shape)
# (3, 2) (3, 2)

  • np.vstack concatenates along the first dimension...

    print(np.vstack((a, b)).shape)
    # (6, 2)
    

  • np.hstack concatenates along the second dimension...

    print(np.hstack((a, b)).shape)
    # (3, 4)
    

  • and np.dstack concatenates along the third dimension.

    print(np.dstack((a, b)).shape)
    # (3, 2, 2)
    

Since a and b are both two dimensional, np.dstack expands them by inserting a third dimension of size 1. This is equivalent to indexing them in the third dimension with np.newaxis (or alternatively, None) like this:

print(a[:, :, np.newaxis].shape)
# (3, 2, 1)

If c = np.dstack((a, b)), then c[:, :, 0] == a and c[:, :, 1] == b.

You could do the same operation more explicitly using np.concatenate like this:

print(np.concatenate((a[..., None], b[..., None]), axis=2).shape)
# (3, 2, 2)


* Importing the entire contents of a module into your global namespace using import * is considered bad practice for several reasons. The idiomatic way is to import numpy as np.

这篇关于了解numpy的dstack函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆