Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

AI VIETNAM

Free AI Course 2020

Numpy
Introduction and applications to data
processing (draft version)
Quang-Vinh Dinh
Ph.D. in Computer Science

Year 2020
AI VIETNAM
Free AI Course 2020

Outline
 Introduction to Numpy
 Numpy Array Indexing
 Numpy Array Operations
 Broadcasting
 Data Processing
AI VIETNAM
Free AI Course
Introduction
 Numpy is a Python library

Year 2020 1
AI VIETNAM
Free AI Course
Introduction
 Numpy is a Python library
 For scientific computations

Year 2020 2
AI VIETNAM
Free AI Course
Introduction
 Numpy is a Python library
 For scientific computations
 Numpy array Tensor in
Tensorflow and Pytorch

Year 2020 3
AI VIETNAM
Free AI Course
Introduction
 Numpy is a Python library
 For scientific computations
 Numpy array Tensor in Tensorflow and Pytorch
 Numpy arrays are multi-dimensional arrays

Axis 1

1 2 1 2 3
1 2 3 Axis 0

Axis 0
3 4 Axis 0 4 5 6
5 6 7 8 9 Axis 2

Axis 1

1D array 2D array 3D array

Year 2020 4
AI VIETNAM
Free AI Course
Introduction
 Create Numpy array
 From List

arr_np = np.array(python_list)

data
 
data
1
0 1
1 2
data
 
2 3
2

Year 2020 5
AI VIETNAM
Free AI Course
Introduction
 Common attributes
 dtype: data type
 shape: return a tuple of #elements in each dimension
shape=(3,3,2)
 ndim: return #dimensions Axis 1 1 2 3
Axis 0 ndim=2
1 2 4 5 6
1 2 3 shape=(3,) Axis 0
shape=(3,2)
ndim=1 3 4 ndim=2 7 8 9 Axis 2
Axis 0 dtype example
5 6 Axis 1

Year 2020 6
AI VIETNAM
Free AI Course
Introduction
 Update an element

index 0 1 2
data = 1 2 3

data[0] = 8

data = 8 2 3

Year 2020 7
AI VIETNAM
Free AI Course
Introduction
Create Numpy array

zeros() function ones() function full() function

Year 2020 8
AI VIETNAM
Free AI Course
Introduction
Create Numpy array
arange() function eye() function random() function
0 1 2 3 4

arr1 = 0 1 2 3 4
0 1 2

arr2 = 0 2 4

Year 2020 9
AI VIETNAM
Free AI Course
Introduction
Some important functions
flatten() function
where() function
1 2
arr =
arr = 0 1 2 3 4 3 4
arr<3 = T T T F F flatten()

out = 0 1 2 6 8 out = 1 2 3 4

Year 2020 10
AI VIETNAM
Free AI Course
Introduction
Some important functions

reshape() function

data data_rs
1 2 3 1 2
4 5 6 3 4
5 6

Year 2020 114


AI VIETNAM
Free AI Course 2020

Outline
 Introduction to Numpy
 Numpy Array Indexing
 Numpy Array Operations
 Broadcasting
 Data Processing
AI VIETNAM
Free AI Course
Array Indexing
 Slicing
arr[for_axis_0, for_axis_1, …]
  ‘:’: get all the elements
‘a:b’: get the elements from to

data   data   data   data   data


0 1 0 1 0 1 0 1 0 1
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
1 3 4 1 3 4 1 3 4 1 3 4 1 3 4
2 5 6 2 5 6 2 5 6 2 5 6 2 5 6

Year 2020 124


AI VIETNAM
Free AI Course
Array Indexing
 Slicing

Year 2020 134


AI VIETNAM
Free AI Course
Array Indexing
Slicing
 Mutable

0 1 2
1
0
1 2 3 a_arr [0,1]
a_arr = 0 2
1
5 6 7
b_arr = a_arr[:, 1:3]
0 1 0 1

0 2 3 b_arr [0,1]= 99 0 99 3
b_arr =
1 6 7 1 6 7

result

0 1 2
0
1 99 3
a_arr =
1
5 6 7

Year 2020 144


AI VIETNAM
Free AI Course
Array Indexing
Get a row

0 1 2

0 1 2 3
arr = 1 5 6 7
2 9 10 11
0 1 2

row_m1 = 5 6 7 shape(3, )
0 1 2

row_m2 = 0 5 6 7 shape(1, 3)

Year 2020 154


AI VIETNAM
Free AI Course
Array Indexing
 Get a column

0 1 2
0
1 2 3
arr = 1
5 6 7
2 9 10 11
0 1 2

col_m1 = 2 6 10 (3, )

0 2
col_m2 = 1 6 (3, 1)
2 10

Year 2020 164


AI VIETNAM
Free AI Course
Array Indexing
 Using Lists as indices

0 1

0 1 2
arr = 1 3 4
2 5 6

arr[[0, 1, 2], [0, 1, 0]] = 1 4 5

arr[[0, 0], [1, 1]] = 2 2

Year 2020 174


AI VIETNAM
Free AI Course
Array Indexing
 Boolean indices

0 1 0 1

0 1 2 0 F F
arr = 1 3 4 arr > 2 = 1 T T
2 5 6 2 T T

0 1 2 3

arr[arr>2] = 3 4 5 6

Year 2020 184


AI VIETNAM
Free AI Course 2020

Outline
 Introduction to Numpy
 Numpy Array Indexing
 Numpy Array Operations
 Broadcasting
 Data Processing
