an introduction to python - nims.re.krkgwg.nims.re.kr/2016nrgw/python.pdfan introduction to python...

26
An introduction to Python Edwin J. Son (National Institute for Mathematical Sciences) [link to this slides: https://goo.gl/kOtWpZ ] [jupyter notebook: https://goo.gl/9m9RmK ]

Upload: hathu

Post on 29-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

An introduction to

PythonEdwin J. Son

(National Institute for Mathematical Sciences)

[link to this slides: https://goo.gl/kOtWpZ ]

[jupyter notebook: https://goo.gl/9m9RmK ]

Why Python? The scientist's needs [www.scipy-lectures.org]

• Get data (simulation, experiment control)

• Manipulate and process data.

• Visualize results…

• to understand what we are doing!

• Communicate results: produce figures for reports or publications, write presentations.

2

A powerful module:

NumPy int i, j, k; int ROW_A = 1024; int COL_A = 1024; int COL_B = 1024; float sum, *M_A, *M_B, *M_C;

M_A = (float *)calloc(ROW_A * COL_A, sizeof(float)); M_B = (float *)calloc(COL_A * COL_B, sizeof(float)); M_C = (float *)calloc(ROW_A * COL_B, sizeof(float));

srand(0);

float (*A)[COL_A] = (float (*)[COL_A])M_A; for (i = 0; i < ROW_A; i++) { for (j = 0; j < COL_A; j++) { A[i][j] = (float)(rand() % 1000) / 100.0f; } }

float (*B)[COL_B] = (float (*)[COL_B])M_B; for (i = 0; i < COL_A; i++) { for (j = 0; j < COL_B; j++) { B[i][j] = (float)(rand() % 1000) / 100.0f; } }

for (i = 0; i < ROW_A; i++) { for (j = 0; j < COL_B; j++) { sum = 0.0; for (k = 0; k < COL_A; k++) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; } }

import numpy as np

ROW_A = 1024 COL_A = 1024 COL_B = 1024

np.random.seed(seed=0) M_A = np.float32(10.0 * np.random.rand(ROW_A, COL_A)) M_B = np.float32(10.0 * np.random.rand(COL_A, COL_B)) M_C = np.empty((ROW_A, COL_B), dtype=np.float32)

for i in range(ROW_A): for j in range(COL_B): M_C[i,j] = (M_A[i,:] * M_B[:,j]).sum()

Sequential C ~ 8s

Python ~ 30s

import numpy as np

ROW_A = 1024 COL_A = 1024 COL_B = 1024

np.random.seed(seed=0) M_A = np.float32(10.0 * np.random.rand(ROW_A, COL_A)) M_B = np.float32(10.0 * np.random.rand(COL_A, COL_B)) M_C = np.empty((ROW_A, COL_B), dtype=np.float32)

M_C = np.dot(M_A, M_B)

NumPy ~ 0.03s3

Another powerful module:

Matplotlibimport matplotlib.pyplot as plt import matplotlib.gridspec as gspec

gs = gspec.GridSpec(4,2)

plt.subplot(gs[0,0]) plt.title('Downsampling') plt.plot(xtime(f,sr),f,label='original 16k') plt.ylabel('original 16k')

plt.subplot(gs[1,0]) plt.plot(freq,ftp,label='16k sample in freq. domain') plt.ylabel('fft result')

plt.subplot(gs[2,0]) plt.plot(dfreq,dftp/2,label='HF cut for '+str(16/dr)+'k sample') plt.ylabel('high freq. cutoff')

plt.subplot(gs[3,0]) plt.plot(xtime(f,sr),f,'--',lw=3,label='original 16k') plt.plot(xtime(d,sr/dr),d,label='downsampled '+str(16/dr)+'k') plt.ylabel('orig(16k)/down('+str(16/dr)+'k)') plt.xlabel('time (s)') plt.legend() plt.subplot(gs[0,1]) plt.title('Upsampling') plt.plot(xtime(d,sr/dr),d,label='original '+str(16/dr)+'k') plt.ylabel('original '+str(16/dr)+'k')

plt.subplot(gs[1,1]) plt.plot(dfreq,dftp,label=''+str(16/dr)+'k sample in freq. domain') plt.ylabel('fft result')

plt.subplot(gs[2,1]) plt.plot(ufreq,uftp,label='expanded to '+str(16/dr*ur)+'k') plt.ylabel(''+str(16/dr*ur)+'k-expanded result')

plt.subplot(gs[3,1]) plt.plot(xtime(f,sr),f,lw=3,label='original 16k') plt.plot(xtime(d,sr/dr),d,'--',lw=3,label='downsampled '+str(16/dr)+'k') plt.plot(xtime(u,sr/dr*ur),u,label='upsampled '+str(16/dr*ur)+'k') plt.ylabel('orig/down/up') plt.xlabel('time (s)') plt.legend()

plt.savefig("resampling.png",dpi=300)

4

2015 Top 10 Programming Languages [http://spectrum.ieee.org/computing/software/the-2015-top-ten-programming-languages]

Why not?

5

$ python Python 2.7.5 (default, Jun 4 2014, 11:15:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello, World!" Hello, World! >>> exit() Hello, World!• Python: https://www.python.org/

• ipython: http://ipython.org/

• jupyter: http://jupyter.org/

• NumPy: http://www.numpy.org/

• Matplotlib: http://matplotlib.org/

• pyplot api documents: http://matplotlib.org/api/pyplot_api.html

• (eBook) 왕초보를 위한 Python 2.7: https://wikidocs.net/book/2

• (eBook) Jump to Python: https://wikidocs.net/book/1

>>> print "Hello, World!" Hello, World!

>>> print(“Hello, World!”) Hello, World!

[Python 2] vs. [Python 3]

6

$ cat HelloWorld.py #!/usr/bin/env python

print "Hello, World!" $ python HelloWorld.py Hello, World!

$ ipython Python 2.7.5 (default, Jun 4 2014, 11:15:32) Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details.

In [1]: print "Hello, World!" Hello, World!

In [2]: exit

[key diff]

Contents [Ref: Paul Barry, “Head First Python,” O’Reilly Media]

1. Lists in Python

• list vs. tuple vs. dict

• for vs. while

2. Others in Python

• if, files, functions, etc.

3. Modules (~ Libraries in C)

• numpy (array)

• matplotlib (pyplot)

dict_ex = {} # initialize dictionary key_tuple = (1, "two", "drei") # prepare keys as a tuple val_list = ["one", "zwei", 3] # prepare values as a list

for i in range(len(key_tuple)): dict_ex[key_tuple[i]] = val_list[i] # set value for each key key_list = list(dict_ex.keys()) # extract keys as a list

file_object = open("dict_test.txt", "w") # open a file in write mode

for key in key_list: if key in key_list[:-1]: file_object.write("{0}: {1}\n".format(key, dict_ex[key])) # write key and value to the file

file_object.close() # close the file

7

In [1]: list? Docstring: list() -> new empty list list(iterable) -> new list initialized from iterable's items Type: type

Lists in Python• list vs. tuple vs. dict • for vs. while

Data Type

• String • Number • List • Tuple • Dictionary

• int : 16 • float : 16.0, 1.6e1 • complex : 16.+0j • oct : 020, 0o20 • hex : 0x10

• “Hello, World.” • “Date: %d %s %d” % (27, ‘June’, 2016) • ‘{0} USD for {1}’.format(10, “lunch”)

>>> lst = [1, “two”, 3.0] >>> lst[1] ‘two’ >>> lst[1:] [‘two’, 3.0] >>> lst[:-1] [1, ‘two’]

>>> tpl = (1, “two”, 3.0) >>> tpl[1] ‘two’ >>> tpl[1:] (‘two’, 3.0) >>> tpl[:-1] (1, ‘two’)

>>> dct = {1: “eins”, “two”: “zwei”} >>> dct[“two”] zwei

9

Some functions for list• To add element(s)

- append, insert, + (concatenate), * (repeat)

• To remove an element - del, remove

• To modify element(s)- a[start:end] = b

• Other functions - sort, reverse, index, pop, count, extend

10

0 1 2 3 41 ‘two’ 3.0 (4+0j)

-4 -3 -2 -1

List Examples

>>> a = [1, “two”, 3.0]

>>> a.append(4+0j)

>>> print a[1, ‘two’, 3.0, (4+0j)]

>>> a + [“penta”, “sechs”] [1, ‘two’, 3.0, (4+0j), ‘penta’, ‘sechs’]

>>> a[1, ‘two’, 3.0, (4+0j)]

>>> a * 2[1, ‘two’, 3.0, (4+0j), 1, ‘two’, 3.0, (4+0j)]

>>> del a[3],; print a [1, ‘two’, 3.0]

>>> a[0] = 1.0

>>> a[1:2] = [1.5, 2.0, 2.5]

>>> print a[1.0, 1.5, 2.0, 2.5, 3.0]

>>> a[3:] = []

>>> a[1.0, 1.5, 2.0]

>>> a.insert(0, 0.),; print a [0.0, 1.0, 1.5, 2.0]

>>> a.remove(1.5),; print a [0.0, 1.0, 2.0]

>>> a[::-1][2.0, 1.0, 0.0]

>>> b = “abcde” >>> b[2:4] ‘cd’ >>> b + “fg” ‘abcdefg’

0 1 2 3 41 ‘two’ 3.0 (4+0j)

-4 -3 -2 -1

11

>>> a=[1,2,3] >>> b=a >>> c=a[:] >>> b[1]="two" >>> a [1, 'two', 3] >>> c [1, 2, 3]

Loops• for element in list:

do_something(elemenent)

• Notice the colon and the indent

• range(N)- returns a list of length N with elements 0, …, (N-1)

• len(list)- returns the length of list

• for i in range(len(list)): do_something(list[i]) another_list[i] = list[i-1]

• continue & break may be used

• while condition: a_routine_changing_cond()

N = 10 fib = range(N) for i in range(2, N): fib[i] = fib[i-2] + fib[i-1] # fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

N = 10 fib = [0, 1] while len(fib) < N: fib.append(fib[-2] + fib[-1]) # fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

12

>>> [.5 * i - 1 for i in range(5)] [-1.0, -0.5, 0.0, 0.5, 1.0]

• a tip for list

Others in Python• if, files, functions, etc.

if … elif … else (Python doesn’t have switch … case)

• if condition1: do_something1() elif condition2: do_something2() elif condition3: do_something3() else: do_otherthing()

N = 10 fib = [] while True: if len(fib) < min(N, 2): fib.append(len(fib)) elif len(fib) < N: fib.append(fib[-2] + fib[-1]) continue else: break # fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

N = 10 fib = [] for i in range(N): if i < 2: fib.append(i) else: fib.append(fib[-2] + fib[-1]) # fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

14

File I/O• f = open(filename, mode)

- mode: “r” for read, “w” for write, “a” for append

• load file contents- f.read()- f.readline()- f.readlines()

• save contents- f.write()- f.writelines()

• f.close()

• useful functions- split, strip

filename = "dict_test.txt"

fobj = open(filename, "r") lines = fobj.readlines() fobj.close()

d = {}

for line in lines: pair = line.split(":") key, value = [item.strip() for item in pair] d[key] = value key_list = list(d.keys()) key_list.sort() fobj = open(filename, "w")

for key in key_list: fobj.write("{0}: {1}\n".format(key, d[key])) fobj.close()

15

Functions• def do_something(arg=False):

if arg is False: pass else: print arg

• def do_something1(arg1=False, *arg): if arg1 is False: pass else: do_something(*arg)

• def do_something2(arg2=False, **kwarg): if arg2 is False: pass else: do_something(**kwarg)

16

def fib(n): if n < 1: return 0 elif n < 2: return 1 else: return fib(n-2) + fib(n-1) # [fib(i) for i in range(10)] # = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

def dict_to_file(filename, dictionary, mode="w"): with open(filename, mode) as f: for key in dictionary.keys(): f.write("{0}: {1}\n".format(key, dictionary[key]))

def file_to_dict(filename): d = {} with open(filename) as file_object: for line in file_object: pair = line.split(":") key, val = [i.strip() for i in pair] d[key] = val return d

Modules• numpy (array) • matplotlib (pyplot)

• NumPy array

>>> import numpy

>>> b = numpy.array(a)

>>> b[0, 1]2

>>> b[1]array([4, 5, 6])

>>> b[1, :]array([4, 5, 6])

>>> b[:, 1]array([2, 5])

List vs. NumPy array• List

>>> a = [[1, 2, 3], [4, 5, 6]]

>>> a[0][1]2

>>> a[1][4, 5, 6]

>>> a[1][:][4, 5, 6]

>>> a[:][1][4, 5, 6]

18

Some functions for array• Arithmetic operators (+,-,*,/) on arrays

apply elementwise, so the two array must have the same shape.

• to create array - array, ones, zeros, empty, …

• to stack together- hstack, vstack, concatenate, …

• to split into parts- hsplit, vsplit, …

• to manipulate shape - reshape, resize, ravel, …

• to load/save file- loadtxt, savetxt, fromfile, tofile, …

In [1]: import numpy

In [2]: a = numpy.array([1, 1])

In [3]: b = numpy.ones(2)

In [4]: a + b Out[4]: array([ 2., 2.])

In [5]: numpy.hstack((a, b)) Out[5]: array([ 1., 1., 1., 1.])

In [6]: numpy.vstack((a, b)) Out[6]: array([[ 1., 1.], [ 1., 1.]])

In [7]: c = _6

In [8]: c.ndim Out[8]: 2

In [9]: c.shape Out[9]: (2, 2)

In [10]: c.reshape((1, 4)) Out[10]: array([[ 1., 1., 1., 1.]])

In [11]: c.reshape((4)) Out[11]: array([ 1., 1., 1., 1.])

In [12]: c.ravel() Out[12]: array([ 1., 1., 1., 1.])

19

Index tricks for array• Indexing with arrays of indices

In [13]: a = numpy.arange(10)**2

In [14]: idx = numpy.array([[1, 3], [5, 9]])

In [15]: a[idx] Out[15]: array([[ 1, 9], [25, 81]])

In [16]: a.resize((2, 5))

In [17]: a Out[17]: array([[ 0, 1, 4, 9, 16], [25, 36, 49, 64, 81]])

In [18]: idx = numpy.array([[1, 3], [1, 4]])

In [19]: idy = numpy.array([[0, 0], [1, 1]])

In [20]: a[idy, idx] Out[20]: array([[ 1, 9], [36, 81]])

• Indexing with boolean arraysIn [24]: b = (a % 3) == 1

In [25]: b Out[25]: array([[False, True, True, False, True], [ True, False, True, True, False]], dtype=bool)

In [26]: a[b] Out[26]: array([ 1, 4, 16, 25, 49, 64])

In [21]: idy2 = numpy.array([[0], [1]])

In [22]: a[idy2, idx] Out[22]: array([[ 1, 9], [36, 81]])

In [23]: a[0, idx] Out[23]: array([[ 1, 9], [ 1, 16]])

20

Linear algebras & Random numbers

• Simple operations - transpose, trace, dot, eye, …

• Linear algebras (numpy.linalg)- inv, solve, eig, …

• Random numbers (numpy.random)- seed, rand, randn, …

21

In [27]: a = -1 * numpy.eye(2)

In [28]: a Out[28]: array([[-1., -0.], [-0., -1.]])

In [29]: a.dot(a) Out[29]: array([[ 1., 0.], [ 0., 1.]])

In [30]: y = numpy.array([[3],[7]])

In [31]: numpy.linalg.solve(a,y) Out[31]: array([[-3.], [-7.]])

In [32]: numpy.linalg.eig(a) Out[32]: (array([-1., -1.]), array([[ 1., 0.], [ 0., 1.]]))

In [33]: numpy.random.rand(2,3) Out[33]: array([[ 0.47046349, 0.69026221, 0.30952254], [ 0.7718955 , 0.45324524, 0.48673549]])

In [34]: numpy.random.randn(2,3) Out[34]: array([[-0.36871181, 0.20575024, -0.81693305], [-0.10137747, 0.50682285, 1.57086067]])

matplotlib.pyplot• plotting functions

- plot, scatter, hist, loglog, semilogx, semilogy, …

• to make multiple plots- subplot, subplot2grid, … cf) GridSpec

• other functions - figure, title, xlabel, ylabel, xscale, yscale, xlim, ylim, grid, colorbar, show, savefig, clf, close, …

import numpy as np import matplotlib.pyplot as plt

a = np.random.randn(10000)

plt.hist(a, bins=50) plt.title('Standard Normal Distribution Test') plt.ylabel('count') plt.grid(b=True, which='both') plt.savefig('temp.png')

22

subplot(nrows, ncols, plot_number)

23

import numpy as np import matplotlib.pyplot as plt import scipy.signal as sg

fsr = 256.0 t = np.arange(-1, 1, 1/fsr) y, yenv = sg.gausspulse(t, fc=5, retenv=True) psd, freq = plt.psd(y, Fs=fsr)

plt.figure(figsize=(12, 6)) plt.subplot(1, 2, 1) plt.plot(t,y, t,yenv,'r--', t,-yenv,'r--') plt.title("sine-Gaussian waveform") plt.xlabel("t") plt.grid()

plt.subplot(2, 2, 2) plt.plot(freq, psd) plt.title("PSD") plt.grid() plt.subplot(224) plt.loglog(freq, psd) plt.xlabel("frequency") plt.xlim(1, fsr/2) plt.ylim(1e-15, 1e-2) plt.grid(b=True, which="both")

plt.savefig("temp.png")

24

Other modules you might be interested in

• SciPy: http://www.scipy.org/

• SymPy: http://www.sympy.org/

• scikit-learn: http://scikit-learn.org/

• MPI for Python: http://pythonhosted.org/mpi4py/

• Cython: http://cython.org/

• PyOpenCL: http://mathema.tician.de/software/pyopencl/

• PyCuda: http://mathema.tician.de/software/pycuda/

25

[https://xkcd.com/353/]

감 사 합 니 다

Thank you