77
Scientific Computing with Python - NumPy 2017/08/03 (Thus.) WeiYuan

Scientific Computing with Python - NumPy | WeiYuan

Embed Size (px)

Citation preview

Page 1: Scientific Computing with Python - NumPy | WeiYuan

Scientific Computing with Python- NumPy

2017/08/03 (Thus.)WeiYuan

Page 2: Scientific Computing with Python - NumPy | WeiYuan

site: v123582.github.ioline: weiwei63

§ 全端⼯程師 + 資料科學家略懂⼀點網站前後端開發技術,學過資料探勘與機器學習的⽪⽑。平時熱愛參與技術社群聚會及貢獻開源程式的樂趣。

Page 3: Scientific Computing with Python - NumPy | WeiYuan

3

Page 4: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

4

Page 5: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

5

Page 6: Scientific Computing with Python - NumPy | WeiYuan

the Ecosystem of Python

6Reference:https://www.edureka.co/blog/why-you-should-choose-python-for-big-data

Page 7: Scientific Computing with Python - NumPy | WeiYuan

7Reference:https://www.slideshare.net/gabrielspmoreira/python-for-data-science-python-brasil-11-2015

Page 8: Scientific Computing with Python - NumPy | WeiYuan

About NumPy§NumPy is the fundamental package for scientific computing

with Python. It contains among other things:• a powerful N-dimensional array object• sophisticated (broadcasting) functions• tools for integrating C/C++ and Fortran code• useful linear algebra, Fourier transform, and random number

capabilities• be used as an efficient multi-dimensional container of generic data.

8

Page 9: Scientific Computing with Python - NumPy | WeiYuan

About NumPy§NumPy is the fundamental package for scientific computing

with Python. It contains among other things:• a powerful N-dimensional array object• sophisticated (broadcasting) functions• tools for integrating C/C++ and Fortran code• useful linear algebra, Fourier transform, and random number

capabilities• be used as an efficient multi-dimensional container of generic data.

9

Page 10: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Import the numpy package under the name np

10

Page 11: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Print the numpy version and the configuration

11

Page 12: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

12

Page 13: Scientific Computing with Python - NumPy | WeiYuan

Ndarray§ shape § ndim§ dtype§ size§ itemsize§ data

13

123456789101112

from numpy import * a = array([

[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]]

)

Page 14: Scientific Computing with Python - NumPy | WeiYuan

Ndarray§ shape § ndim§ dtype§ size§ itemsize§ data

14

123456789101112

from numpy import * a = array([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]

])

a.shape # (3, 5)a.ndim # 2a.dtype.name # 'int32’a.size # 15a.itemsize # 4 type(a) # numpy.ndarray

Page 15: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

15

Page 16: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§One Dimension Array (1dnarray)§Multiple Dimension Array (ndarray)§ Zeros, Ones, Empty§ arange and linspace§ random array§ array from list/tuple

16

Page 17: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§One Dimension Array (1dnarray)

17

123456789

import numpy as np

arr1 = np.array([0, 1, 2, 3, 4]) # array([0 1 2 3 4])type(arr1) # <type 'numpy.ndarray'>arr1.dtype # dtype('int64')

array( )

Page 18: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§One Dimension Array (1dnarray)

18

123456789

import numpy as np

arr1 = np.array([0, 1, 2, 3, 4]) # array([0 1 2 3 4])type(arr1) # <type 'numpy.ndarray'>arr1.dtype # dtype('int64')

arr2 = np.array([1.2, 2.4, 3.6]) # array([1.2, 2.4, 3.6])type(arr2) # <type 'numpy.ndarray'>arr2.dtype # dtype('float64')

array( )

Page 19: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§Question:How to assign data type for an array ?

19

Page 20: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§Multiple Dimension Array (ndarray)

20

123456789

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])# array([[1, 2, 3],# [4, 5, 6]])

array( )

Page 21: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§Question:How to change shape from 1-d array ?

21

Page 22: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§ Zeros, Ones, Empty

22

123456789

import numpy as np

