BASIC PYTHON USAGE

LOAD PACKAGES


In [1]:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
print ("PACKAGES LOADED")


PACKAGES LOADED

In [2]:
print ("Hello, world")
# THERE ARE THREE POPULAR TYPES
# 1. INTEGER
x = 3;
print ("Integer: %01d, %02d, %03d, %04d, %05d" 
       % (x, x, x, x, x))
# 2. FLOAT
x = 123.456;
print ("Float: %.0f, %.1f, %.2f, %1.2f, %2.2f" 
       % (x, x, x, x, x))
# 3. STRING
x = "Hello, world"
print ("String: [%s], [%3s], [%20s]" 
       % (x, x, x))


Hello, world
Integer: 3, 03, 003, 0003, 00003
Float: 123, 123.5, 123.46, 123.46, 123.46
String: [Hello, world], [Hello, world], [        Hello, world]

FOR LOOP + IF/ELSE


In [3]:
dlmethods = ["ANN", "MLP", "CNN", "RNN", "DAE"]
for alg, i in zip(dlmethods, range(len(dlmethods))):
    if alg in ["ANN", "MLP", "CNN"]:
        print ("[%d/%d] %s is a feed-forward network." 
               % (i, len(dlmethods), alg))
    elif alg in ["RNN"]:
        print ("[%d/%d] %s is a recurrent network." 
               % (i, len(dlmethods), alg))
    else:
        print ("[%d/%d] %s is an unsupervised method." 
               % (i, len(dlmethods), alg))


[0/5] ANN is a feed-forward network.
[1/5] MLP is a feed-forward network.
[2/5] CNN is a feed-forward network.
[3/5] RNN is a recurrent network.
[4/5] DAE is an unsupervised method.

FUNCTION


In [4]:
def sum(a, b):
    return a+b
X = 10.
Y = 20.
# Usage 
print ("%.1f + %.1f = %.1f" % (X, Y, sum(X, Y)))


10.0 + 20.0 = 30.0

STRING OPERATION


In [5]:
head = "Deep learning" 
body = "very "
tail = "HARD."
print (head + " is " + body + tail)

# Repeat words
print (head + " is " + body*3 + tail)
print (head + " is " + body*10 + tail)

# It is used in this way
print ("\n" + "="*50)
print (" "*15 + "It is used in this way")
print ("="*50 + "\n")

# Indexing characters in the string
x = "Hello, world" 
for i in range(len(x)):
    print ("Index: [%02d/%02d] Char: %s" 
           % (i, len(x), x[i]))


Deep learning is very HARD.
Deep learning is very very very HARD.
Deep learning is very very very very very very very very very very HARD.

==================================================
               It is used in this way
==================================================

Index: [00/12] Char: H
Index: [01/12] Char: e
Index: [02/12] Char: l
Index: [03/12] Char: l
Index: [04/12] Char: o
Index: [05/12] Char: ,
Index: [06/12] Char:  
Index: [07/12] Char: w
Index: [08/12] Char: o
Index: [09/12] Char: r
Index: [10/12] Char: l
Index: [11/12] Char: d

STRING INDEXING


In [6]:
x = "20160607Cloudy"
year = x[:4]
day = x[4:8]
weather = x[8:]
print ("[%s] -> [%s] + [%s] + [%s] " 
       % (x, year, day, weather))


[20160607Cloudy] -> [2016] + [0607] + [Cloudy] 

LIST


In [7]:
a = []
b = [1, 2, 3]
c = ["Hello", ",", "world"]
d = [1, 2, 3, "x", "y", "z"]
print a, b, c, d


[] [1, 2, 3] ['Hello', ',', 'world'] [1, 2, 3, 'x', 'y', 'z']

In [8]:
x = []
print x
x.append('a')
print x


[]
['a']

In [9]:
x.append(123)
print x


['a', 123]

In [10]:
x.append(["a", "b"])
print x


['a', 123, ['a', 'b']]

DICTIONARY


In [11]:
dic = dict()
dic["name"] = "Sungjoon"
dic["age"] = 32
dic["job"] = "Ph.D. Candidate"
print dic


{'job': 'Ph.D. Candidate', 'age': 32, 'name': 'Sungjoon'}

In [12]:
print dic["job"]