AI VIETNAM
Free AI Course
Numpy Array Operations
 Addition

data x data y result

1 5 6

2 6 8
+ =
3 7 10

4 8 12

Year 2020 194


AI VIETNAM
Free AI Course
Numpy Array Operations
 Subtraction

data x data y result

1 5 4

2 6 4
- =
3 7 4

4 8 4

Year 2020 204


AI VIETNAM
Free AI Course
Numpy Array Operations
 Multiplication

data x data y result

1 5 5

2 6 12
* =
3 7 21

4 8 32

Year 2020 214


AI VIETNAM
Free AI Course
Numpy Array Operations
 Division

data x data y result

1 5 0.2

2 6 0.33
/ =
3 7 0.42

4 8 0.5

Year 2020 224


AI VIETNAM
Free AI Course
Numpy Array Operations
 Square root

data result

1 1.0

2 1.4
sqrt(data) =
3 1.7

4 2.0

Year 2020 234


AI VIETNAM
Free AI Course
Numpy Array Operations
 Inner product

v w
result
1 2
= 8
2 3

Year 2020 244


AI VIETNAM
Free AI Course
Numpy Array Operations
 Vector-matrix multiplication

X v result

1 2 1 5
=
3 4 2 11

v X result

1 2 1 2 7
=
3 4 10

Year 2020 254


AI VIETNAM
Free AI Course
Numpy Array Operations
 Matrix-matrix multiplication

X Y result

1 2 2 3 6 5
=
3 4 2 1 14 13

Y X result

2 3 1 2 11 16
=
2 1 3 4 5 8

Year 2020 264


AI VIETNAM
Free AI Course
Numpy Array Operations
 Transpose

T
1 2 1 3

3 4 2 4

Year 2020 274


AI VIETNAM
Free AI Course
Numpy Array Operations
 Summation

data
sum(data)
1 2
Axis 0 10
3 4

Axis 1

sum(data, axis=0) sum(data, axis=1)

4 6 3 7

Year 2020 284


AI VIETNAM
Free AI Course
Numpy Array Operations
 Max and min
data data
1 1

2 .max( ) = 3 2 .min( ) = 1

3 3

1 2 1 2
3 4 .max(axis=0) = 5 6 3 4 .min(axis=0) = 1 2
5 6 5 6

1 2 1 2
3 4 .max(axis=1) = 2 4 6 3 4 .min(axis=1) = 1 3 5
5 6 5 6
Year 2020 294
AI VIETNAM
Free AI Course 2020

Outline
 Introduction to Numpy
 Numpy Array Indexing
 Numpy Array Operations
 Broadcasting
 Data Processing
AI VIETNAM
Free AI Course
Broadcasting
 Vector and a scalar

data result
0 1 2 0 1 2

1 2 3
* 2 = 2 4 6

data result
0 1 2 0 1 2

1 2 3 - 2 = -1 0 1

Year 2020 304


AI VIETNAM
Free AI Course
Broadcasting
 Matrix and vector
X
0 1 2
0
1 2 3
v
1
4 5 6
2 7 8 9
+ 1 0 1

3 10 11 12

Y
0 1 2 0 1 2
0
1 2 3 1 0 1 0
2 2 4
1
4 5 6 1 0 1 1
5 5 7
2 7 8 9
+ 1 0 1
= 2 8 8 10
3 10 11 12 1 0 1 3 11 11 13
Year 2020 314
AI VIETNAM
Free AI Course 2020

Outline
 Introduction to Numpy
 Numpy Array Indexing
 Numpy Array Operations
 Broadcasting
 Data Processing
AI VIETNAM
Free AI Course
Data Processing
Text data
 IRIS data

Year 2020 324


AI VIETNAM
Free AI Course
Data Processing
Text data
 IRIS data

col 0 col 1 col 2 col 3 col 4

Demo

Year 2020 334


AI VIETNAM
Free AI Course
Image Classification: Image Data
 Grayscale images

(Height, Width)
Pixel p = scalar Resolution: #pixels
Resolution = HeightxWidth
0  ≤ p ≤255
Year 2020 274
AI VIETNAM
Free AI Course
Image Classification: Image Data
 Color images

(Height, Width, channel)


  Pixel
RGB color image Resolution: #pixels
Resolution = HeightxWidth

Year 2020 0  ≤ r ,g , b ≤255 284


Image Data T-shirt

Trouser

Pullover
Fashion-MNIST dataset
Dress

Grayscale images Coat

Resolution=28x28 Sandal

Training set: 60000 samples Shirt

Testing set: 10000 samples


Sneaker

Bag

Ankle
Year 2020 Boot
AI VIETNAM
Free AI Course
Image Classification
 Fashion-MNIST data

Download data

Year 2020 304


AI VIETNAM
Free AI Course
Image Classification
Read data
Fashion-MNIST data

28

28

784 31
AI VIETNAM
Free AI Course
Data Processing
 Images in binary

Demo

Year 2020 344


AI VIETNAM
Free AI Course
Data Processing
 Images in files

Demo

Year 2020 354


AI VIETNAM
Free AI Course 2020

Summary
Indexing
Broadcasting data   data   data   data   data
0 1 0 1 0 1 0 1 0 1
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
1 3 4 1 3 4 1 3 4 1 3 4 1 3 4
2 5 6 2 5 6 2 5 6 2 5 6 2 5 6

Multi-dimension Array

1 2 3
Axis 0 4 5 6
7 8 9 Axis 2

Axis 1
36

You might also like