zeros = np.zeros(5) # array([ 0., 0., 0., 0., 0.])

zeros( )

Page 23: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§ Zeros, Ones, Empty

23

123456789

import numpy as np

zeros = np.ones(5) # array([ 1., 1., 1., 1., 1.])

ones( )

Page 24: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§ Zeros, Ones, Empty

24

123456789

import numpy as np

zeros = np.empty(5) # array([ 0., 0., 0., 0., 0.])

empty( )

Page 25: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§ arange and linspace

25

123456789

import numpy as np

arange = np.arange(5) # array([0 1 2 3 4])

arange( )

Page 26: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§ arange and linspace

26

123456789

import numpy as np

linspace = np.linspace(0, 4, 5) # array([ 0., 1., 2., 3., 4.])

linspace( )

Page 27: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§ random array

27

123456789

import numpy as np

linspace = np.random.randint(0, 2, size=4) # array([ 0, 1, 1, 1])

random.randint( )

Page 28: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a 3x3x3 array with random values

28

Page 29: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Find indices of non-zero elements

29

Page 30: Scientific Computing with Python - NumPy | WeiYuan

Create a new Ndarray§ array from list/tuple

30

123456789

import numpy as np

x = [1,2,3]a = np.asarray(x)

x = (1,2,3)a = np.asarray(x)

asarray( )

Page 31: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a null vector of size 10

31

Page 32: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a vector with values ranging from 10 to 49

32

Page 33: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a 3x3 identity matrix

33

Page 34: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

34

Page 35: Scientific Computing with Python - NumPy | WeiYuan

Property of Ndarray§ shape § ndim§ dtype

§ size§ itemsize

§ data

35

123456789101112

import numpy as np

a = np.array([[11, 12, 13, 14, 15],[16, 17, 18, 19, 20],[21, 22, 23, 24, 25],[26, 27, 28 ,29, 30],[31, 32, 33, 34, 35]

])

Page 36: Scientific Computing with Python - NumPy | WeiYuan

Property of Ndarray§ shape § ndim§ dtype

§ size§ itemsize

§ data

36

123456789101112

print(type(a))

print(a.shape) print(a.ndim) print(a.dtype)

print(a.size) print(a.itemsize) print(a.nbytes)

Page 37: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:How to find the memory size of any array

37

Page 38: Scientific Computing with Python - NumPy | WeiYuan

data type

38

§Question:How to assign data type for an array ?

1. Set dtype with create the array2. Change dtype function

Page 39: Scientific Computing with Python - NumPy | WeiYuan

1. Set dtype with create the array

39

123456789

x = numpy.array([1,2.6,3], dtype = numpy.int64)print(x) #print(x.dtype) #

x = numpy.array([1,2,3], dtype = numpy.float64)print(x) #print(x.dtype) #

array( )

Page 40: Scientific Computing with Python - NumPy | WeiYuan

2. Change dtype function

40

123456789

x = numpy.array([1,2.6,3], dtype = numpy.float64)

y = x.astype(numpy.int32)print(y) # [1 2 3]print(y.dtype)

z = y.astype(numpy.float64)print(z) # [ 1. 2. 3.]print(z.dtype)

astype( )

Page 41: Scientific Computing with Python - NumPy | WeiYuan

41

Page 42: Scientific Computing with Python - NumPy | WeiYuan

data shape

42

§Question:How to change shape from 1-d array ?

1. Set multiple array with create the array2. Assign new shape to shape property3. Change shape function

Page 43: Scientific Computing with Python - NumPy | WeiYuan

1. Set multiple array with create the array

43

123456789

import numpy as np

a = np.array([[1,2,3],[4,5,6]]) a.shape # (2, 3)

array( )

Page 44: Scientific Computing with Python - NumPy | WeiYuan

2. Assign new shape to shape property

44

123456789

a = np.array([[1,2,3],[4,5,6]])a.shape = (3,2) a # [[1, 2], [3, 4], [5, 6]]

array( )

Page 45: Scientific Computing with Python - NumPy | WeiYuan

3. Change shape function

45

123456789

