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)