numpy and matplotlib - chiang mai university...เราจะสร้างvector และ matrix...

26
Computer Science, CMU NumPy and Matplotlib http://www.engr.ucsb.edu/~shell/che210d/numpy.pdf 204101 Introduction to Computer 1

Upload: others

Post on 24-Jan-2021

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

Computer Science, CMU

NumPy and Matplotlib

http://www.engr.ucsb.edu/~shell/che210d/numpy.pdf204101 Introduction to Computer 1

Page 2: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

Computer Science, CMU

Overview

NumPy (Numeric Python) เปนโมดลสวนเสรมของ Python ทมฟงกชนเกยวกบคณตศาสตรและการค านวณตางๆ มาใหใชงาน โดยทวไปจะเกยวกบการจดการขอมลชด (Array) ขนาดใหญและเมทรกซ

NumPy นครบคลมการค านวณมากมายสามารถท างานไดใกลเคยงกบcommercial software เชน MatLab เลยทเดยว

เนองจาก NumPy มความสามารถมากในรายวชานเราจะเรยนเกยวกบVector and matrix mathematics

204101 Introduction to Computer 2

Page 3: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 4: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

Computer Science, CMU

Arrays

Arrays เปนคณสมบตหลกของ NumPy มลกษณะคลายกบ list ยกเวนสมาชกทกตวใน array จะตองเปนขอมลชนดเดยวกน โดยทวไปแลวขอมลทเกบจะเปนตวเลขเชน int หรอ float

Arrays มความสามารถในการด าเนนการเกยวกบขอมลทเปนตวเลขจ านวนมากๆ ไดอยางรวดเรวและมประสทธภาพมากกวา list

เราจะน า Array นมาสรางเปน Vectors และ Matrices

204101 Introduction to Computer 4

Page 5: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 6: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 7: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 8: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 9: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 10: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 11: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 12: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 13: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 14: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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.]

Page 15: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 16: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 17: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 18: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 19: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 20: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 21: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

Computer Science, CMU

Vector and Matrix mathematics

NumPy มฟงกชนหลายฟงกชนใหใชงานเกยวกบการด าเนนการทางคณตศาสตรทเกยวกบเวกเตอรและเมทรกซ ไดแก

dot product (คณเมทรกซ)

inner, outer, cross product

determinant

eigenvalues และ eigenvectors

inverse ของเมทรกซ

204101 Introduction to Computer 21

Page 22: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 23: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 24: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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

Page 25: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

Computer Science, CMU

Linear algebra function

นอกจากน NumPy ยง build-in ฟงกชนส าหรบแกปญหาเกยวกบ linear algebra ทเกยวกบเมทรกซ แตตองเรยกผาน sub module linalg ซงฟงกชนทสามารถเรยกใชไดแก

det() ส าหรบหา determinant

inv() ส าหรบหา inverse ของเมทรกซ

204101 Introduction to Computer 25

Page 26: NumPy and Matplotlib - Chiang Mai University...เราจะสร้างVector และ Matrix ได้โดยการใช้ Array Array สามารถถูกสร้างได้จาก

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 ]]