NCTU ECM9042 Machine Learning Lab, NCTU ED912
ECM9042-Deep Learning
Basic Programming Skill Tutorial
Machine Learning Lab, NCTU ED912
PYTHON
Introduction
Machine Learning Lab, NCTU ED912
Python {…}
• Python 是一種直譯語言 Interpreted Language
• 1991 年由 Guido van Rossum 於阿姆斯特丹
大學創作及發展
• Python 對於「縮排」是非常嚴謹的,而非使
用大括號或者關鍵字
https://en.wikipedia.org/wiki/Python_(programming_language)
Machine Learning Lab, NCTU ED912
How to install Python 3 ?
Python Software Foundation Anaconda distribution
https://www.python.org/ https://www.anaconda.com/distribution/
Machine Learning Lab, NCTU ED912
Python 3 on Linux
Ubuntu already has Python3
Ubuntu18.04 has Python3.6 Ubuntu16.04 has Python3.5
If you still want to update python on 16.04…
Run the commands carefully !!
http://ubuntuhandbook.org/index.php/2017/07/
install-python-3-6-1-in-ubuntu-16-04-lts/
Machine Learning Lab, NCTU ED912
Python Shell & File
Python Shell
Run .py File
Machine Learning Lab, NCTU ED912
OpenCV
Scientific Computing Modules
NumPy Matplotlib SciPy
scikit-learn Pandas Natural Language Toolkit
Machine Learning Lab, NCTU ED912
Python Programming Tutorials
Machine Learning Lab, NCTU ED912
Python Control Flow
• if…elif…else
• for…in
• While
Machine Learning Lab, NCTU ED912
Python Iteration & Loop
A for loop is used for iterating over
a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
To loop through a set of code a
specified number of times, we can
use the range() function.
Machine Learning Lab, NCTU ED912
Python Function
Hello MLLAB !!
Hello Jacky !!
Hello Jessica !!
Machine Learning Lab, NCTU ED912
Python Iterator and Generator
Iterator apple
banana
cherry
1
Generator 2
3
Machine Learning Lab, NCTU ED912
Python Data Structures
• Lists
• Tuples
• Dictionaries
• Strings
• Sets
• Frozensets
• Bytes
• Bytearrays https://docs.python.org/3/tutorial/datastructures.html
Machine Learning Lab, NCTU ED912
Common built-in function
Function Notes
all 列表中所有元素為真
any 列表中只要有任何元素為真
enumerate 取得列表中每個元素及其索引值 (i, element)
zip 合併將多個 list 合併成 tuple of list
range 取得一序列
sort 針對 list 進行排序(由小到大),會改變原來的 list
sorted 針對 list 進行排序(由小到大),不會改變原來的 list
map 針對 list 中元素進行自訂的函數
filter 針對 list 中元素進行自訂的條件過濾
reduce 將list 中元素進行自訂的級聯運算
https://book.pythontips.com/en/latest/map_filter.html
https://blog.kdchang.cc/2016/10/30/python101-tutorial/
Machine Learning Lab, NCTU ED912
Python Class (Encapsulation)
Private:朕賜給你的,才是你的﹐朕不給﹐你不能搶。
Machine Learning Lab, NCTU ED912
Python Class (Inheritance)
https://www.brilliantcode.net/761/python-3-6-class/
Machine Learning Lab, NCTU ED912
Python Class (Polymorphism)
https://www.brilliantcode.net/761/python-3-6-class/
Machine Learning Lab, NCTU ED912
An Intro to Threading in Python
Main
Thread Protect shared resource
• Lock
• Semaphore
Fork • RLock
…...
Thread 0
Thread 1 Join
Thread 4
Thread 3
Thread 2
Done.
Machine Learning Lab, NCTU ED912
NumPy Tutorials
https://docs.scipy.org/doc/numpy/user/quickstart.html
Machine Learning Lab, NCTU ED912
B
A
Install numpy
$ pip3 install numpy
https://pypi.org/project/numpy/#files
https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Machine Learning Lab, NCTU ED912
NumPy ndarray
• ndarray is a fast and space-efficient multidimensional array
providing vectorized arithmetic operations and sophisticated
broadcasting capabilities
Machine Learning Lab, NCTU ED912
import numpy as np
Operations Notes
np.array([1,2,3]) One dimensional array
np.array([(1,2,3),(4,5,6)]) Two dimensional array
NumPy np.zeros(3)
np.ones((3,4))
1D array of length 3 all values 0
3x4 array with all values 1
Creating np.eye(5) 5x5 array of 0 with 1 on diagonal (Identity matrix)
Arrays np.linspace(0,100,6)
np.arange(0,10,3)
Array of 6 evenly divided values from 0 to 100
Array of values from 0 to less than 10 with step 3(eg. [0,3,6,9])
np.full((2,3),8) 2x3 array with all values 8
https://dataquest.io/blog/numpy-cheat-sheet/
Machine Learning Lab, NCTU ED912
Matrix ※ Scalar add subtract multiply divide power mod
operator
Operations Notes
NumPy np.add(arr,1)
np.subtract(arr,2)
Add 1 to each array element
Subtract 2 from each array element
Arithmetic np.multiply(arr,3) Multiply each array element by 3
Operations np.divide(arr,4)
np.power(arr,5)
Divide each array element by 4
Raise each array element to the 5th power
np.mod(arr,6) The element-wise remainder of the quotient.
# (returns np.nan for division by zero)
https://dataquest.io/blog/numpy-cheat-sheet/
Machine Learning Lab, NCTU ED912
Array ※ Array add subtract multiply divide power mod
operator
Operations Notes
np.add(arr1,arr2) Elementwise add arr2 to arr1
np.subtract(arr1,arr2) Elementwise subtract arr2 from arr1
np.multiply(arr1,arr2) Elementwise multiply arr1 by arr2
NumPy np.dot(arr1,arr2) Dot product of two arrays
Arithmetic np.matmul(arr1,arr2) Matrix product of two arrays.
Operations np.divide(arr1,arr2)
np.power(arr1,arr2)
Elementwise divide arr1 by arr2
Elementwise raise arr1 raised to the power of arr2
np.array_equal(arr1,arr2) Returns True if the arrays have the same elements and shape
Matrix ※ Matrix matmul
operator @
https://dataquest.io/blog/numpy-cheat-sheet/
Machine Learning Lab, NCTU ED912
Operations Notes
np.sqrt(arr) Square root of each element in the array
np.sin(arr) Sine of each element in the array
NumPy np.log(arr) Natural log of each element in the array
Arithmetic np.abs(arr)
np.ceil(arr)
Absolute value of each element in the array
Rounds up to the nearest int
Operations np.floor(arr) Rounds down to the nearest int
np.round(arr) Rounds to the nearest int
https://dataquest.io/blog/numpy-cheat-sheet/
Machine Learning Lab, NCTU ED912
import numpy.random
https://docs.scipy.org/doc/numpy-
1.15.0/reference/routines.random.html
Operations Notes
rand(d0, d1, …, dn) Random values in a given shape
NumPy randn(d0, d1, …, dn) Return samples from the "standard normal" distribution
randint(low[, high, size, dtype]) Return random integers from low (inclusive) to high (exclusive)
Random random_integers(low[, high, size]) Random integers between low and high, inclusive
sampling random_sample([size]) Return random floats in the half-open interval [0.0, 1.0)
random([size]) Return random floats in the half-open interval [0.0, 1.0)
ranf([size]) Return random floats in the half-open interval [0.0, 1.0)
sample([size]) Return random floats in the half-open interval [0.0, 1.0)
choice(a[, size, replace, p]) Generates a random sample from a given 1-D array
bytes(length) Return random bytes
https://dataquest.io/blog/numpy-cheat-sheet/
Machine Learning Lab, NCTU ED912
https://docs.scipy.org/doc/numpy-1.17.0/reference/routines.statistics.html
NumPy Operations Notes
Statistics np.mean(arr,axis=0) Returns mean along specific axis
arr.sum() Returns sum of arr
arr.min() Returns minimum value of arr
arr.max(axis=0) Returns maximum value of specific axis
np.var(arr) Returns the variance of array
np.std(arr,axis=1) Returns the standard deviation of specific axis
arr.corrcoef() Returns correlation coefficient of array
https://dataquest.io/blog/numpy-cheat-sheet/
Machine Learning Lab, NCTU ED912
Matrix eigenvalues
NumPy
Linear Norms and other numbers
Algebra
Solving equations & inverting matrices
https://docs.scipy.org/doc/numpy/reference/routines.linalg.html
Machine Learning Lab, NCTU ED912
Matplotlib Tutorials
https://matplotlib.org/3.1.1/tutorials/index.html
Machine Learning Lab, NCTU ED912
Install Matplotlib
B
A
$ pip3 install matplotlib
https://pypi.org/project/matplotlib/#files
Machine Learning Lab, NCTU ED912
Matplotlib Basic
https://matplotlib.org/tutorials/index.html
Machine Learning Lab, NCTU ED912
Matplotlib scatter
Machine Learning Lab, NCTU ED912
Matplotlib bar
Machine Learning Lab, NCTU ED912
Matplotlib Image
$ pip3 install Pillow
https://pillow.readthedocs.io/en/stable/
https://matplotlib.org/tutorials/introductory/images.html
Machine Learning Lab, NCTU ED912
Matplotlib Image
Numpy Array
Shape: (height, width, channel)
[[[0.40784314 0.40784314 0.40784314]
[0.40784314 0.40784314 0.40784314]
[0.40784314 0.40784314 0.40784314]
...
[0.42745098 0.42745098 0.42745098]
[0.42745098 0.42745098 0.42745098]
[0.42745098 0.42745098 0.42745098]]
...
[[0.44313726 0.44313726 0.44313726]
[0.4509804 0.4509804 0.4509804 ]
[0.4509804 0.4509804 0.4509804 ]
...
[0.44705883 0.44705883 0.44705883]
[0.44705883 0.44705883 0.44705883]
[0.44313726 0.44313726 0.44313726]]]
Machine Learning Lab, NCTU ED912
scikit-learn Tutorials
https://scikit-learn.org/stable/tutorial/index.html
Machine Learning Lab, NCTU ED912
scikit-learn
Some machine learning algorithm
Check this website:
https://scikit-learn.org/stable/user_guide.html
Machine Learning Lab, NCTU ED912
scikit-learn
https://scikit-learn.org/stable/user_guide.html
Cross-validation
Machine Learning Lab, NCTU ED912
scikit-learn https://scikit-learn.org/stable/user_guide.html
Machine Learning Lab, NCTU ED912
scikit-learn https://scikit-learn.org/stable/user_guide.html
Machine Learning Lab, NCTU ED912
Try to find the answer for yourself!
https://docs.python.org/3/
Try to understand,
don't PLAGIARISM.