a = np.array([[1,2,3],[4,5,6]])b = a.reshape(3,2) b # [[1, 2], [3, 4], [5, 6]]

reshape( )

Page 46: Scientific Computing with Python - NumPy | WeiYuan

3. Change shape function

46

123456789

a = np.array([[1,2,3],[4,5,6]])b = a.reshape(3,2) b # [[1, 2], [3, 4], [5, 6]]

a.resize(3,2) a # [[1, 2], [3, 4], [5, 6]]

resize( )

Page 47: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a 3x3 matrix with values ranging from 0 to 8

47

Page 48: Scientific Computing with Python - NumPy | WeiYuan

index and slicing§ index

§ slicing

48

1234

array[0] # 0array[1] # 1array[-1] # 4

12345

array[1:3] # [1, 2]array[:4] # [0, 1, 3]array[3:] # [3, 4]array[1:4:2] # [1, 3]array[::-1] # [4, 3, 2, 1, 0]

([0,1,2,3,4])

Page 49: Scientific Computing with Python - NumPy | WeiYuan

index and slicing§ index

§ slicing

49

1234

array[1] # [0, 1]array[1][0] # 0 array[1][1] # 1array[2][0] # 2

12345

array[0:2] # [[0, 1], [2, 3]]array[:2] # [[0, 1], [2, 3]]array[2:] # [[4, 5]]

([0,1,0,1,0],[2,3,2,3,2],[4,5,4,5,4])

Page 50: Scientific Computing with Python - NumPy | WeiYuan

index and slicing§ slicing

50

1234

array[0, 1:4] # [1, 0, 1]array[[0, 0, 0], [1, 2, 3]]array[1:3, 0] # [1, 3, 5]array[[1, 2], [0, 0, 0]]

([0,1,0,1,0],[2,3,2,3,2],[4,5,4,5,4])

#Note: Numpy與 python的 index/slicing 差異?

Page 51: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a null vector of size 10 but the fifth value which

is 1

51

Page 52: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Reverse a vector (first element becomes last)

52

Page 53: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a 2d array with 1 on the border and 0 inside

53

Page 54: Scientific Computing with Python - NumPy | WeiYuan

Try it!§#練習:Create a 8x8 matrix and fill it with a checkerboard

pattern

54

Page 55: Scientific Computing with Python - NumPy | WeiYuan

Try it!

§#練習:

55

123456789

import numpy as npa = np.array([[1,2,3],[3,4,5],[4,5,6]])

# 第二列元素

# 第二行元素

# 除了第二列的元素

Page 56: Scientific Computing with Python - NumPy | WeiYuan

Try it!

§#練習:

56

1234

array[::2,::2] array[:, 1]

Page 57: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

57

Page 58: Scientific Computing with Python - NumPy | WeiYuan

Basic Operators

58

123456789101112131415

import numpy as np

a = np.arange(25).reshape((5, 5))b = np.linspace(0, 25, 25).reshape((5,5))

print(a + b)print(a - b)print(a * b)print(a / b)print(a ** 2)print(a < b)print(a > b)

print(a.dot(b))

Page 59: Scientific Computing with Python - NumPy | WeiYuan

sophisticated (broadcasting)

59

123456789101112

import numpy as np

a = np.arange(25).reshape((5, 5))b = np.array([1.0, 2.0, 3.0, 4.0, 5.0])

print(a + b)

Page 60: Scientific Computing with Python - NumPy | WeiYuan

sophisticated (broadcasting)

60

123456789101112

import numpy as np

a = np.arange(25).reshape((5, 5))b = np.array([1.0, 2.0, 3.0, 4.0, 5.0])

print(a + b)

c = np.arange(25).reshape((5, 5))d = np.arange(5).reshape((1, 5))

print(c + d)

Page 61: Scientific Computing with Python - NumPy | WeiYuan

ufunc

61

123456789101112

import numpy as np

a = np.arange(5)

exp(a)

sqrt(a)

add(a + a)

Page 62: Scientific Computing with Python - NumPy | WeiYuan

staticstic

62

