<jupyter_start><jupyter_text>BASIC PYTHON USAGE  LOAD PACKAGES<jupyter_code>import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
print ("PACKAGES LOADED")<jupyter_output>PACKAGES LOADED<jupyter_text>PRINT FUNCTION<jupyter_code>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))<jupyter_output>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]<jupyter_text>FOR LOOP + IF/ELSE<jupyter_code>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))<jupyter_output>[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.<jupyter_text>FUNCTION<jupyter_code>def sum(a, b):
    return a+b
X = 10.
Y = 20.
# Usage 
print ("%.1f + %.1f = %.1f" % (X, Y, sum(X, Y)))<jupyter_output>10.0 + 20.0 = 30.0<jupyter_text>STRING OPERATION<jupyter_code>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]))<jupyter_output>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<jupyter_text>STRING INDEXING<jupyter_code>x = "20160607Cloudy"
year = x[:4]
day = x[4:8]
weather = x[8:]
print ("[%s] -> [%s] + [%s] + [%s] " 
       % (x, year, day, weather))<jupyter_output>[20160607Cloudy] -> [2016] + [0607] + [Cloudy]<jupyter_text>LIST<jupyter_code>a = []
b = [1, 2, 3]
c = ["Hello", ",", "world"]
d = [1, 2, 3, "x", "y", "z"]
print a, b, c, d
x = []
print x
x.append('a')
print x
x.append(123)
print x
x.append(["a", "b"])
print x<jupyter_output>['a', 123, ['a', 'b']]<jupyter_text>DICTIONARY<jupyter_code>dic = dict()
dic["name"] = "Sungjoon"
dic["age"] = 32
dic["job"] = "Ph.D. Candidate"
print dic
print dic["job"]<jupyter_output>Ph.D. Candidate<jupyter_text>CLASS<jupyter_code>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)
g = Greeter('Fred')  
g.greet()            
g.greet(loud=True)   
d = Greeter()  
d.greet()            
d.greet(loud=True)<jupyter_output>Hello, Sam
HELLO, SAM!<jupyter_text>NUMPY<jupyter_code>def print_np(x):
    print ("TYPE IS %s" % (type(x)))
    print ("SHAPE IS %s" % (x.shape,))
    print ("VALUES ARE \n%s" % (x))<jupyter_output><empty_output><jupyter_text>RANK 1 ARRAY<jupyter_code>x = np.array([1, 2, 3]) # rank 1 array
print_np(x)
x[0] = 5 # INDEX STARTS WITH 0
print_np(x)<jupyter_output>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]<jupyter_text>RANK 2 ARRAY<jupyter_code>y = np.array([[1,2,3], [4,5,6]]) 
print_np(y)<jupyter_output>TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 3)
VALUES ARE 
[[1 2 3]
 [4 5 6]]<jupyter_text>ZEROS<jupyter_code>a = np.zeros((3, 2))  
print_np(a)<jupyter_output>TYPE IS <type 'numpy.ndarray'>
SHAPE IS (3, 2)
VALUES ARE 
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]<jupyter_text>ONES<jupyter_code>b = np.ones((1, 2))   
print_np(b)<jupyter_output>TYPE IS <type 'numpy.ndarray'>
SHAPE IS (1, 2)
VALUES ARE 
[[ 1.  1.]]<jupyter_text>IDENTITY<jupyter_code>c = np.eye(2, 2)   
print_np(c)<jupyter_output>TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[ 1.  0.]
 [ 0.  1.]]<jupyter_text>RANDOM (UNIFORM)<jupyter_code>d = np.random.random((2, 2))    
print_np(d)<jupyter_output>TYPE IS <type 'numpy.ndarray'>
SHAPE IS (2, 2)
VALUES ARE 
[[ 0.45951498  0.47019354]
 [ 0.38390971  0.95262032]]<jupyter_text>RANDOM (GAUSSIAN)<jupyter_code>e = np.random.randn(1, 10)    
print_np(e)
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)<jupyter_output>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]]<jupyter_text>GET ROW (IMPORTANT!!)<jupyter_code>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)<jupyter_output>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]]<jupyter_text>OPERATIONS  ADD<jupyter_code>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)<jupyter_output>[[  6.   8.]
 [ 10.  12.]]
[[  6.   8.]
 [ 10.  12.]]<jupyter_text>SUBSTRACT<jupyter_code>print x - y
print np.subtract(x, y)<jupyter_output>[[-4. -4.]
 [-4. -4.]]
[[-4. -4.]
 [-4. -4.]]<jupyter_text>MULTIPLY<jupyter_code>print x * y
print np.multiply(x, y)<jupyter_output>[[  5.  12.]
 [ 21.  32.]]
[[  5.  12.]
 [ 21.  32.]]<jupyter_text>DIVIDE<jupyter_code>print x / y
print np.divide(x, y)<jupyter_output>[[ 0.2         0.33333334]
 [ 0.42857143  0.5       ]]
[[ 0.2         0.33333334]
 [ 0.42857143  0.5       ]]<jupyter_text>DOT<jupyter_code>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)
# 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))<jupyter_output>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]]<jupyter_text>SUM<jupyter_code>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<jupyter_output>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]<jupyter_text>TILE<jupyter_code>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)<jupyter_output>TYPE IS <type 'numpy.ndarray'>
SHAPE IS (4, 3)
VALUES ARE 
[[1 0 1]
 [1 0 1]
 [1 0 1]
 [1 0 1]]<jupyter_text>MATPLOTLIB<jupyter_code># 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)

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()
# 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()<jupyter_output><empty_output><jupyter_text>SOME USEFUL COMMANDS  WHOS: SEE VARIABLES<jupyter_code>whos<jupyter_output>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      [...]<jupyter_text>CTRL + SHIFT + '-' : SPLIT CELL  SHIFT + UP/DOWN: SELECT MULTIPLE CELLS  SHIFT + M: MERGE MULTIPLE SELECTED CELLS  ?np.add: LINK TO DOCUMENTATION  %env: ENVIRONMENT VARIABLES<jupyter_code>%env<jupyter_output><empty_output><jupyter_text>%run: RUN OTHER PYTHON FILES OR JUPYTER NOTEBOOKS<jupyter_code>%who<jupyter_output>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<jupyter_text>%timeit: TIMING<jupyter_code>%timeit np.random.normal(size=1000)<jupyter_output>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<jupyter_text>%pdb: TOGGLE DEBUG<jupyter_code>%pdb 
zzz
%pdb<jupyter_output>Automatic pdb calling has been turned ON
{'fraction_non_alphanumeric': 0.17584783778934146, 'fraction_numerical': 0.05975237753454154, 'mean_word_length': 3.2464761904761903, 'pattern_counts': {'":': 0, '<': 120, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '31', 'dir': 'jupyter-structured-clean-dedup', 'id': '484824', 'license': 'MIT', 'n_tokens_mistral': 5214, 'n_tokens_neox': 4767, 'n_words': 1286, 'path': 'code/basic_python.ipynb', 'repo_name': 'cpslab-snu/deep_learning', 'size': '11146'}