0% found this document useful (0 votes)
58 views41 pages

Python Basics for Machine Learning

This document provides an introduction and tutorial to Python and related machine learning libraries for a Machine Learning Lab course. It covers installing Python, the Python shell and files, Python programming concepts like control flow, functions, and data structures. It also introduces commonly used scientific computing and machine learning libraries like NumPy, Pandas, Matplotlib and scikit-learn. Specific NumPy topics covered include ndarray, operations, and random number generation. The document is intended to provide basic programming skills for students in the Machine Learning Lab course.

Uploaded by

Manish Sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views41 pages

Python Basics for Machine Learning

This document provides an introduction and tutorial to Python and related machine learning libraries for a Machine Learning Lab course. It covers installing Python, the Python shell and files, Python programming concepts like control flow, functions, and data structures. It also introduces commonly used scientific computing and machine learning libraries like NumPy, Pandas, Matplotlib and scikit-learn. Specific NumPy topics covered include ndarray, operations, and random number generation. The document is intended to provide basic programming skills for students in the Machine Learning Lab course.

Uploaded by

Manish Sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 對於「縮排」是非常嚴謹的,而非使
用大括號或者關鍵字

[Link]
Machine Learning Lab, NCTU ED912

How to install Python 3 ?


Python Software Foundation Anaconda distribution

[Link] [Link]
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 !!
[Link]
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 [Link]
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 中元素進行自訂的級聯運算
[Link]
[Link]
Machine Learning Lab, NCTU ED912

Python Class (Encapsulation)

Private:朕賜給你的,才是你的﹐朕不給﹐你不能搶。
Machine Learning Lab, NCTU ED912

Python Class (Inheritance)

[Link]
Machine Learning Lab, NCTU ED912

Python Class (Polymorphism)

[Link]
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
[Link]
Machine Learning Lab, NCTU ED912

B
A
Install numpy
$ pip3 install numpy

[Link]

[Link]
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
[Link]([1,2,3]) One dimensional array
[Link]([(1,2,3),(4,5,6)]) Two dimensional array

NumPy [Link](3)
[Link]((3,4))
1D array of length 3 all values 0
3x4 array with all values 1
Creating [Link](5) 5x5 array of 0 with 1 on diagonal (Identity matrix)

Arrays [Link](0,100,6)
[Link](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])
[Link]((2,3),8) 2x3 array with all values 8

[Link]
Machine Learning Lab, NCTU ED912

Matrix ※ Scalar add subtract multiply divide power mod

operator

Operations Notes

NumPy [Link](arr,1)
[Link](arr,2)
Add 1 to each array element
Subtract 2 from each array element
Arithmetic [Link](arr,3) Multiply each array element by 3

Operations [Link](arr,4)
[Link](arr,5)
Divide each array element by 4
Raise each array element to the 5th power
[Link](arr,6) The element-wise remainder of the quotient.

# (returns [Link] for division by zero)

[Link]
Machine Learning Lab, NCTU ED912
Array ※ Array add subtract multiply divide power mod

operator

Operations Notes
[Link](arr1,arr2) Elementwise add arr2 to arr1
[Link](arr1,arr2) Elementwise subtract arr2 from arr1
[Link](arr1,arr2) Elementwise multiply arr1 by arr2
NumPy [Link](arr1,arr2) Dot product of two arrays
Arithmetic [Link](arr1,arr2) Matrix product of two arrays.

Operations [Link](arr1,arr2)
[Link](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 @
[Link]
Machine Learning Lab, NCTU ED912

Operations Notes
[Link](arr) Square root of each element in the array
[Link](arr) Sine of each element in the array
NumPy [Link](arr) Natural log of each element in the array

Arithmetic [Link](arr)
[Link](arr)
Absolute value of each element in the array
Rounds up to the nearest int
Operations [Link](arr) Rounds down to the nearest int
[Link](arr) Rounds to the nearest int

[Link]
Machine Learning Lab, NCTU ED912

import [Link]
[Link]
1.15.0/reference/[Link]

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

[Link]
Machine Learning Lab, NCTU ED912
[Link]

NumPy Operations Notes


Statistics [Link](arr,axis=0) Returns mean along specific axis
[Link]() Returns sum of arr
[Link]() Returns minimum value of arr
[Link](axis=0) Returns maximum value of specific axis
[Link](arr) Returns the variance of array
[Link](arr,axis=1) Returns the standard deviation of specific axis
[Link]() Returns correlation coefficient of array
[Link]
Machine Learning Lab, NCTU ED912

Matrix eigenvalues

NumPy
Linear Norms and other numbers

Algebra
Solving equations & inverting matrices

[Link]
Machine Learning Lab, NCTU ED912

Matplotlib Tutorials
[Link]
Machine Learning Lab, NCTU ED912

Install Matplotlib
B
A
$ pip3 install matplotlib

[Link]
Machine Learning Lab, NCTU ED912

Matplotlib Basic

[Link]
Machine Learning Lab, NCTU ED912

Matplotlib scatter
Machine Learning Lab, NCTU ED912

Matplotlib bar
Machine Learning Lab, NCTU ED912

Matplotlib Image
$ pip3 install Pillow
[Link]

[Link]
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
[Link]
Machine Learning Lab, NCTU ED912

scikit-learn
Some machine learning algorithm

Check this website:

[Link]
Machine Learning Lab, NCTU ED912

scikit-learn
[Link]

Cross-validation
Machine Learning Lab, NCTU ED912

scikit-learn [Link]
Machine Learning Lab, NCTU ED912

scikit-learn [Link]
Machine Learning Lab, NCTU ED912

Try to find the answer for yourself!

[Link]

Try to understand,
don't PLAGIARISM.

You might also like