Skip to content

Commit 8e5d399

Browse files
author
Divesh
committed
Merge pull request #8 from PythonCHB/master
week 6_2
2 parents e712b79 + 03b8bf4 commit 8e5d399

22 files changed

+2386
-27
lines changed

week-06/code/numpy/basic.ipynb

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
{
2+
"metadata": {
3+
"name": "basic"
4+
},
5+
"nbformat": 3,
6+
"nbformat_minor": 0,
7+
"worksheets": [
8+
{
9+
"cells": [
10+
{
11+
"cell_type": "code",
12+
"collapsed": false,
13+
"input": [
14+
"from start import *"
15+
],
16+
"language": "python",
17+
"metadata": {},
18+
"outputs": [],
19+
"prompt_number": 1
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"Reshaping"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"collapsed": false,
31+
"input": [
32+
"a = np.arange(12)\n",
33+
"a"
34+
],
35+
"language": "python",
36+
"metadata": {},
37+
"outputs": [
38+
{
39+
"output_type": "pyout",
40+
"prompt_number": 4,
41+
"text": [
42+
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
43+
]
44+
}
45+
],
46+
"prompt_number": 4
47+
},
48+
{
49+
"cell_type": "code",
50+
"collapsed": false,
51+
"input": [
52+
"a.shape"
53+
],
54+
"language": "python",
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"output_type": "pyout",
59+
"prompt_number": 5,
60+
"text": [
61+
"(12,)"
62+
]
63+
}
64+
],
65+
"prompt_number": 5
66+
},
67+
{
68+
"cell_type": "code",
69+
"collapsed": false,
70+
"input": [
71+
"print np.reshape(a, (2,6) )"
72+
],
73+
"language": "python",
74+
"metadata": {},
75+
"outputs": [
76+
{
77+
"output_type": "stream",
78+
"stream": "stdout",
79+
"text": [
80+
"[[ 0 1 2 3 4 5]\n",
81+
" [ 6 7 8 9 10 11]]\n"
82+
]
83+
}
84+
],
85+
"prompt_number": 6
86+
},
87+
{
88+
"cell_type": "code",
89+
"collapsed": false,
90+
"input": [
91+
"# unknown dimension\n",
92+
"a.shape = ( -1, 2)\n",
93+
"print a"
94+
],
95+
"language": "python",
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"output_type": "stream",
100+
"stream": "stdout",
101+
"text": [
102+
"[[ 0 1]\n",
103+
" [ 2 3]\n",
104+
" [ 4 5]\n",
105+
" [ 6 7]\n",
106+
" [ 8 9]\n",
107+
" [10 11]]\n"
108+
]
109+
}
110+
],
111+
"prompt_number": 7
112+
},
113+
{
114+
"cell_type": "code",
115+
"collapsed": false,
116+
"input": [
117+
"# flatten\n",
118+
"for i in a.flat: # iterator\n",
119+
" print i "
120+
],
121+
"language": "python",
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"output_type": "stream",
126+
"stream": "stdout",
127+
"text": [
128+
"0\n",
129+
"1\n",
130+
"2\n",
131+
"3\n",
132+
"4\n",
133+
"5\n",
134+
"6\n",
135+
"7\n",
136+
"8\n",
137+
"9\n",
138+
"10\n",
139+
"11\n"
140+
]
141+
}
142+
],
143+
"prompt_number": 8
144+
},
145+
{
146+
"cell_type": "code",
147+
"collapsed": false,
148+
"input": [
149+
"np.ravel(a)"
150+
],
151+
"language": "python",
152+
"metadata": {},
153+
"outputs": [
154+
{
155+
"output_type": "pyout",
156+
"prompt_number": 9,
157+
"text": [
158+
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
159+
]
160+
}
161+
],
162+
"prompt_number": 9
163+
},
164+
{
165+
"cell_type": "code",
166+
"collapsed": false,
167+
"input": [
168+
"a.T"
169+
],
170+
"language": "python",
171+
"metadata": {},
172+
"outputs": [
173+
{
174+
"output_type": "pyout",
175+
"prompt_number": 10,
176+
"text": [
177+
"array([[ 0, 2, 4, 6, 8, 10],\n",
178+
" [ 1, 3, 5, 7, 9, 11]])"
179+
]
180+
}
181+
],
182+
"prompt_number": 10
183+
},
184+
{
185+
"cell_type": "code",
186+
"collapsed": false,
187+
"input": [
188+
"a = np.ravel(a)"
189+
],
190+
"language": "python",
191+
"metadata": {},
192+
"outputs": [],
193+
"prompt_number": 11
194+
},
195+
{
196+
"cell_type": "code",
197+
"collapsed": false,
198+
"input": [
199+
"a"
200+
],
201+
"language": "python",
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"output_type": "pyout",
206+
"prompt_number": 12,
207+
"text": [
208+
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
209+
]
210+
}
211+
],
212+
"prompt_number": 12
213+
},
214+
{
215+
"cell_type": "markdown",
216+
"metadata": {},
217+
"source": [
218+
"Checking dimensions:"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"collapsed": false,
224+
"input": [
225+
"a = np.atleast_2d(a)\n",
226+
"a"
227+
],
228+
"language": "python",
229+
"metadata": {},
230+
"outputs": [
231+
{
232+
"output_type": "pyout",
233+
"prompt_number": 15,
234+
"text": [
235+
"array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])"
236+
]
237+
}
238+
],
239+
"prompt_number": 15
240+
},
241+
{
242+
"cell_type": "code",
243+
"collapsed": false,
244+
"input": [
245+
"a.shape"
246+
],
247+
"language": "python",
248+
"metadata": {},
249+
"outputs": [
250+
{
251+
"output_type": "pyout",
252+
"prompt_number": 16,
253+
"text": [
254+
"(1, 12)"
255+
]
256+
}
257+
],
258+
"prompt_number": 16
259+
},
260+
{
261+
"cell_type": "code",
262+
"collapsed": false,
263+
"input": [
264+
"# reducing:\n",
265+
"a.shape = (2,1,6)\n",
266+
"a.shape"
267+
],
268+
"language": "python",
269+
"metadata": {},
270+
"outputs": [
271+
{
272+
"output_type": "pyout",
273+
"prompt_number": 19,
274+
"text": [
275+
"(2, 1, 6)"
276+
]
277+
}
278+
],
279+
"prompt_number": 19
280+
},
281+
{
282+
"cell_type": "code",
283+
"collapsed": false,
284+
"input": [
285+
"b = np.squeeze(a)\n",
286+
"b.shape"
287+
],
288+
"language": "python",
289+
"metadata": {},
290+
"outputs": [
291+
{
292+
"output_type": "pyout",
293+
"prompt_number": 20,
294+
"text": [
295+
"(2, 6)"
296+
]
297+
}
298+
],
299+
"prompt_number": 20
300+
},
301+
{
302+
"cell_type": "code",
303+
"collapsed": false,
304+
"input": [
305+
"# re-arranging:\n",
306+
"b\n",
307+
"print np.flipud(b)"
308+
],
309+
"language": "python",
310+
"metadata": {},
311+
"outputs": [
312+
{
313+
"output_type": "stream",
314+
"stream": "stdout",
315+
"text": [
316+
"[[ 6 7 8 9 10 11]\n",
317+
" [ 0 1 2 3 4 5]]\n"
318+
]
319+
}
320+
],
321+
"prompt_number": 22
322+
},
323+
{
324+
"cell_type": "code",
325+
"collapsed": false,
326+
"input": [
327+
"print np.fliplr(b)"
328+
],
329+
"language": "python",
330+
"metadata": {},
331+
"outputs": [
332+
{
333+
"output_type": "stream",
334+
"stream": "stdout",
335+
"text": [
336+
"[[ 5 4 3 2 1 0]\n",
337+
" [11 10 9 8 7 6]]\n"
338+
]
339+
}
340+
],
341+
"prompt_number": 23
342+
},
343+
{
344+
"cell_type": "code",
345+
"collapsed": false,
346+
"input": [],
347+
"language": "python",
348+
"metadata": {},
349+
"outputs": []
350+
}
351+
],
352+
"metadata": {}
353+
}
354+
]
355+
}

0 commit comments

Comments
 (0)