Numpy
Numpy
6
ARRAYS
Structured lists of numbers.
𝑝𝑥
Vectors
𝑝𝑦
Matrices
𝑝𝑧
Images
Tensors 𝑎11 ⋯ 𝑎1𝑛
ConvNets ⋮ ⋱ ⋮
𝑎𝑚1 ⋯ 𝑎𝑚𝑛
7
ARRAYS
Structured lists of numbers.
Vectors
Matrices
Images
Tensors
ConvNets
8
ARRAYS
Structured lists of numbers.
Vectors
Matrices
Images
Tensors
ConvNets
9
NUMPY ARRAYS
There are a couple of mechanisms for creating arrays in NumPy:
• Conversion from other Python structures (e.g., lists, tuples).
• Built-in NumPy array creation (e.g., arange, ones, zeros, etc.).
• Reading arrays from disk, either from standard or custom formats (e.g. reading in
from a CSV file).
• and others …
ARRAYS, CREATION
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
11
ARRAYS, CREATION
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
12
ARRAYS, CREATION
np.ones, np.zeros, np.diag
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
13
ARRAYS, CREATION
np.ones, np.zeros, np.diag
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
14
ARRAYS, CREATION
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
15
ARRAYS, CREATION
np.ones, np.zeros, np.diag
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
16
ARRAYS, CREATION
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
17
ARRAYS, CREATION
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
18
ARRAYS, DANGER ZONE
Must be dense, no holes.
Must be one type
Cannot combine arrays of different shape
19
INDEXING
Single-dimension indexing is accomplished as usual.
>>> x = np.arange(10)
>>> x[2]
2 0 1 2 3 4 5 6 7 8 9
>>> x[-2]
8
Multi-dimensional arrays support multi-dimensional indexing.
>>> x.shape = (2,5) # now x is 2-dimensional
>>> x[1,3]
0 1 2 3 4
8
5 6 7 8 9
>>> x[1,-1]
9
INDEXING
Using fewer dimensions to index will result in a subarray.
>>> x[0]
array([0, 1, 2, 3, 4])
This means that x[i, j] == x[i][j] but the second method is less efficient.
INDEXING
x[0,0] # top-left element 1 2 3
4 5 6
7 8 9
Notes:
Zero-indexing
Multi-dimensional indices are comma-separated 22
SLICING
>>> x = np.arange(10)
24
AXES
a.mean() # meam all entries
a.mean(axis=0) # rata rata tiap kolom
a.mean(axis=1) # rata rata tiap baris
25
BOOLEAN SLICING
arr = np.arange(10) # membuat array berisi angka 0-10
arr.mean() # rata-rata semua elemen
arr> arr.mean() # boolean array
arr[arr> arr.mean()] # ambil baris yang bernilai true (nilai elemen > rata rata)
There are also some built-in
methods of ndarray objects.
>>> I = np.identity(2)
>>> I
array([[ 1., 0.],
Since multiplication is done [ 0., 1.]])
element-wise, you need to >>> I[1,0] = 0
specifically perform a dot product
to perform matrix multiplication. >>> b = np.arange(4).reshape(2,2)
>>> b
array([[0, 1],
[2, 3]])
>>> a*b
array([[ 0., 1.],
[ 2., 3.]])
>>> np.dot(a,b)
array([[ 2., 4.],
[ 2., 4.]])
•For array, '*' means element-wise multiplication,
and the dot() function is used for matrix multiplication.
ARRAY OPERATIONS
>>> a = np.floor(10*np.random.random((3,4)))
>>> print a
[[ 9. 8. 7. 9.]
An array shape can be [ 7. 5. 9. 7.]
manipulated by a number of [ 8. 2. 7. 5.]]
methods. >>> a.shape
(3, 4)
>>> a.ravel()
resize(size) will modify an array([ 9., 8., 7., 9., 7., 5., 9., 7., 8., 2., 7., 5.])
array in place. >>> a.shape = (6,2)
>>> print a
[[ 9. 8.]
reshape(size) will return a
[ 7. 9.]
copy of the array with a new [ 7. 5.]
shape. [ 9. 7.]
[ 8. 2.]
[ 7. 5.]]
>>> a.transpose()
array([[ 9., 7., 7., 9., 8., 7.],
[ 8., 9., 5., 7., 2., 5.]])
Test Yourself
Seorang guru menyimpan nilai ujian 12 siswa yang terbagi dalam 4 kelompok. Setiap kelompok
terdiri dari 3 siswa
Kelompok 1: 65, 70, 75
Kelompok 2: 60, 55, 58
Kelompok 3: 80, 85, 82
Kelompok 4: 70, 68, 72