Ph.D. Candidate

CLASS


In [13]:
class Greeter:
    def __init__(self, name='Sam'): # CONSTRUCTOR
        self.name = name  
    def greet(self, loud=False): # MEMBER FUNCTION
        if loud:
            print ('HELLO, %s!' % self.name.upper())
        else:
            print ('Hello, %s' % self.name)

In [14]:
g = Greeter('Fred')  
g.greet()            
g.greet(loud=True)


Hello, Fred
HELLO, FRED!

In [15]:
d = Greeter()  
d.greet()            
d.greet(loud=True)


Hello, Sam
HELLO, SAM!

NUMPY


In [16]:
def print_np(x):
    print ("TYPE IS %s" % (type(x)))
    print ("SHAPE IS %s" % (x.shape,))
    print ("VALUES ARE \n%s" % (x))

RANK 1 ARRAY


In [17]:
x = np.array([1, 2, 3]) # rank 1 array
print_np(x)
x[0] = 5 # INDEX STARTS WITH 0
print_np(x)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (3,)
VALUES ARE 
[1 2 3]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (3,)
VALUES ARE 
[5 2 3]

RANK 2 ARRAY


In [18]:
y = np.array([[1,2,3], [4,5,6]]) 
print_np(y)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 3)
VALUES ARE 
[[1 2 3]
 [4 5 6]]

ZEROS


In [19]:
a = np.zeros((3, 2))  
print_np(a)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (3, 2)
VALUES ARE 
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

ONES


In [20]:
b = np.ones((1, 2))   
print_np(b)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (1, 2)
VALUES ARE 
[[ 1.  1.]]

IDENTITY


In [21]:
c = np.eye(2, 2)   
print_np(c)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[ 1.  0.]
 [ 0.  1.]]

RANDOM (UNIFORM)


In [22]:
d = np.random.random((2, 2))    
print_np(d)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[ 0.45951498  0.47019354]
 [ 0.38390971  0.95262032]]

RANDOM (GAUSSIAN)


In [23]:
e = np.random.randn(1, 10)    
print_np(e)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (1, 10)
VALUES ARE 
[[-1.64505615 -0.52916241  0.47980778  0.73932641  1.85015705  0.63813784
  -2.2290135  -0.21253559  0.17094529  0.96313682]]

In [24]:
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print_np(a)

# INDEXING
b = a[:2, 1:3]
print_np(b)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (3, 4)
VALUES ARE 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[2 3]
 [6 7]]

GET ROW (IMPORTANT!!)


In [25]:
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print_np(a)

row_r1 = a[1, :]    # Rank 1 view of the second row of a  
row_r2 = a[1:2, :]  # Rank 2 view of the second row of a
row_r3 = a[[1], :]  # Rank 2 view of the second row of a

print_np(row_r1)
print_np(row_r2)
print_np(row_r3)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (3, 4)
VALUES ARE 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (4,)
VALUES ARE 
[5 6 7 8]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (1, 4)
VALUES ARE 
[[5 6 7 8]]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (1, 4)
VALUES ARE 
[[5 6 7 8]]

OPERATIONS

ADD


In [26]:
x = np.array([[1,2],[3,4]], dtype=np.float32)
y = np.array([[5,6],[7,8]], dtype=np.float32)
# Elementwise sum; both produce the array
print x + y
print np.add(x, y)


[[  6.   8.]
 [ 10.  12.]]
[[  6.   8.]
 [ 10.  12.]]

SUBSTRACT


In [27]:
print x - y
print np.subtract(x, y)


[[-4. -4.]
 [-4. -4.]]
[[-4. -4.]
 [-4. -4.]]

MULTIPLY


In [28]:
print x * y
print np.multiply(x, y)


[[  5.  12.]
 [ 21.  32.]]
[[  5.  12.]
 [ 21.  32.]]

DIVIDE


In [29]:
print x / y
print np.divide(x, y)


[[ 0.2         0.33333334]
 [ 0.42857143  0.5       ]]
[[ 0.2         0.33333334]
 [ 0.42857143  0.5       ]]

DOT


In [30]:
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
print_np(x)
print_np(y)
print_np(v)
print_np(w)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[1 2]
 [3 4]]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[5 6]
 [7 8]]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2,)