123456789101112

import numpy as np

a = np.arange(10)

print(a.sum()) print(a.min()) print(a.max()) print(a.cumsum())

Page 63: Scientific Computing with Python - NumPy | WeiYuan

staticstic

63

123456789101112

import numpy as np

a = np.arange(10)

print(np.sum(a)) # 45print(np.median(a)) # 45print(np.mean(a)) # 45print(np.average(a)) # 45print(np.std(a))print(np.var(a))

Page 64: Scientific Computing with Python - NumPy | WeiYuan

staticstic

64

123456789101112

import numpy as np

a = np.arange(10)

print(np.sum(a, axis=0))print(np.sum(a, axis=1))

Page 65: Scientific Computing with Python - NumPy | WeiYuan

staticstic

65

123456789101112

import numpy as np

a = np.arange(10)

np.sort(a)

Page 66: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

66

Page 67: Scientific Computing with Python - NumPy | WeiYuan

2d-array

67

123456789101112

# ndarray数组的转置和轴对换k = numpy.arange(9) # [0,1,....8]m = k.reshape((3,3)) # 改变数组的shape复制生成2维的,每个维度长度为3的数组print(k) # [0 1 2 3 4 5 6 7 8]print(m) # [[0 1 2] [3 4 5] [6 7 8]]

print m.T # [[0 3 6] [1 4 7] [2 5 8]]print numpy.dot(m,m.T) # numpy.dot点乘

Page 68: Scientific Computing with Python - NumPy | WeiYuan

Matrix

68

123456789101112

A = matrix('1.0 2.0; 3.0 4.0')

AA.T

X = matrix('5.0 7.0')Y = X.T

A*YA.Isolve(A, Y)

Page 69: Scientific Computing with Python - NumPy | WeiYuan

2d-array vs Matrix

69

123456789101112

A = np.arange(12).reshape((3,4))M = np.mat(A.copy())

print type(A), type(M)print A[:], A[:].shapeprint M[:], M[:].shape

print A[:,1]; print A[:,1].shapeprint M[:,1]; print M[:,1].shape

Page 70: Scientific Computing with Python - NumPy | WeiYuan

Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages

70

Page 71: Scientific Computing with Python - NumPy | WeiYuan

Advanced Usages§ Fancy indexing §Boolean masking §Where function§Customize dtype

71

Page 72: Scientific Computing with Python - NumPy | WeiYuan

Fancy indexing

72

123456789101112

import numpy as np

a = np.arange(0, 100, 10) # [ 0 10 20 30 40 50 60 70 80 90]indices = [1, 5, -1] # [ 1, 5, -1]b = a[indices] # [10, 50, 90]

Page 73: Scientific Computing with Python - NumPy | WeiYuan

Boolean masking

73

123456789101112

import numpy as np

a = np.arange(0, 100, 10) # [0 10 20 30 40 50 60 70 80 90]a > 30 # [F F F F T T T T T T]b = a[a > 30] # [40 50 60 70 80 90]

Page 74: Scientific Computing with Python - NumPy | WeiYuan

Boolean masking

74

123456789101112

import numpy as np

a = np.linspace(0, 2 * np.pi, 50)b = np.sin(a)plt.plot(a,b)mask = b >= 0plt.plot(a[mask], b[mask], 'bo')mask = (b >= 0) & (a <= np.pi / 2)plt.plot(a[mask], b[mask], 'go')plt.show()

Page 75: Scientific Computing with Python - NumPy | WeiYuan

where()

75

123456789101112

a = np.arange(0, 100, 10)b = np.where(a < 50) c = np.where(a >= 50)[0]

print(b)print(c)

Page 76: Scientific Computing with Python - NumPy | WeiYuan

Customize dtype

76

123456789101112

import numpy as np

student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')

])

a = np.array([('abc', 21, 50),('xyz', 18, 75)

], dtype = student)

Page 77: Scientific Computing with Python - NumPy | WeiYuan

Thanks for listening.2017/08/03 (Thus.) Scientific Computing with Python – NumPyWei-Yuan [email protected]