numpy and matplotlib - chiang mai university...เราจะสร้างvector และ matrix...
TRANSCRIPT
Computer Science, CMU
NumPy and Matplotlib
http://www.engr.ucsb.edu/~shell/che210d/numpy.pdf204101 Introduction to Computer 1
Computer Science, CMU
Overview
NumPy (Numeric Python) เปนโมดลสวนเสรมของ Python ทมฟงกชนเกยวกบคณตศาสตรและการค านวณตางๆ มาใหใชงาน โดยทวไปจะเกยวกบการจดการขอมลชด (Array) ขนาดใหญและเมทรกซ
NumPy นครบคลมการค านวณมากมายสามารถท างานไดใกลเคยงกบcommercial software เชน MatLab เลยทเดยว
เนองจาก NumPy มความสามารถมากในรายวชานเราจะเรยนเกยวกบVector and matrix mathematics
204101 Introduction to Computer 2
Computer Science, CMU
การ Import NumPy module
มหลายวธในการ import NumPy เขามาใชงาน โดยทวไปวธมาตรฐานไดแกค าสง
import numpy
อยางไรกตามหากมการเรยกใชงาน NumPy บอยครง แตละครงจะตองพมพ numpy.X ดงนนเพอความสะดวก เราจะยอเวลาเรยกเปน np เราจะเปลยนการ import เปน
import numpy as np
ท าใหเราเรยกใชงานฟงกชนไดโดยเขยนเปน np.X
204101 Introduction to Computer 3
Computer Science, CMU
Arrays
Arrays เปนคณสมบตหลกของ NumPy มลกษณะคลายกบ list ยกเวนสมาชกทกตวใน array จะตองเปนขอมลชนดเดยวกน โดยทวไปแลวขอมลทเกบจะเปนตวเลขเชน int หรอ float
Arrays มความสามารถในการด าเนนการเกยวกบขอมลทเปนตวเลขจ านวนมากๆ ไดอยางรวดเรวและมประสทธภาพมากกวา list
เราจะน า Array นมาสรางเปน Vectors และ Matrices
204101 Introduction to Computer 4
Computer Science, CMU
Vector เปนล าดบของตวเลขทเขยนในรป
𝑢 =
𝑢1𝑢2…𝑢𝑛
ตวอยางเชน 1−2
,0.6−49
Matrix เปนแถวล าดบสเหลยมของตวเลขซงเขยนในรป
A =
𝑎11 ⋯ 𝑎1𝑛⋮ ⋱ ⋮
𝑎𝑚1 ⋯ 𝑎𝑚𝑛
ตวอยางเชน 1 23 4
Vector and Matrix
204101 Introduction to Computer 5
Computer Science, CMU
การสราง array
เราจะสราง Vector และ Matrix ไดโดยการใช Array
Array สามารถถกสรางไดจาก list
import numpy as np
a = np.array([1,2,4,8],float)
print(a)
type(a)
Array 1 มต เทยบไดกบ Vector นนเอง
[ 1. 2. 4. 8.]
<class 'numpy.ndarray'>
204101 Introduction to Computer 6
Computer Science, CMU
การสรางและเขาถงขอมลใน array
ในการสราง array จะรบคา 2 คาไดแกlist ทตองการเปลยนเปน array ชนดของสมาชกทตองการสรางเปน array
สมาชกของ array จะถกเขาถง แบงและจดการไดเชนเดยวกบ list เชนimport numpy as npa = np.array([1,2,4,8], float)print(a[3])a[0] = 5print(a)
8.0
[ 5. 2. 4. 8.]
204101 Introduction to Computer 7
Computer Science, CMU
การเขาถงขอมลใน array หลายมต
Array สามารถท าใหเปนหลายมตได ไมเหมอนกบ list การสามารถเขาถงขอมลในแกนทแตกตางกนท าไดโดยใช comma ภายในปกกา
Array 2 มต เทยบไดกบ Matrix นนเอง
import numpy as np
a = np.array([[1,2,3],[4,5,6]],float)
print(a)
print(a[0,0])
print(a[0,1])
[[ 1. 2. 3.][ 4. 5. 6.]]
1.02.0
204101 Introduction to Computer 8
Computer Science, CMU
การ Transpose
การ transpose array สามารถท าไดโดยใชค าสง transpose() ซงจะเปนการสราง array ใหม
import numpy as np
a = np.array([[1,2,3],[4,5,6]],float)
print(a)
b = a.transpose()
print(b)
[[ 1. 2. 3.][ 4. 5. 6.]]
[[ 1. 4.][ 2. 5.][ 3. 6.]]
204101 Introduction to Computer 9
Computer Science, CMU
การท า array ใหเหลอมตเดยว
Array หลายมตสามารถท าใหเหลอมตเดยวไดโดยใชฟงกชน flatten()
import numpy as np
a = np.array([[1,2,3],[4,5,6]],float)
print(a)
a = a.flatten()
print(a)
[[ 1. 2. 3.][ 4. 5. 6.]]
[ 1. 2. 3. 4. 5. 6.]
204101 Introduction to Computer 10
Computer Science, CMU
การสราง array แบบอน
การสราง array แบบอน
ใชฟงกชน arange ซงคลายกบฟงกชน range แตคนคาเปน array
import numpy as np
a = np.arange(5,dtype=float)
print(a)
b = np.arange(1,6,2, dtype=float)
print(b)
[ 0. 1. 2. 3. 4.]
[ 1. 3. 5.]
204101 Introduction to Computer 11
Computer Science, CMU
Array mathematics
การด าเนนการทางคณตศาสตรทท ากบ array โดยทวไปจะท ากบสมาชกทละตว นนหมายความวา array ควรจะมขนาดเทากนระหวางทมการบวก, ลบ หรอการด าเนนการอนๆimport numpy as npa = np.array([1,2,3],dtype=float)b = np.array([5,1,8],dtype=float)print(a+b)print(a-b)print(a*b)print(a/b)print(a%b)print(b**a)
[ 6. 3. 11.][-4. 1. -5.][ 5. 2. 24.][ 0.2 2. 0.375][ 1. 0. 3.][ 5. 1. 512.]
204101 Introduction to Computer 12
Computer Science, CMU
Array mathematics
ส าหรบ array 2 มต การคณจะเปนการคณตวตอตว ไมใชการคณเมทรกซส าหรบการคณเมทรกซจะมอธบายภายหลง
import numpy as np
a = np.array([[1,2],[3,4]],dtype=float)
b = np.array([[2,0],[1,3]],dtype=float)
print(a*b) [[ 2. 0.][ 3. 12.]]
204101 Introduction to Computer 13
Computer Science, CMU
Array mathematics
อยางไรกตามหาก array ทมตไมสอดคลองกนจะถก broadcast แทน นนหมายความวา array ขนาดเลกจะถกท าซ า ตวอยาง
import numpy as np
a = np.array([[1,2],[3,4],[5,6]],dtype=float)
b = np.array([-1,3],dtype=float)
print(a+b)
ซงในทน array b ทมขนาด 1 มตนนจะถก broadcast ไปยง array 2 มตทขนาดสอดคลองกบ a นนคอ b จะถก ท าซ าในแตละ item ของ a ราวกบวา ท างานกบ [[ -1. 3.], [-1. 3.], [-1. 3.]] อย
[[ 0. 5.][ 2. 7.][ 4. 9.]]
204101 Introduction to Computer 14
[[ 1. 2.][ 3. 4.][ 5. 6.]]
[ -1. 3.]
Computer Science, CMU
Array mathematics
นอกจากการด าเนนการพนฐานตางๆ NumPy ยงมฟงกชนทางคณตศาสตรใหใชงานอกมากมากซงจะท างานกบสมาชกใน array ทละตว เชน
abc, sign, sqrt, log, log10, exp, sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, tanh, arcsinh, arccosh, arctanh, floor, ceil, rint, pi, e (np.pi, np.e)
import numpy as np
a = np.array([1,4,9],dtype=float)
print(np.sqrt(a)) [ 1. 2. 3.]
204101 Introduction to Computer 15
Computer Science, CMU
Basic array operation
มหลายฟงกชนทท างานกบทง array
ขอมลใน array สามารถหาผลรวมหรอคณกนทงหมดได
import numpy as np
a = np.array([2,4,3], float)
print(a.sum())
print(a.prod())
c = np.array([[0,-2],[3,-1],[3,-5]], float)
print(c.sum())
หรอใช np.sum(a) np.prod(a) แทนไดเชนกน
9.024.0
-2
204101 Introduction to Computer 16
Computer Science, CMU
Basic array operation
มฟงกชนส าหรบค านวณคาทางสถตทท ากบชดขอมลทเกบใน array เชน mean, variance และ standard deviation โดยเรยกดวย mean(), var() และ std()ตามล าดบ
import numpy as np
a = np.array([2,1,9], float)
print(a.mean())
print(a.var())
print(a.std())
4.012.66666666673.55902608401
204101 Introduction to Computer 17
Computer Science, CMU
Basic array operation
max() ฟงกชนส าหรบการหาคามากทสด
min() ฟงกชนส าหรบการหาคานอยทสด
argmax() ฟงกชนส าหรบการหาต าแหนงของคาทมากทสด
argmin() ฟงกชนส าหรบการหาต าแหนงของคาทนอยทสด
import numpy as np
a = np.array([2,1,9], float)
print(a.max())
print(a.min())
print(a.argmax())
print(a.argmin())
9.01.021
204101 Introduction to Computer 18
Computer Science, CMU
Basic array operation
ส าหรบ array หลายมตแตละฟงกชนทอธบายกอนหนาสามารถระบแกนทตองการท าการด าเนนการได ตวอยางเชน
import numpy as np
a = np.array([[0,-2],[3,-1],[3,-5]], float)
print(a.mean(axis=0))
print(a.mean(axis=1))
print(a.min(axis=1))
print(a.max(axis=0))
[ 2. -2.66666667][-1. 1. -1.][-2. -1. -5.][ 3. -1.]
204101 Introduction to Computer 19
[[ 0. -2.]
[ 3. -1.]
[ 3. -5.]]
axis=0
axis=1
Computer Science, CMU
การเรยงล าดบขอมลใน array 1 มต
หาตองการให array 1 มตเรยงล าดบใชค าสง sorted()
import numpy as np
a = np.array([6,4,8,2,9], float)
b = sorted(a)
print(b) [2.0, 4.0, 6.0, 8.0, 9.0]
204101 Introduction to Computer 20
Computer Science, CMU
Vector and Matrix mathematics
NumPy มฟงกชนหลายฟงกชนใหใชงานเกยวกบการด าเนนการทางคณตศาสตรทเกยวกบเวกเตอรและเมทรกซ ไดแก
dot product (คณเมทรกซ)
inner, outer, cross product
determinant
eigenvalues และ eigenvectors
inverse ของเมทรกซ
204101 Introduction to Computer 21
Computer Science, CMU
dot product
ตวอยาง 𝑎 =1 2−1 03 2
, 𝑏 = 1 5 2−2 0 1
ตองการหา 𝑎𝑏 = ?
𝑎𝑏 =−3 5 4−1 −5 −2−1 15 8
204101 Introduction to Computer 22
Computer Science, CMU
ตวอยางการใชงาน dot product
import numpy as np
a = np.array([[1,2],[-1,0],[3,2]])
b = np.array([[1,5,2],[-2,0,1]])
c = np.dot(a,b)
print(c) [[-3 5 4][-1 -5 -2][-1 15 8]]
204101 Introduction to Computer 23
Computer Science, CMU
ตวอยาง inner, outer, cross product
import numpy as np
a = np.array([1,4,0],float)
b = np.array([2,2,1],float)
x = np.outer(a,b)
print(x)
y = np.inner(a,b)
print(y)
z = np.cross(a,b)
print(z)
[[ 2. 2. 1.][ 8. 8. 4.][ 0. 0. 0.]]
[ 4. -1. -6.]
10.0
204101 Introduction to Computer 24
Computer Science, CMU
Linear algebra function
นอกจากน NumPy ยง build-in ฟงกชนส าหรบแกปญหาเกยวกบ linear algebra ทเกยวกบเมทรกซ แตตองเรยกผาน sub module linalg ซงฟงกชนทสามารถเรยกใชไดแก
det() ส าหรบหา determinant
inv() ส าหรบหา inverse ของเมทรกซ
204101 Introduction to Computer 25
Computer Science, CMU
ตวอยางการใชงาน Linear algebra function
import numpy as np
a = np.array([[4,2,0],[9,3,7],[1,2,1]])
x = np.linalg.det(a)
print(x)
y = np.linalg.inv(a)
print(y)
z = np.dot(a,y) ค าถาม Z ควรมคาเปนเทาไร
204101 Introduction to Computer 26
-48.0
[[ 0.22916667 0.04166667 -0.29166667]
[ 0.04166667 -0.08333333 0.58333333]
[-0.3125 0.125 0.125 ]]