Numpy 中stack , vstack , hstack, dstack 的区别

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame as df

Numpy 数组、堆栈的关系

a = np.array([[1,5], [2,6], [3,7]])
b = np.array([[2,9], [3,10], [4,11]])

[stack](https://docs.scipy.org/doc/numpy/reference/generated/numpy.stack.html#numpy.stack)

Join a sequence of arrays along a new axis.

[hstack](https://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html#numpy.hstack)

Stack arrays in sequence horizontally (column wise).

[dstack](https://docs.scipy.org/doc/numpy/reference/generated/numpy.dstack.html#numpy.dstack)

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

[concatenate](https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html#numpy.concatenate)

Join a sequence of arrays along an existing axis.

[vsplit](https://docs.scipy.org/doc/numpy/reference/generated/numpy.vsplit.html#numpy.vsplit)

Split array into a list of multiple sub-arrays vertically.

[block](https://docs.scipy.org/doc/numpy/reference/generated/numpy.block.html#numpy.block)

Assemble arrays from blocks.

st = np.stack((a,b))
st
array([[[ 1,  5],
        [ 2,  6],
        [ 3,  7]],

       [[ 2,  9],
        [ 3, 10],
        [ 4, 11]]])

stack 与其他不同的地方在于:stack 只接受一个array作为参数,其作用是把一系列数组按照新的轴重新连接
axis参数指定结果尺寸中新轴的索引。例如,如果axis = 0,它将是第一个尺寸;如果axis = -1,它将是最后的尺寸。

arrays = [np.random.randn(3, 4) for _ in range(10)]
arrays
[array([[-0.53973719,  0.50299209,  0.95745118, -0.26949673],
        [ 0.64744531,  0.13725537, -0.95072266,  0.23871383],
        [-0.85645093,  0.43447251, -0.66127764, -0.39638917]]),
 array([[ 0.01645376, -0.43514026,  0.42762195, -0.18683743],
        [-2.45660066, -1.64311795,  0.05201928, -0.53271272],
        [ 0.08035383, -0.11772183, -0.44833717, -0.52417051]]),
 array([[-0.36282446, -0.8293021 ,  0.65878115, -0.29003162],
        [ 1.5406287 , -1.37874536,  0.32952276,  0.48805537],
        [-0.39932278, -0.26140464,  1.59012041,  0.59182549]]),
 array([[-0.89590869, -0.44579809, -0.1563686 , -0.63460744],
        [-0.64827124,  0.37291795, -0.71756578, -1.39594468],
        [-1.95727215,  0.77149991,  0.67372866, -0.86272508]]),
 array([[-0.44499269,  1.05138265, -0.82899606, -0.04693535],
        [-0.94719501,  1.01974679,  2.51189655,  0.17649006],
        [-0.78724837, -1.63699567,  1.67663715, -0.89756524]]),
 array([[ 0.73367542,  1.39412142,  1.20672362,  0.44721082],
        [ 0.14497737,  0.20632498, -0.3818614 , -0.39202536],
        [ 0.07117833, -0.39330401,  0.07495679,  0.19792976]]),
 array([[ 0.99542269,  0.8791468 , -0.726964  , -1.83092773],
        [ 0.45760754,  0.08729036,  0.76038075,  1.6032756 ],
        [-0.21459043, -0.6342964 ,  0.25124415, -0.32549615]]),
 array([[ 1.12063159,  0.10496444, -0.30072915,  0.79139661],
        [-0.57128813,  0.93327623,  0.38186975,  0.70966337],
        [-0.54950243,  0.07771464, -0.99980336, -0.39703295]]),
 array([[ 1.08352049, -2.04123149,  1.3500611 ,  0.97597872],
        [-0.18822966,  2.28699942,  0.9388475 , -0.35978844],
        [ 0.35086597, -1.90569461, -1.25395147, -0.35768165]]),
 array([[-0.5783528 , -0.63846615, -0.09930153, -1.89769832],
        [ 0.66218078,  0.59212707, -1.2857862 ,  0.1300314 ],
        [-1.24610799,  0.57966823,  0.79196263,  0.28236773]])]
np.array(arrays).shape
(10, 3, 4)
for i in range(3) :
    st = np.stack(arrays, axis=i)
    print("在第{0}维上入栈形状为:{1}".format(i, np.stack(arrays, axis=i).shape)    )
    print(st)
在第0维上入栈形状为:(10, 3, 4)
[[[-0.53973719  0.50299209  0.95745118 -0.26949673]
  [ 0.64744531  0.13725537 -0.95072266  0.23871383]
  [-0.85645093  0.43447251 -0.66127764 -0.39638917]]

 [[ 0.01645376 -0.43514026  0.42762195 -0.18683743]
  [-2.45660066 -1.64311795  0.05201928 -0.53271272]
  [ 0.08035383 -0.11772183 -0.44833717 -0.52417051]]

 [[-0.36282446 -0.8293021   0.65878115 -0.29003162]
  [ 1.5406287  -1.37874536  0.32952276  0.48805537]
  [-0.39932278 -0.26140464  1.59012041  0.59182549]]

 [[-0.89590869 -0.44579809 -0.1563686  -0.63460744]
  [-0.64827124  0.37291795 -0.71756578 -1.39594468]
  [-1.95727215  0.77149991  0.67372866 -0.86272508]]

 [[-0.44499269  1.05138265 -0.82899606 -0.04693535]
  [-0.94719501  1.01974679  2.51189655  0.17649006]
  [-0.78724837 -1.63699567  1.67663715 -0.89756524]]

 [[ 0.73367542  1.39412142  1.20672362  0.44721082]
  [ 0.14497737  0.20632498 -0.3818614  -0.39202536]
  [ 0.07117833 -0.39330401  0.07495679  0.19792976]]

 [[ 0.99542269  0.8791468  -0.726964   -1.83092773]
  [ 0.45760754  0.08729036  0.76038075  1.6032756 ]
  [-0.21459043 -0.6342964   0.25124415 -0.32549615]]

 [[ 1.12063159  0.10496444 -0.30072915  0.79139661]
  [-0.57128813  0.93327623  0.38186975  0.70966337]
  [-0.54950243  0.07771464 -0.99980336 -0.39703295]]

 [[ 1.08352049 -2.04123149  1.3500611   0.97597872]
  [-0.18822966  2.28699942  0.9388475  -0.35978844]
  [ 0.35086597 -1.90569461 -1.25395147 -0.35768165]]

 [[-0.5783528  -0.63846615 -0.09930153 -1.89769832]
  [ 0.66218078  0.59212707 -1.2857862   0.1300314 ]
  [-1.24610799  0.57966823  0.79196263  0.28236773]]]
在第1维上入栈形状为:(3, 10, 4)
[[[-0.53973719  0.50299209  0.95745118 -0.26949673]
  [ 0.01645376 -0.43514026  0.42762195 -0.18683743]
  [-0.36282446 -0.8293021   0.65878115 -0.29003162]
  [-0.89590869 -0.44579809 -0.1563686  -0.63460744]
  [-0.44499269  1.05138265 -0.82899606 -0.04693535]
  [ 0.73367542  1.39412142  1.20672362  0.44721082]
  [ 0.99542269  0.8791468  -0.726964   -1.83092773]
  [ 1.12063159  0.10496444 -0.30072915  0.79139661]
  [ 1.08352049 -2.04123149  1.3500611   0.97597872]
  [-0.5783528  -0.63846615 -0.09930153 -1.89769832]]

 [[ 0.64744531  0.13725537 -0.95072266  0.23871383]
  [-2.45660066 -1.64311795  0.05201928 -0.53271272]
  [ 1.5406287  -1.37874536  0.32952276  0.48805537]
  [-0.64827124  0.37291795 -0.71756578 -1.39594468]
  [-0.94719501  1.01974679  2.51189655  0.17649006]
  [ 0.14497737  0.20632498 -0.3818614  -0.39202536]
  [ 0.45760754  0.08729036  0.76038075  1.6032756 ]
  [-0.57128813  0.93327623  0.38186975  0.70966337]
  [-0.18822966  2.28699942  0.9388475  -0.35978844]
  [ 0.66218078  0.59212707 -1.2857862   0.1300314 ]]

 [[-0.85645093  0.43447251 -0.66127764 -0.39638917]
  [ 0.08035383 -0.11772183 -0.44833717 -0.52417051]
  [-0.39932278 -0.26140464  1.59012041  0.59182549]
  [-1.95727215  0.77149991  0.67372866 -0.86272508]
  [-0.78724837 -1.63699567  1.67663715 -0.89756524]
  [ 0.07117833 -0.39330401  0.07495679  0.19792976]
  [-0.21459043 -0.6342964   0.25124415 -0.32549615]
  [-0.54950243  0.07771464 -0.99980336 -0.39703295]
  [ 0.35086597 -1.90569461 -1.25395147 -0.35768165]
  [-1.24610799  0.57966823  0.79196263  0.28236773]]]
在第2维上入栈形状为:(3, 4, 10)
[[[-0.53973719  0.01645376 -0.36282446 -0.89590869 -0.44499269
    0.73367542  0.99542269  1.12063159  1.08352049 -0.5783528 ]
  [ 0.50299209 -0.43514026 -0.8293021  -0.44579809  1.05138265
    1.39412142  0.8791468   0.10496444 -2.04123149 -0.63846615]
  [ 0.95745118  0.42762195  0.65878115 -0.1563686  -0.82899606
    1.20672362 -0.726964   -0.30072915  1.3500611  -0.09930153]
  [-0.26949673 -0.18683743 -0.29003162 -0.63460744 -0.04693535
    0.44721082 -1.83092773  0.79139661  0.97597872 -1.89769832]]

 [[ 0.64744531 -2.45660066  1.5406287  -0.64827124 -0.94719501
    0.14497737  0.45760754 -0.57128813 -0.18822966  0.66218078]
  [ 0.13725537 -1.64311795 -1.37874536  0.37291795  1.01974679
    0.20632498  0.08729036  0.93327623  2.28699942  0.59212707]
  [-0.95072266  0.05201928  0.32952276 -0.71756578  2.51189655
   -0.3818614   0.76038075  0.38186975  0.9388475  -1.2857862 ]
  [ 0.23871383 -0.53271272  0.48805537 -1.39594468  0.17649006
   -0.39202536  1.6032756   0.70966337 -0.35978844  0.1300314 ]]

 [[-0.85645093  0.08035383 -0.39932278 -1.95727215 -0.78724837
    0.07117833 -0.21459043 -0.54950243  0.35086597 -1.24610799]
  [ 0.43447251 -0.11772183 -0.26140464  0.77149991 -1.63699567
   -0.39330401 -0.6342964   0.07771464 -1.90569461  0.57966823]
  [-0.66127764 -0.44833717  1.59012041  0.67372866  1.67663715
    0.07495679  0.25124415 -0.99980336 -1.25395147  0.79196263]
  [-0.39638917 -0.52417051  0.59182549 -0.86272508 -0.89756524
    0.19792976 -0.32549615 -0.39703295 -0.35768165  0.28236773]]]

-1维即第二维:

    st = np.stack(arrays, axis=-1
                 )
    st.shape
(3, 4, 10)

vstack 在 矩阵的第一维,这等效于形状(N,)的一维数组已重整为(1,N)后沿第一轴进行np.concatenate。逆过程是np.vsplit()

vst = np.vstack((a,b))
vst
array([[ 1,  5],
       [ 2,  6],
       [ 3,  7],
       [ 2,  9],
       [ 3, 10],
       [ 4, 11]])
np.vstack()
hstack 等效于沿第二个轴的np.concatenate,逆过程是np.hsplit()
hst = np.hstack((a,b))
hst
array([[ 1,  5,  2,  9],
       [ 2,  6,  3, 10],
       [ 3,  7,  4, 11]])
dst = np.dstack((a,b))
dst
array([[[ 1,  2],
        [ 5,  9]],

       [[ 2,  3],
        [ 6, 10]],

       [[ 3,  4],
        [ 7, 11]]])
conca = np.concatenate((a,b))
conca
array([[ 1,  5],
       [ 2,  6],
       [ 3,  7],
       [ 2,  9],
       [ 3, 10],
       [ 4, 11]])
np.vsplit(conca,2)
[array([[1, 5],
        [2, 6],
        [3, 7]]),
 array([[ 2,  9],
        [ 3, 10],
        [ 4, 11]])]
np.vsplit(dst,3)
[array([[[1, 2],
         [5, 9]]]),
 array([[[ 2,  3],
         [ 6, 10]]]),
 array([[[ 3,  4],
         [ 7, 11]]])]
np.array(np.vsplit(dst,3)).shape
(3, 1, 2, 2)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,186评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,858评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,620评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,888评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,009评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,149评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,204评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,956评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,385评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,698评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,863评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,544评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,185评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,899评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,141评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,684评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,750评论 2 351