X Tutup
Skip to content

Commit 7175f7f

Browse files
committed
numpy stack的理解
1 parent ed544e4 commit 7175f7f

File tree

1 file changed

+308
-0
lines changed

1 file changed

+308
-0
lines changed
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## 1 axis=0的时候"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 38,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"data": {
17+
"text/plain": [
18+
"array([[[1, 2, 3],\n",
19+
" [1, 2, 3],\n",
20+
" [1, 2, 3]],\n",
21+
"\n",
22+
" [[4, 5, 6],\n",
23+
" [4, 5, 6],\n",
24+
" [4, 5, 6]]])"
25+
]
26+
},
27+
"execution_count": 38,
28+
"metadata": {},
29+
"output_type": "execute_result"
30+
}
31+
],
32+
"source": [
33+
"import numpy as np\n",
34+
"a = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])\n",
35+
"b = np.array([[4, 5, 6], [4, 5, 6], [4, 5, 6]])\n",
36+
"c = np.stack((a, b), axis=0)\n",
37+
"c"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 39,
43+
"metadata": {},
44+
"outputs": [
45+
{
46+
"data": {
47+
"text/plain": [
48+
"((3, 3), (2, 3, 3))"
49+
]
50+
},
51+
"execution_count": 39,
52+
"metadata": {},
53+
"output_type": "execute_result"
54+
}
55+
],
56+
"source": [
57+
"a.shape,c.shape"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"c的每个轴的维度为2,3,3,至于这里c的维度为什么变成2,3,3,可以了解下stack,这里2相当于最外层的[]里面变成两个元素,一个是a,一个是b"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 40,
70+
"metadata": {},
71+
"outputs": [
72+
{
73+
"data": {
74+
"text/plain": [
75+
"array([[[1, 2, 3],\n",
76+
" [1, 2, 3],\n",
77+
" [1, 2, 3]],\n",
78+
"\n",
79+
" [[4, 5, 6],\n",
80+
" [4, 5, 6],\n",
81+
" [4, 5, 6]]])"
82+
]
83+
},
84+
"execution_count": 40,
85+
"metadata": {},
86+
"output_type": "execute_result"
87+
}
88+
],
89+
"source": [
90+
"c[:,:,:]"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"上面是每个轴取全部元素"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 41,
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"data": {
107+
"text/plain": [
108+
"array([[[4, 5, 6],\n",
109+
" [4, 5, 6]]])"
110+
]
111+
},
112+
"execution_count": 41,
113+
"metadata": {},
114+
"output_type": "execute_result"
115+
}
116+
],
117+
"source": [
118+
"c[1:2,:2,:]"
119+
]
120+
},
121+
{
122+
"cell_type": "markdown",
123+
"metadata": {},
124+
"source": [
125+
"`c[1:2,:2,:]`也等同于`c[1,:2,:]`,取第1个轴的第二个元素,取第2个轴的前两个元素,取第3个轴的全部元素"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 42,
131+
"metadata": {},
132+
"outputs": [
133+
{
134+
"data": {
135+
"text/plain": [
136+
"array([[1, 2, 3],\n",
137+
" [1, 2, 3],\n",
138+
" [1, 2, 3]])"
139+
]
140+
},
141+
"execution_count": 42,
142+
"metadata": {},
143+
"output_type": "execute_result"
144+
}
145+
],
146+
"source": [
147+
"c[0,:,:]"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"`c[0,:,:]`等同于`c[0]`,取第1个轴的全部元素,其他取全部"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 43,
160+
"metadata": {},
161+
"outputs": [
162+
{
163+
"data": {
164+
"text/plain": [
165+
"array([[1, 2, 3],\n",
166+
" [1, 2, 3],\n",
167+
" [1, 2, 3]])"
168+
]
169+
},
170+
"execution_count": 43,
171+
"metadata": {},
172+
"output_type": "execute_result"
173+
}
174+
],
175+
"source": [
176+
"c[0]"
177+
]
178+
},
179+
{
180+
"cell_type": "markdown",
181+
"metadata": {},
182+
"source": [
183+
"## 2 axis=1的时候"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": 45,
189+
"metadata": {},
190+
"outputs": [
191+
{
192+
"data": {
193+
"text/plain": [
194+
"array([[[1, 2, 3],\n",
195+
" [4, 5, 6]],\n",
196+
"\n",
197+
" [[1, 2, 3],\n",
198+
" [4, 5, 6]],\n",
199+
"\n",
200+
" [[1, 2, 3],\n",
201+
" [4, 5, 6]]])"
202+
]
203+
},
204+
"execution_count": 45,
205+
"metadata": {},
206+
"output_type": "execute_result"
207+
}
208+
],
209+
"source": [
210+
"import numpy as np\n",
211+
"a = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])\n",
212+
"b = np.array([[4, 5, 6], [4, 5, 6], [4, 5, 6]])\n",
213+
"c = np.stack((a, b), axis=1)\n",
214+
"c"
215+
]
216+
},
217+
{
218+
"cell_type": "markdown",
219+
"metadata": {},
220+
"source": [
221+
"![](http://wx4.sinaimg.cn/mw690/af2d2659ly1fmxw587oboj20hb0brgli.jpg)"
222+
]
223+
},
224+
{
225+
"cell_type": "markdown",
226+
"metadata": {},
227+
"source": [
228+
"## 3 axis=2的时候"
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": 46,
234+
"metadata": {},
235+
"outputs": [
236+
{
237+
"data": {
238+
"text/plain": [
239+
"array([[[1, 4],\n",
240+
" [2, 5],\n",
241+
" [3, 6]],\n",
242+
"\n",
243+
" [[1, 4],\n",
244+
" [2, 5],\n",
245+
" [3, 6]],\n",
246+
"\n",
247+
" [[1, 4],\n",
248+
" [2, 5],\n",
249+
" [3, 6]]])"
250+
]
251+
},
252+
"execution_count": 46,
253+
"metadata": {},
254+
"output_type": "execute_result"
255+
}
256+
],
257+
"source": [
258+
"import numpy as np\n",
259+
"a = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])\n",
260+
"b = np.array([[4, 5, 6], [4, 5, 6], [4, 5, 6]])\n",
261+
"c = np.stack((a, b), axis=2)\n",
262+
"c"
263+
]
264+
},
265+
{
266+
"cell_type": "markdown",
267+
"metadata": {},
268+
"source": [
269+
"![](http://wx1.sinaimg.cn/mw690/af2d2659ly1fmxw58osmzj20fm0c6mx2.jpg)"
270+
]
271+
},
272+
{
273+
"cell_type": "markdown",
274+
"metadata": {},
275+
"source": [
276+
"reference:https://blog.csdn.net/qq_17550379/article/details/78934529"
277+
]
278+
},
279+
{
280+
"cell_type": "code",
281+
"execution_count": null,
282+
"metadata": {},
283+
"outputs": [],
284+
"source": []
285+
}
286+
],
287+
"metadata": {
288+
"kernelspec": {
289+
"display_name": "Python 3",
290+
"language": "python",
291+
"name": "python3"
292+
},
293+
"language_info": {
294+
"codemirror_mode": {
295+
"name": "ipython",
296+
"version": 3
297+
},
298+
"file_extension": ".py",
299+
"mimetype": "text/x-python",
300+
"name": "python",
301+
"nbconvert_exporter": "python",
302+
"pygments_lexer": "ipython3",
303+
"version": "3.6.5"
304+
}
305+
},
306+
"nbformat": 4,
307+
"nbformat_minor": 2
308+
}

0 commit comments

Comments
 (0)
X Tutup