forked from IPGP/scientific_python_cheat_sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.html
434 lines (353 loc) · 39.4 KB
/
template.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Scientific python cheat sheet by IPGP</title>
<meta name="viewport" content="width=device-width, initial-scale=1.5">
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
</head>
<body>
<section class="main-content">
<h1>
<a id="scientific-python-cheatsheet" class="anchor" href="#scientific-python-cheatsheet" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Scientific Python Cheatsheet</h1>
<p><strong>Table of Contents</strong></p>
<ul>
<li style="font-size: 0.8rem;">
<a href="#scientific-python-cheatsheet">Scientific Python Cheatsheet</a>
<ul>
<li>
<a href="#pure-python">Pure Python</a>
<ul>
<li><a href="#types">Types</a></li>
<li><a href="#lists">Lists</a></li>
<li><a href="#dictionaries">Dictionaries</a></li>
<li><a href="#strings">Strings</a></li>
<li><a href="#operators">Operators</a></li>
<li>
<a href="#control-flow">Control Flow</a>
<ul>
<li><a href="#ifelifelse">if/elif/else</a></li>
<li><a href="#for">for</a></li>
<li><a href="#while">while</a></li>
<li><a href="#break">break</a></li>
<li><a href="#continue">continue</a></li>
</ul>
</li>
<li>
<a href="#functions-classes-generators-decorators">Functions, Classes, Generators, Decorators</a>
<ul>
<li><a href="#function">Function</a></li>
<li><a href="#class">Class</a></li>
<li><a href="#generators">Generators</a></li>
<li><a href="#decorators">Decorators</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#numpy">NumPy</a>
<ul>
<li><a href="#array-initialization">array initialization</a></li>
<li><a href="#reading-writing-files">reading/ writing files</a></li>
<li><a href="#array-properties-and-operations">array properties and operations</a></li>
<li><a href="#indexing">indexing</a></li>
<li><a href="#boolean-arrays">boolean arrays</a></li>
<li><a href="#elementwise-operations-and-math-functions">elementwise operations and math functions</a></li>
<li><a href="#inner--outer-products">inner / outer products</a></li>
<li><a href="#interpolation-integration">interpolation, integration</a></li>
<li><a href="#fft">fft</a></li>
<li><a href="#rounding">rounding</a></li>
<li><a href="#random-variables">random variables</a></li>
</ul>
</li>
<li>
<a href="#matplotlib">Matplotlib</a>
<ul>
<li><a href="#figures-and-axes">figures and axes</a></li>
<li><a href="#figures-and-axes-properties">figures and axes properties</a></li>
<li><a href="#plotting-routines">plotting routines</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="group">
<h2>
<a id="pure-python" class="anchor" href="#pure-python" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Pure Python</h2>
<h3>
<a id="types" class="anchor" href="#types" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Types</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">=</span> <span class="pl-c1">2</span> <span class="pl-c"># integer</span>
b <span class="pl-k">=</span> <span class="pl-c1">5.0</span> <span class="pl-c"># float</span>
c <span class="pl-k">=</span> <span class="pl-c1">8.3e5</span> <span class="pl-c"># exponential</span>
d <span class="pl-k">=</span> <span class="pl-c1">1.5</span> <span class="pl-k">+</span> <span class="pl-c1">0.5<span class="pl-k">j</span></span> <span class="pl-c"># complex</span>
e <span class="pl-k">=</span> <span class="pl-c1">3</span> <span class="pl-k">></span> <span class="pl-c1">4</span> <span class="pl-c"># boolean</span>
f <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>word<span class="pl-pds">'</span></span> <span class="pl-c"># string</span></pre></div>
<h3>
<a id="lists" class="anchor" href="#lists" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Lists</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">=</span> [<span class="pl-s"><span class="pl-pds">'</span>red<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>blue<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>green<span class="pl-pds">'</span></span>] <span class="pl-c"># manually initialization</span>
b <span class="pl-k">=</span> <span class="pl-c1">range</span>(<span class="pl-c1">5</span>) <span class="pl-c"># initialization through a function</span>
c <span class="pl-k">=</span> [nu<span class="pl-k">**</span><span class="pl-c1">2</span> <span class="pl-k">for</span> nu <span class="pl-k">in</span> b] <span class="pl-c"># initialize through list comprehension</span>
d <span class="pl-k">=</span> [nu<span class="pl-k">**</span><span class="pl-c1">2</span> <span class="pl-k">for</span> nu <span class="pl-k">in</span> b <span class="pl-k">if</span> b <span class="pl-k"><</span> <span class="pl-c1">3</span>] <span class="pl-c"># list comprehension withcondition</span>
e <span class="pl-k">=</span> c[<span class="pl-c1">0</span>] <span class="pl-c"># access element</span>
f <span class="pl-k">=</span> e[<span class="pl-c1">1</span>: <span class="pl-c1">2</span>] <span class="pl-c"># access a slice of the list</span>
g <span class="pl-k">=</span> [<span class="pl-s"><span class="pl-pds">'</span>re<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>bl<span class="pl-pds">'</span></span>] <span class="pl-k">+</span> [<span class="pl-s"><span class="pl-pds">'</span>gr<span class="pl-pds">'</span></span>] <span class="pl-c"># list concatenation</span>
h <span class="pl-k">=</span> [<span class="pl-s"><span class="pl-pds">'</span>re<span class="pl-pds">'</span></span>] <span class="pl-k">*</span> <span class="pl-c1">5</span> <span class="pl-c"># repeat a list</span>
[<span class="pl-s"><span class="pl-pds">'</span>re<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>bl<span class="pl-pds">'</span></span>].index(<span class="pl-s"><span class="pl-pds">'</span>re<span class="pl-pds">'</span></span>) <span class="pl-c"># returns index of 're'</span>
<span class="pl-s"><span class="pl-pds">'</span>re<span class="pl-pds">'</span></span> <span class="pl-k">in</span> [<span class="pl-s"><span class="pl-pds">'</span>re<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>bl<span class="pl-pds">'</span></span>] <span class="pl-c"># true if 're' in list</span>
<span class="pl-c1">sorted</span>([<span class="pl-c1">3</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>]) <span class="pl-c"># returns sorted list</span>
z <span class="pl-k">=</span> [<span class="pl-s"><span class="pl-pds">'</span>red<span class="pl-pds">'</span></span>] <span class="pl-k">+</span> [<span class="pl-s"><span class="pl-pds">'</span>green<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>blue<span class="pl-pds">'</span></span>] <span class="pl-c"># list concatenation</span></pre></div>
<h3>
<a id="dictionaries" class="anchor" href="#dictionaries" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Dictionaries</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">=</span> {<span class="pl-s"><span class="pl-pds">'</span>red<span class="pl-pds">'</span></span>: <span class="pl-s"><span class="pl-pds">'</span>rouge<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>blue<span class="pl-pds">'</span></span>: <span class="pl-s"><span class="pl-pds">'</span>bleu<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>green<span class="pl-pds">'</span></span>: <span class="pl-s"><span class="pl-pds">'</span>vert<span class="pl-pds">'</span></span>} <span class="pl-c"># dictionary</span>
b <span class="pl-k">=</span> a[<span class="pl-s"><span class="pl-pds">'</span>red<span class="pl-pds">'</span></span>] <span class="pl-c"># translate item</span>
c <span class="pl-k">=</span> [value <span class="pl-k">for</span> key, value <span class="pl-k">in</span> b.items()] <span class="pl-c"># loop through contents</span>
d <span class="pl-k">=</span> a.get(<span class="pl-s"><span class="pl-pds">'</span>yellow<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>no translation found<span class="pl-pds">'</span></span>) <span class="pl-c"># return default</span></pre></div>
<h3>
<a id="strings" class="anchor" href="#strings" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Strings</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>red<span class="pl-pds">'</span></span> <span class="pl-c"># assignment</span>
char <span class="pl-k">=</span> a[<span class="pl-c1">2</span>] <span class="pl-c"># access individual characters</span>
<span class="pl-s"><span class="pl-pds">'</span>red <span class="pl-pds">'</span></span> <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">'</span>blue<span class="pl-pds">'</span></span> <span class="pl-c"># string concatenation</span>
<span class="pl-s"><span class="pl-pds">'</span>1, 2, three<span class="pl-pds">'</span></span>.split(<span class="pl-s"><span class="pl-pds">'</span>,<span class="pl-pds">'</span></span>) <span class="pl-c"># split string into list</span>
<span class="pl-s"><span class="pl-pds">'</span>.<span class="pl-pds">'</span></span>.join([<span class="pl-s"><span class="pl-pds">'</span>1<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>2<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>three<span class="pl-pds">'</span></span>]) <span class="pl-c"># concatenate list into string</span></pre></div>
<h3>
<a id="operators" class="anchor" href="#operators" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Operators</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">=</span> <span class="pl-c1">2</span> <span class="pl-c"># assignment</span>
a <span class="pl-k">+=</span> <span class="pl-c1">1</span> (<span class="pl-k">*</span>=, <span class="pl-k">/</span>=) <span class="pl-c"># change and assign</span>
<span class="pl-c1">3</span> <span class="pl-k">+</span> <span class="pl-c1">2</span> <span class="pl-c"># addition</span>
<span class="pl-c1">3</span> <span class="pl-k">/</span> <span class="pl-c1">2</span> <span class="pl-c"># integer division (python2) or float division (python3)</span>
<span class="pl-c1">3</span> <span class="pl-k">//</span> <span class="pl-c1">2</span> <span class="pl-c"># integer division</span>
<span class="pl-c1">3</span> <span class="pl-k">*</span> <span class="pl-c1">2</span> <span class="pl-c"># multiplication</span>
<span class="pl-c1">3</span> <span class="pl-k">**</span> <span class="pl-c1">2</span> <span class="pl-c"># exponent</span>
<span class="pl-c1">3</span> <span class="pl-k">%</span> <span class="pl-c1">2</span> <span class="pl-c"># remainder</span>
<span class="pl-c1">abs</span>() <span class="pl-c"># absolute value</span>
<span class="pl-c1">1</span> <span class="pl-k">==</span> <span class="pl-c1">1</span> <span class="pl-c"># equal</span>
<span class="pl-c1">2</span> <span class="pl-k">></span> <span class="pl-c1">1</span> <span class="pl-c"># larger</span>
<span class="pl-c1">2</span> <span class="pl-k"><</span> <span class="pl-c1">1</span> <span class="pl-c"># smaller</span>
<span class="pl-c1">1</span> <span class="pl-k">!=</span> <span class="pl-c1">2</span> <span class="pl-c"># not equal</span>
<span class="pl-c1">1</span> <span class="pl-k">!=</span> <span class="pl-c1">2</span> <span class="pl-k">and</span> <span class="pl-c1">2</span> <span class="pl-k"><</span> <span class="pl-c1">3</span> <span class="pl-c"># logical AND</span>
<span class="pl-c1">1</span> <span class="pl-k">!=</span> <span class="pl-c1">2</span> <span class="pl-k">or</span> <span class="pl-c1">2</span> <span class="pl-k"><</span> <span class="pl-c1">3</span> <span class="pl-c"># logical OR</span>
<span class="pl-k">not</span> <span class="pl-c1">1</span> <span class="pl-k">==</span> <span class="pl-c1">2</span> <span class="pl-c"># logical NOT</span>
a <span class="pl-k">in</span> b <span class="pl-c"># test if a is in b</span>
a <span class="pl-k">is</span> b <span class="pl-c"># test if objects point to the same memory (id)</span></pre></div>
<h3>
<a id="control-flow" class="anchor" href="#control-flow" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Control Flow</h3>
<h4>
<a id="ifelifelse" class="anchor" href="#ifelifelse" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>if/elif/else</h4>
<div class="highlight highlight-source-python"><pre>a, b <span class="pl-k">=</span> <span class="pl-c1">1</span>, <span class="pl-c1">2</span>
<span class="pl-k">if</span> a <span class="pl-k">+</span> b <span class="pl-k">==</span> <span class="pl-c1">3</span>:
<span class="pl-c1">print</span> <span class="pl-s"><span class="pl-pds">'</span>True<span class="pl-pds">'</span></span>
<span class="pl-k">elif</span> a <span class="pl-k">+</span> b <span class="pl-k">==</span> <span class="pl-c1">1</span>:
<span class="pl-c1">print</span> <span class="pl-s"><span class="pl-pds">'</span>False<span class="pl-pds">'</span></span>
<span class="pl-k">else</span>:
<span class="pl-c1">print</span> <span class="pl-s"><span class="pl-pds">'</span>?<span class="pl-pds">'</span></span></pre></div>
<h4>
<a id="for" class="anchor" href="#for" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>for</h4>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">=</span> [<span class="pl-s"><span class="pl-pds">'</span>red<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>blue<span class="pl-pds">'</span></span>,
<span class="pl-s"><span class="pl-pds">'</span>green<span class="pl-pds">'</span></span>]
<span class="pl-k">for</span> color <span class="pl-k">in</span> a:
<span class="pl-c1">print</span> color</pre></div>
<h4>
<a id="while" class="anchor" href="#while" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>while</h4>
<div class="highlight highlight-source-python"><pre>number <span class="pl-k">=</span> <span class="pl-c1">1</span>
<span class="pl-k">while</span> number <span class="pl-k"><</span> <span class="pl-c1">10</span>:
<span class="pl-c1">print</span> number
number <span class="pl-k">+=</span> <span class="pl-c1">1</span></pre></div>
<h4>
<a id="break" class="anchor" href="#break" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>break</h4>
<div class="highlight highlight-source-python"><pre>number <span class="pl-k">=</span> <span class="pl-c1">1</span>
<span class="pl-k">while</span> <span class="pl-c1">True</span>:
<span class="pl-c1">print</span> number
number <span class="pl-k">+=</span> <span class="pl-c1">1</span>
<span class="pl-k">if</span> number <span class="pl-k">></span> <span class="pl-c1">10</span>:
<span class="pl-k">break</span></pre></div>
<h4>
<a id="continue" class="anchor" href="#continue" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>continue</h4>
<div class="highlight highlight-source-python"><pre><span class="pl-k">for</span> i <span class="pl-k">in</span> <span class="pl-c1">range</span>(<span class="pl-c1">20</span>):
<span class="pl-k">if</span> i <span class="pl-k">%</span> <span class="pl-c1">2</span> <span class="pl-k">==</span> <span class="pl-c1">0</span>:
<span class="pl-k">continue</span>
<span class="pl-c1">print</span> i</pre></div>
<h3>
<a id="functions-classes-generators-decorators" class="anchor" href="#functions-classes-generators-decorators" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Functions, Classes, Generators, Decorators</h3>
<h4>
<a id="function" class="anchor" href="#function" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Function</h4>
<div class="highlight highlight-source-python"><pre><span class="pl-k">def</span> <span class="pl-en">myfunc</span>(<span class="pl-smi">a1</span>, <span class="pl-smi">a2</span>):
<span class="pl-k">return</span> x
x <span class="pl-k">=</span> my_function(a1,a2)</pre></div>
<h4>
<a id="class" class="anchor" href="#class" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Class</h4>
<div class="highlight highlight-source-python"><pre><span class="pl-k">class</span> <span class="pl-en">Point</span>(<span class="pl-c1">object</span>):
<span class="pl-k">def</span> <span class="pl-c1">__init__</span>(<span class="pl-smi"><span class="pl-smi">self</span></span>, <span class="pl-smi">x</span>):
<span class="pl-v">self</span>.x <span class="pl-k">=</span> x
<span class="pl-k">def</span> <span class="pl-c1">__call__</span>(<span class="pl-smi"><span class="pl-smi">self</span></span>):
<span class="pl-c1">print</span> <span class="pl-v">self</span>.x
x <span class="pl-k">=</span> Point(<span class="pl-c1">3</span>)</pre></div>
<h4>
<a id="generators" class="anchor" href="#generators" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Generators</h4>
<div class="highlight highlight-source-python"><pre><span class="pl-k">def</span> <span class="pl-en">firstn</span>(<span class="pl-smi">n</span>):
num <span class="pl-k">=</span> <span class="pl-c1">0</span>
<span class="pl-k">while</span> num <span class="pl-k"><</span> n:
<span class="pl-k">yield</span> num
num <span class="pl-k">+=</span> <span class="pl-c1">1</span>
x <span class="pl-k">=</span> [<span class="pl-k">for</span> i <span class="pl-k">in</span> firstn(<span class="pl-c1">10</span>)]</pre></div>
<h4>
<a id="decorators" class="anchor" href="#decorators" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Decorators</h4>
<div class="highlight highlight-source-python"><pre><span class="pl-k">class</span> <span class="pl-en">myDecorator</span>(<span class="pl-c1">object</span>):
<span class="pl-k">def</span> <span class="pl-c1">__init__</span>(<span class="pl-smi"><span class="pl-smi">self</span></span>, <span class="pl-smi">f</span>):
<span class="pl-v">self</span>.f <span class="pl-k">=</span> f
<span class="pl-k">def</span> <span class="pl-c1">__call__</span>(<span class="pl-smi"><span class="pl-smi">self</span></span>):
<span class="pl-c1">print</span> <span class="pl-s"><span class="pl-pds">"</span>call<span class="pl-pds">"</span></span>
<span class="pl-v">self</span>.f()
<span class="pl-en">@myDecorator</span>
<span class="pl-k">def</span> <span class="pl-en">my_funct</span>():
<span class="pl-c1">print</span> <span class="pl-s"><span class="pl-pds">'</span>func<span class="pl-pds">'</span></span>
my_func()</pre></div>
</div>
<div class="group">
<h2>
<a id="numpy" class="anchor" href="#numpy" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>NumPy</h2>
<h3>
<a id="array-initialization" class="anchor" href="#array-initialization" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>array initialization</h3>
<div class="highlight highlight-source-python"><pre>np.array([<span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>]) <span class="pl-c"># direct initialization</span>
np.empty(<span class="pl-c1">20</span>, <span class="pl-v">dtype</span><span class="pl-k">=</span>np.float32) <span class="pl-c"># single precision array with 20 entries</span>
np.zeros(<span class="pl-c1">200</span>) <span class="pl-c"># initialize 200 zeros</span>
np.ones((<span class="pl-c1">3</span>,<span class="pl-c1">3</span>), <span class="pl-v">dtype</span><span class="pl-k">=</span>np.int32) <span class="pl-c"># 3 x 3 integer matrix with ones</span>
np.eye(<span class="pl-c1">200</span>) <span class="pl-c"># ones on the diagonal</span>
np.zeros_like(a) <span class="pl-c"># returns array with zeros and the shape of a</span>
np.linspace(<span class="pl-c1">0</span>., <span class="pl-c1">10</span>., <span class="pl-c1">100</span>) <span class="pl-c"># 100 points from 0 to 10</span>
np.arange(<span class="pl-c1">0</span>, <span class="pl-c1">100</span>, <span class="pl-c1">2</span>) <span class="pl-c"># points from 0 to <100 with step width 2</span>
np.logspace(<span class="pl-k">-</span><span class="pl-c1">5</span>, <span class="pl-c1">2</span>, <span class="pl-c1">100</span>) <span class="pl-c"># 100 log-spaced points between 1e-5 and 1e2</span>
np.copy(a) <span class="pl-c"># copy array to new memory</span></pre></div>
<h3>
<a id="reading-writing-files" class="anchor" href="#reading-writing-files" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>reading/ writing files</h3>
<div class="highlight highlight-source-python"><pre>np.fromfile(fname<span class="pl-k">/</span><span class="pl-c1">object</span>, <span class="pl-v">dtype</span><span class="pl-k">=</span>np.float32, <span class="pl-v">count</span><span class="pl-k">=</span><span class="pl-c1">5</span>) <span class="pl-c"># read binary data from file</span>
np.loadtxt(fname<span class="pl-k">/</span><span class="pl-c1">object</span>, <span class="pl-v">skiprows</span><span class="pl-k">=</span><span class="pl-c1">2</span>, <span class="pl-v">delimiter</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>,<span class="pl-pds">'</span></span>) <span class="pl-c"># read ascii data from file</span></pre></div>
<h3>
<a id="array-properties-and-operations" class="anchor" href="#array-properties-and-operations" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>array properties and operations</h3>
<div class="highlight highlight-source-python"><pre>a.shape <span class="pl-c"># a tuple with the lengths of each axis</span>
<span class="pl-c1">len</span>(a) <span class="pl-c"># length of axis 0</span>
a.ndim <span class="pl-c"># number of dimensions (axes)</span>
a.sort(<span class="pl-v">axis</span><span class="pl-k">=</span><span class="pl-c1">1</span>) <span class="pl-c"># sort array along axis</span>
a.flatten() <span class="pl-c"># collapse array to one dimension</span>
a.conj() <span class="pl-c"># return complex conjugate</span>
a.astype(np.int16) <span class="pl-c"># cast to integer</span>
np.argmax(a, <span class="pl-v">axis</span><span class="pl-k">=</span><span class="pl-c1">2</span>) <span class="pl-c"># return index of maximum along a given axis</span>
np.cumsum(a) <span class="pl-c"># return cumulative sum</span>
np.any(a) <span class="pl-c"># True if any element is True</span>
np.all(a) <span class="pl-c"># True if all elements are True</span>
np.argsort(a, <span class="pl-v">axis</span><span class="pl-k">=</span><span class="pl-c1">1</span>) <span class="pl-c"># return sorted index array along axis</span></pre></div>
<h3>
<a id="indexing" class="anchor" href="#indexing" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>indexing</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">=</span> np.arange(<span class="pl-c1">100</span>) <span class="pl-c"># initialization with 0 - 99</span>
a[: <span class="pl-c1">3</span>] <span class="pl-k">=</span> <span class="pl-c1">0</span> <span class="pl-c"># set the first three indices to zero</span>
a[<span class="pl-c1">1</span>: <span class="pl-c1">5</span>] <span class="pl-k">=</span> <span class="pl-c1">1</span> <span class="pl-c"># set indices 1-4 to 1</span>
a[start:stop:step] <span class="pl-c"># general form of indexing/slicing</span>
a[<span class="pl-c1">None</span>, :] <span class="pl-c"># transform to column vector</span>
a[[<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">3</span>, <span class="pl-c1">8</span>]] <span class="pl-c"># return array with values of the indices</span>
a <span class="pl-k">=</span> a.reshape(<span class="pl-c1">10</span>, <span class="pl-c1">10</span>) <span class="pl-c"># transform to 10 x 10 matrix</span>
a.<span class="pl-c1">T</span> <span class="pl-c"># return transposed view</span>
np.transpose(a, (<span class="pl-c1">2</span>, <span class="pl-c1">1</span>, <span class="pl-c1">0</span>)) <span class="pl-c"># transpose array to new axis order</span>
a[a <span class="pl-k"><</span> <span class="pl-c1">2</span>] <span class="pl-c"># returns array that fulfills elementwise condition</span></pre></div>
<h3>
<a id="boolean-arrays" class="anchor" href="#boolean-arrays" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>boolean arrays</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k"><</span> <span class="pl-c1">2</span> <span class="pl-c"># returns array with boolean values</span>
np.logical_and(a <span class="pl-k"><</span> <span class="pl-c1">2</span>, b <span class="pl-k">></span> <span class="pl-c1">10</span>) <span class="pl-c"># elementwise logical and</span>
np.logical_or(a <span class="pl-k"><</span> <span class="pl-c1">2</span>, b <span class="pl-k">></span> <span class="pl-c1">10</span>) <span class="pl-c"># elementwise logical or</span>
<span class="pl-k">~</span>a <span class="pl-c"># invert boolean array</span>
np.invert(a) <span class="pl-c"># invert boolean array</span></pre></div>
<h3>
<a id="elementwise-operations-and-math-functions" class="anchor" href="#elementwise-operations-and-math-functions" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>elementwise operations and math functions</h3>
<div class="highlight highlight-source-python"><pre>a <span class="pl-k">*</span> <span class="pl-c1">5</span> <span class="pl-c"># multiplication with scalar</span>
a <span class="pl-k">+</span> <span class="pl-c1">5</span> <span class="pl-c"># addition with scalar</span>
a <span class="pl-k">+</span> b <span class="pl-c"># addition with array b</span>
a <span class="pl-k">/</span> b <span class="pl-c"># division with b (np.NaN for division by zero)</span>
np.exp(a) <span class="pl-c"># exponential (complex and real)</span>
np.sin(a) <span class="pl-c"># sine</span>
np.cos(a) <span class="pl-c"># cosine</span>
np.arctan2(y,x) <span class="pl-c"># arctan(y/x)</span>
np.arcsin(x) <span class="pl-c"># arcsin</span>
np.radians(a) <span class="pl-c"># degrees to radians</span>
np.degrees(a) <span class="pl-c"># radians to degrees</span>
np.var(a) <span class="pl-c"># variance of array</span>
np.std(a, <span class="pl-v">axis</span><span class="pl-k">=</span><span class="pl-c1">1</span>) <span class="pl-c"># standard deviation</span></pre></div>
<h3>
<a id="inner--outer-products" class="anchor" href="#inner--outer-products" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>inner / outer products</h3>
<div class="highlight highlight-source-python"><pre>np.dot(a, b) <span class="pl-c"># inner matrix product: a_mi b_in</span>
np.einsum(<span class="pl-s"><span class="pl-pds">'</span>ijkl,klmn->ijmn<span class="pl-pds">'</span></span>, a, b) <span class="pl-c"># einstein summation convention</span>
np.sum(a, <span class="pl-v">axis</span><span class="pl-k">=</span><span class="pl-c1">1</span>) <span class="pl-c"># sum over axis 1</span>
np.abs(a) <span class="pl-c"># return array with absolute values</span>
a[<span class="pl-c1">None</span>, :] <span class="pl-k">+</span> b[:, <span class="pl-c1">None</span>] <span class="pl-c"># outer sum</span>
a[<span class="pl-c1">None</span>, :] <span class="pl-k">*</span> b[<span class="pl-c1">None</span>, :] <span class="pl-c"># outer product</span>
np.outer(a, b) <span class="pl-c"># outer product</span>
np.sum(a <span class="pl-k">*</span> a.<span class="pl-c1">T</span>) <span class="pl-c"># matrix norm</span></pre></div>
<h3>
<a id="interpolation-integration" class="anchor" href="#interpolation-integration" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>interpolation, integration</h3>
<div class="highlight highlight-source-python"><pre>np.trapz(y, <span class="pl-v">x</span><span class="pl-k">=</span>x, <span class="pl-v">axis</span><span class="pl-k">=</span><span class="pl-c1">1</span>) <span class="pl-c"># integrate along axis 1</span>
np.interp(x, xp, yp) <span class="pl-c"># interpolate function xp, yp at points x</span></pre></div>
<h3>
<a id="fft" class="anchor" href="#fft" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>fft</h3>
<div class="highlight highlight-source-python"><pre>np.fft.fft(y) <span class="pl-c"># complex fourier transform of y</span>
np.fft.fftfreqs(<span class="pl-c1">len</span>(y)) <span class="pl-c"># fft frequencies for a given length</span>
np.fft.fftshift(freqs) <span class="pl-c"># shifts zero frequency to the middle</span>
np.fft.rfft(y) <span class="pl-c"># real fourier transform of y</span>
np.fft.rfftfreqs(<span class="pl-c1">len</span>(y)) <span class="pl-c"># real fft frequencies for a given length</span></pre></div>
<h3>
<a id="rounding" class="anchor" href="#rounding" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>rounding</h3>
<div class="highlight highlight-source-python"><pre>np.ceil(a) <span class="pl-c"># rounds to nearest upper int</span>
np.floor(a) <span class="pl-c"># rounds to nearest lower int</span>
np.round(a) <span class="pl-c"># rounds to neares int</span></pre></div>
<h3>
<a id="random-variables" class="anchor" href="#random-variables" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>random variables</h3>
<div class="highlight highlight-source-python"><pre>np.random.normal(<span class="pl-v">loc</span><span class="pl-k">=</span><span class="pl-c1">0</span>, <span class="pl-v">scale</span><span class="pl-k">=</span><span class="pl-c1">2</span>, <span class="pl-v">size</span><span class="pl-k">=</span><span class="pl-c1">100</span>) <span class="pl-c"># 100 normal distributed random numbers</span>
np.random.seed(<span class="pl-c1">23032</span>) <span class="pl-c"># resets the seed value</span>
np.random.rand(<span class="pl-c1">200</span>) <span class="pl-c"># 200 random numbers in [0, 1)</span>
np.random.uniform(<span class="pl-c1">1</span>, <span class="pl-c1">30</span>, <span class="pl-c1">200</span>) <span class="pl-c"># 200 random numbers in [1, 30)</span>
np.random.random_integers(<span class="pl-c1">1</span>, <span class="pl-c1">15</span>, <span class="pl-c1">300</span>) <span class="pl-c"># 300 random integers between [1, 15]</span></pre></div>
</div>
<div class="group">
<h2>
<a id="matplotlib" class="anchor" href="#matplotlib" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Matplotlib</h2>
<h3>
<a id="figures-and-axes" class="anchor" href="#figures-and-axes" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>figures and axes</h3>
<div class="highlight highlight-source-python"><pre>fig <span class="pl-k">=</span> plt.figure(<span class="pl-v">figsize</span><span class="pl-k">=</span>(<span class="pl-c1">5</span>, <span class="pl-c1">2</span>), <span class="pl-v">facecolor</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>black<span class="pl-pds">'</span></span>) <span class="pl-c"># initialize figure</span>
ax <span class="pl-k">=</span> fig.add_subplot(<span class="pl-c1">3</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>) <span class="pl-c"># add second subplot in a 3 x 2 grid</span>
fig, axes <span class="pl-k">=</span> plt.subplots(<span class="pl-c1">5</span>, <span class="pl-c1">2</span>, <span class="pl-v">figsize</span><span class="pl-k">=</span>(<span class="pl-c1">5</span>, <span class="pl-c1">5</span>)) <span class="pl-c"># return fig and array of axes in a 5 x 2 grid</span>
ax <span class="pl-k">=</span> fig.add_axes([left, bottom, width, height]) <span class="pl-c"># manually add axes at a certain position</span></pre></div>
<h3>
<a id="figures-and-axes-properties" class="anchor" href="#figures-and-axes-properties" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>figures and axes properties</h3>
<div class="highlight highlight-source-python"><pre>fig.suptitle(<span class="pl-s"><span class="pl-pds">'</span>title<span class="pl-pds">'</span></span>) <span class="pl-c"># big figure title</span>
fig.subplots_adjust(<span class="pl-v">bottom</span><span class="pl-k">=</span><span class="pl-c1">0.1</span>, <span class="pl-v">right</span><span class="pl-k">=</span><span class="pl-c1">0.8</span>, <span class="pl-v">top</span><span class="pl-k">=</span><span class="pl-c1">0.9</span>, <span class="pl-v">wspace</span><span class="pl-k">=</span><span class="pl-c1">0.2</span>,
<span class="pl-v">hspace</span><span class="pl-k">=</span><span class="pl-c1">0.5</span>) <span class="pl-c"># adjust subplot positions</span>
fig.tight_layout(<span class="pl-v">pad</span><span class="pl-k">=</span><span class="pl-c1">0.1</span>,<span class="pl-v">h_pad</span><span class="pl-k">=</span><span class="pl-c1">0.5</span>, <span class="pl-v">w_pad</span><span class="pl-k">=</span><span class="pl-c1">0.5</span>, <span class="pl-v">rect</span><span class="pl-k">=</span><span class="pl-c1">None</span>) <span class="pl-c"># adjust</span>
subplots to fit perfectly into fig
ax.set_xlabel() <span class="pl-c"># set xlabel</span>
ax.set_ylabel() <span class="pl-c"># set ylabel</span>
ax.set_xlim(<span class="pl-c1">1</span>, <span class="pl-c1">2</span>) <span class="pl-c"># sets x limits</span>
ax.set_ylim(<span class="pl-c1">3</span>, <span class="pl-c1">4</span>) <span class="pl-c"># sets y limits</span>
ax.set_title(<span class="pl-s"><span class="pl-pds">'</span>blabla<span class="pl-pds">'</span></span>) <span class="pl-c"># sets the axis title</span>
ax.set(<span class="pl-v">xlabel</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>bla<span class="pl-pds">'</span></span>) <span class="pl-c"># set multiple parameters at once</span>
ax.legend(<span class="pl-v">loc</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>upper center<span class="pl-pds">'</span></span>) <span class="pl-c"># activate legend</span>
ax.grid(<span class="pl-c1">True</span>, <span class="pl-v">which</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>both<span class="pl-pds">'</span></span>) <span class="pl-c"># activate grid</span>
bbox <span class="pl-k">=</span> ax.get_position() <span class="pl-c"># returns the axes bounding box</span>
bbox.x0 <span class="pl-k">+</span> bbox.width <span class="pl-c"># bounding box parameters</span></pre></div>
<h3>
<a id="plotting-routines" class="anchor" href="#plotting-routines" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>plotting routines</h3>
<div class="highlight highlight-source-python"><pre>ax.plot(x,y, <span class="pl-s"><span class="pl-pds">'</span>-o<span class="pl-pds">'</span></span>, <span class="pl-v">c</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>red<span class="pl-pds">'</span></span>, <span class="pl-v">lw</span><span class="pl-k">=</span><span class="pl-c1">2</span>, <span class="pl-v">label</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>bla<span class="pl-pds">'</span></span>) <span class="pl-c"># plots a line</span>
ax.scatter(x,y, <span class="pl-v">s</span><span class="pl-k">=</span><span class="pl-c1">20</span>, <span class="pl-v">c</span><span class="pl-k">=</span>color) <span class="pl-c"># scatter plot</span>
ax.pcolormesh(xx,yy,zz, <span class="pl-v">shading</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>gouraud<span class="pl-pds">'</span></span>) <span class="pl-c"># fast colormesh function</span>
ax.colormesh(xx,yy,zz, <span class="pl-v">norm</span><span class="pl-k">=</span>norm) <span class="pl-c"># slower colormesh function</span>
ax.contour(xx,yy,zz, <span class="pl-v">cmap</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>jet<span class="pl-pds">'</span></span>) <span class="pl-c"># contour line plot</span>
ax.contourf(xx,yy,zz, <span class="pl-v">vmin</span><span class="pl-k">=</span><span class="pl-c1">2</span>, <span class="pl-v">vmax</span><span class="pl-k">=</span><span class="pl-c1">4</span>) <span class="pl-c"># filled contours plot</span>
n, bins, patch <span class="pl-k">=</span> ax.hist(x, <span class="pl-c1">50</span>) <span class="pl-c"># histogram</span>
ax.imshow(matrix, <span class="pl-v">origin</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>lower<span class="pl-pds">'</span></span>, <span class="pl-v">extent</span><span class="pl-k">=</span>(x1, x2, y1, y2)) <span class="pl-c"># show image</span>
ax.specgram(y, <span class="pl-v">FS</span><span class="pl-k">=</span><span class="pl-c1">0.1</span>, <span class="pl-v">noverlap</span><span class="pl-k">=</span><span class="pl-c1">128</span>, <span class="pl-v">scale</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">'</span>linear<span class="pl-pds">'</span></span>) <span class="pl-c"># plot a spectrogram</span></pre></div>
</div>
<footer class="site-footer">
<span class="site-footer-owner"><a href="https://github.com/IPGP/scientific_python_cheat_sheet">Scientific python cheat sheet</a> is maintained by <a href="https://github.com/IPGP">IPGP</a>.</span>
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span>
</footer>
</section>
</body>
</html>