VALUES ARE 
[ 9 10]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2,)
VALUES ARE 
[11 12]

In [31]:
# VECTOR / VECTOR PRODUCT => SCALAR 
print_np(np.dot(v, w)) # <= v * w'
# MATRIX / VECTOR PRODUCT => RANK 1 
print_np(np.dot(x, v)) # <= x * v'
# MATRIX / MATRIX PRODUCT => RANK 2 
print_np(np.dot(x, y))


TYPE IS <type 'numpy.int64'>
SHAPE IS ()
VALUES ARE 
219
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2,)
VALUES ARE 
[29 67]
TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[19 22]
 [43 50]]

SUM


In [32]:
x = np.array([[1,2],[3,4]])
print_np(x)
print x
print x.T
print np.sum(x)  # Compute sum of all elements
print np.sum(x, axis=0)  # Compute sum of each column
print np.sum(x, axis=1)  # Compute sum of each row


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[1 2]
 [3 4]]
[[1 2]
 [3 4]]
[[1 3]
 [2 4]]
10
[4 6]
[3 7]

TILE


In [33]:
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1))  # Stack 4 copies of v on top of each other
print_np(vv)


TYPE IS <type 'numpy.ndarray'>
SHAPE IS (4, 3)
VALUES ARE 
[[1 0 1]
 [1 0 1]
 [1 0 1]
 [1 0 1]]

MATPLOTLIB


In [34]:
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)

# Plot the points using matplotlib
plt.plot(x, y)


Out[34]:
[<matplotlib.lines.Line2D at 0x7f9bb635de90>]

In [35]:
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])

# Show the figure.
plt.show()



In [36]:
# Compute the x and y coordinates for points 
# on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)

# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')

# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')

# Show the figure.
plt.show()


SOME USEFUL COMMANDS

WHOS: SEE VARIABLES


In [37]:
whos


Variable    Type                Data/Info
-----------------------------------------
Greeter     classobj            __main__.Greeter
X           float               10.0
Y           float               20.0
a           ndarray             3x4: 12 elems, type `int64`, 96 bytes
alg         str                 DAE
b           ndarray             2x2: 4 elems, type `int64`, 32 bytes
body        str                 very 
c           ndarray             2x2: 4 elems, type `float64`, 32 bytes
d           ndarray             2x2: 4 elems, type `float64`, 32 bytes
day         str                 0607
dic         dict                n=3
dlmethods   list                n=5
e           ndarray             1x10: 10 elems, type `float64`, 80 bytes
g           __main__.Greeter    <__main__.Greeter instance at 0x7f9bb8736d88>
head        str                 Deep learning
i           int                 11
np          module              <module 'numpy' from '/us<...>ages/numpy/__init__.pyc'>
plt         module              <module 'matplotlib.pyplo<...>s/matplotlib/pyplot.pyc'>
print_np    function            <function print_np at 0x7f9c04579398>
row_r1      ndarray             4: 4 elems, type `int64`, 32 bytes
row_r2      ndarray             1x4: 4 elems, type `int64`, 32 bytes
row_r3      ndarray             1x4: 4 elems, type `int64`, 32 bytes
sum         function            <function sum at 0x7f9bb8423e60>
tail        str                 HARD.
tf          module              <module 'tensorflow' from<...>tensorflow/__init__.pyc'>
v           ndarray             3: 3 elems, type `int64`, 24 bytes
vv          ndarray             4x3: 12 elems, type `int64`, 96 bytes
w           ndarray             2: 2 elems, type `int64`, 16 bytes
weather     str                 Cloudy
x           ndarray             95: 95 elems, type `float64`, 760 bytes
y           ndarray             95: 95 elems, type `float64`, 760 bytes
y_cos       ndarray             95: 95 elems, type `float64`, 760 bytes
y_sin       ndarray             95: 95 elems, type `float64`, 760 bytes
year        str                 2016

CTRL + SHIFT + '-' : SPLIT CELL

SHIFT + UP/DOWN: SELECT MULTIPLE CELLS

SHIFT + M: MERGE MULTIPLE SELECTED CELLS

%env: ENVIRONMENT VARIABLES


In [38]:
%env


