Scientific Computing with Python - NumPy 2017/08/03 (Thus.) WeiYuan
Scientific Computing with Python- NumPy
2017/08/03 (Thus.)WeiYuan
site: v123582.github.ioline: weiwei63
§ 全端⼯程師 + 資料科學家略懂⼀點網站前後端開發技術,學過資料探勘與機器學習的⽪⽑。平時熱愛參與技術社群聚會及貢獻開源程式的樂趣。
3
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
4
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
5
the Ecosystem of Python
6Reference:https://www.edureka.co/blog/why-you-should-choose-python-for-big-data
7Reference:https://www.slideshare.net/gabrielspmoreira/python-for-data-science-python-brasil-11-2015
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
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
Try it!§#練習:Import the numpy package under the name np
10
Try it!§#練習:Print the numpy version and the configuration
11
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
12
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]]
)
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
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
15
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
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( )
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( )
Create a new Ndarray§Question:How to assign data type for an array ?
19
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( )
Create a new Ndarray§Question:How to change shape from 1-d array ?
21
Create a new Ndarray§ Zeros, Ones, Empty
22
123456789
import numpy as np
zeros = np.zeros(5) # array([ 0., 0., 0., 0., 0.])
zeros( )
Create a new Ndarray§ Zeros, Ones, Empty
23
123456789
import numpy as np
zeros = np.ones(5) # array([ 1., 1., 1., 1., 1.])
ones( )
Create a new Ndarray§ Zeros, Ones, Empty
24
123456789
import numpy as np
zeros = np.empty(5) # array([ 0., 0., 0., 0., 0.])
empty( )
Create a new Ndarray§ arange and linspace
25
123456789
import numpy as np
arange = np.arange(5) # array([0 1 2 3 4])
arange( )
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( )
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( )
Try it!§#練習:Create a 3x3x3 array with random values
28
Try it!§#練習:Find indices of non-zero elements
29
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( )
Try it!§#練習:Create a null vector of size 10
31
Try it!§#練習:Create a vector with values ranging from 10 to 49
32
Try it!§#練習:Create a 3x3 identity matrix
33
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
34
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]
])
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)
Try it!§#練習:How to find the memory size of any array
37
data type
38
§Question:How to assign data type for an array ?
1. Set dtype with create the array2. Change dtype function
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( )
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( )
41
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
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( )
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( )
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( )
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( )
Try it!§#練習:Create a 3x3 matrix with values ranging from 0 to 8
47
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])
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])
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 差異?
Try it!§#練習:Create a null vector of size 10 but the fifth value which
is 1
51
Try it!§#練習:Reverse a vector (first element becomes last)
52
Try it!§#練習:Create a 2d array with 1 on the border and 0 inside
53
Try it!§#練習:Create a 8x8 matrix and fill it with a checkerboard
pattern
54
Try it!
§#練習:
55
123456789
import numpy as npa = np.array([[1,2,3],[3,4,5],[4,5,6]])
# 第二列元素
# 第二行元素
# 除了第二列的元素
Try it!
§#練習:
56
1234
array[::2,::2] array[:, 1]
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
57
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))
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)
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)
ufunc
61
123456789101112
import numpy as np
a = np.arange(5)
exp(a)
sqrt(a)
add(a + a)
staticstic
62
123456789101112
import numpy as np
a = np.arange(10)
print(a.sum()) print(a.min()) print(a.max()) print(a.cumsum())
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))
staticstic
64
123456789101112
import numpy as np
a = np.arange(10)
print(np.sum(a, axis=0))print(np.sum(a, axis=1))
staticstic
65
123456789101112
import numpy as np
a = np.arange(10)
np.sort(a)
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
66
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点乘
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)
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
Outline§About NumPy§Ndarray§Create a new Array§ Property of Array§Operation of Array§Matrix§Advanced Usages
70
Advanced Usages§ Fancy indexing §Boolean masking §Where function§Customize dtype
71
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]
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]
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()
where()
75
123456789101112
a = np.arange(0, 100, 10)b = np.where(a < 50) c = np.where(a >= 50)[0]
print(b)print(c)
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)
Thanks for listening.2017/08/03 (Thus.) Scientific Computing with Python – NumPyWei-Yuan [email protected]