Out[38]:
{'BAZELRC': '/root/.bazelrc',
 'BAZEL_VERSION': '0.4.1',
 'CLICOLOR': '1',
 'CUDA_PKG_VERSION': '8-0=8.0.44-1',
 'CUDA_VERSION': '8.0',
 'CUDNN_VERSION': '5',
 'DISPLAY': ':0',
 'GIT_PAGER': 'cat',
 'HOME': '/home/sj',
 'HOSTNAME': 'aacb90176b41',
 'JPY_PARENT_PID': '376',
 'LD_LIBRARY_PATH': '/usr/local/cuda/extras/CUPTI/lib64:/usr/local/nvidia/lib:/usr/local/nvidia/lib64:',
 'LESSCLOSE': '/usr/bin/lesspipe %s %s',
 'LESSOPEN': '| /usr/bin/lesspipe %s',
 'LIBRARY_PATH': '/usr/local/cuda/lib64/stubs:',
 'LOGNAME': 'sj',
 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:',
 'MAIL': '/var/mail/sj',
 'MPLBACKEND': 'module://ipykernel.pylab.backend_inline',
 'OLDPWD': '/home/sj',
 'PAGER': 'cat',
 'PATH': '/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games',
 'PWD': '/home/sj/notebooks',
 'SHELL': '/bin/bash',
 'SHLVL': '2',
 'TERM': 'xterm-color',
 'TF_CUDA_COMPUTE_CAPABILITIES': '3.0,3.5,5.2',
 'TF_NEED_CUDA': '1',
 'USER': 'sj',
 '_': '/bin/sh'}

%run: RUN OTHER PYTHON FILES OR JUPYTER NOTEBOOKS


In [39]:
%who


Greeter	 X	 Y	 a	 alg	 b	 body	 c	 d	 
day	 dic	 dlmethods	 e	 g	 head	 i	 np	 plt	 
print_np	 row_r1	 row_r2	 row_r3	 sum	 tail	 tf	 v	 vv	 
w	 weather	 x	 y	 y_cos	 y_sin	 year	 

%timeit: TIMING


In [40]:
%timeit np.random.normal(size=1000)


The slowest run took 5.83 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 39.6 µs per loop

%pdb: TOGGLE DEBUG


In [43]:
%pdb 
zzz
%pdb


Automatic pdb calling has been turned ON
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-43-a20a9c0e120e> in <module>()
      1 get_ipython().magic(u'pdb')
----> 2 zzz
      3 get_ipython().magic(u'pdb')

NameError: name 'zzz' is not defined
> <ipython-input-43-a20a9c0e120e>(2)<module>()
      1 get_ipython().magic(u'pdb')
----> 2 zzz
      3 get_ipython().magic(u'pdb')

ipdb> ㅊ

Documented commands (type help <topic>):
========================================
EOF    bt         cont      enable  jump  pdef    psource  run      unt   
a      c          continue  exit    l     pdoc    q        s        until 
alias  cl         d         h       list  pfile   quit     step     up    
args   clear      debug     help    n     pinfo   r        tbreak   w     
b      commands   disable   ignore  next  pinfo2  restart  u        whatis
break  condition  down      j       p     pp      return   unalias  where 

Miscellaneous help topics:
==========================
exec  pdb

Undocumented commands:
======================
ll  longlist  retval  rv

ipdb> c
array([[ 1.,  0.],
       [ 0.,  1.]])
ipdb> a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
ipdb> tf.nn
<module 'tensorflow.python.ops.nn' from '/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn.pyc'>
ipdb> list
      1 get_ipython().magic(u'pdb')
----> 2 zzz
      3 get_ipython().magic(u'pdb')

ipdb> help

Documented commands (type help <topic>):
========================================
EOF    bt         cont      enable  jump  pdef    psource  run      unt   
a      c          continue  exit    l     pdoc    q        s        until 
alias  cl         d         h       list  pfile   quit     step     up    
args   clear      debug     help    n     pinfo   r        tbreak   w     
b      commands   disable   ignore  next  pinfo2  restart  u        whatis
break  condition  down      j       p     pp      return   unalias  where 

Miscellaneous help topics:
==========================
exec  pdb

Undocumented commands:
======================
ll  longlist  retval  rv

ipdb> continue