0% found this document useful (0 votes)
3 views

More on Numpy

The document outlines a workshop on IT, Hardware, and AI, focusing on the use of NumPy in data science and artificial intelligence. It details course objectives, outcomes, and various features of NumPy, including array creation, mathematical operations, and data visualization techniques. The workshop aims to equip students with the skills to understand and apply AI and data science concepts effectively.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

More on Numpy

The document outlines a workshop on IT, Hardware, and AI, focusing on the use of NumPy in data science and artificial intelligence. It details course objectives, outcomes, and various features of NumPy, including array creation, mathematical operations, and data visualization techniques. The workshop aims to equip students with the skills to understand and apply AI and data science concepts effectively.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science &
Engineering)
SUBJECT NAME:- IT, HW AND AI
WORKSHOP
SUBJECT CODE- 24ECP-102
Prepared By: Dr. Shonak Bansal
More on Numpy DISCOVER . LEARN . EMPOWER
1
Course Objectives
S. No. Objectives

1 To develop an understanding of the building blocks of AI.

2 To aware about Data Science/Analytics.

To provide knowledge about data processing.


3

4 To make familiar with AIML Algorithms.

5 To give brief knowledge about IT, HW AND AI.

2
Course Outcomes
CO Title Level
Number

CO1 Recognise the characteristics of disruptive technologies and Remember


understand building blocks of data science, artificial
intelligence, and machine learning.
CO2 Describe AI/ML algorithms and techniques to demonstrate Understand
its applications.
CO3 Experiment with effective data visualizations, and explain Apply
how to work with data through the entire data science
process.
CO4 Analyse and evaluate solutions to address real time problems Analyze and
using AI/ML for different applications. evaluate
CO5 Design, formulate and integrate in a team that can propose, a Create
solution for their selected domain.

3
THEORY

INTRODUCTION - Numpy
 Numpy, Scipy and Matplotlib provide MATLAB-like functionality in
python.
 Numpy Features:
 Typed multi-dimentional arrays (matrices)
 Fast numerical computations (matrix math)
 High-level math functions

4
1. Need of Numpy
 Python does numerical computations slowly.
 1000 x 1000 matrix multiply
 Python triple loop takes > 10 min.
 Numpy takes ~0.03 seconds

5
2. NumPy Overview
1. Arrays
2. Shaping and transposition
3. Mathematical Operations
4. Indexing and slicing
5. Broadcasting

6
3. Arrays
Structured lists of numbers:-
 Vectors
 Matrices
 Images
 Tensors
 ConvNets

7
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets

8
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets

Figure 1. Sample Image[1]

9
Arrays

Structured lists of numbers.


o Vectors
o Matrices
o Images
o Tensors
o ConvNets

Figure 2. Sample Image[1]


10
Arrays

Structured lists of numbers.


 Vectors
 Matrices
 Images
 Tensors
 ConvNets

Figure 3. Sample Image[1] 11


A. Arrays, Basic Properties
import numpy as np
a = np.array([[1,2,3],[4,5,6]],dtype=np.float32)
print a.ndim, a.shape, a.dtype

1. Arrays can have any number of dimensions, including zero (a scalar).


2. Arrays are typed: np.uint8, np.int64, np.float32, np.float64
3. Arrays are dense. Each element of the array exists and has the same
type.

12
B. Arrays Creation
 np.ones, np.zeros
 np.arange
 np.concatenate
 np.astype
 np.zeros_like, np.ones_like
 np.random.random

13
Arrays Creation

• np.ones, np.zeros
• 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.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 Creation

• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random

19
Arrays Creation

• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random

20
C. Arrays, danger zone

• Must be dense, no holes.


• Must be one type.
• Cannot combine arrays of different shape.

21
D. Shaping

a = np.array([1,2,3,4,5,6])
a = a.reshape(3,2)
a = a.reshape(2,-1)
a = a.ravel()
1. Total number of elements cannot change.
2. Use -1 to infer axis shape
3. Row-major by default (MATLAB is column-major)

22
E. Return values

• Numpy functions return either views or copies.


• Views share data with the original array, like references in Java/C++.
Altering entries of a view, changes the same entries in the original.
• The numpy documentation says which functions return views or
copies
• Np.copy, np.view make explicit copies and views.

23
F. Transposition

a = np.arange(10).reshape(5,2)
a = a.T
a = a.transpose((1,0))

np.transpose permutes axes.


a.T transposes the first two axes.

24
G. Saving and loading arrays

np.savez(‘data.npz’, a=a)
data = np.load(‘data.npz’)
a = data[‘a’]

1. NPZ files can hold multiple arrays


2. np.savez_compressed similar.

25
H. Image arrays
Images are 3D arrays: width, height, and channels
Common image formats:
height x width x RGB (band-interleaved)
height x width (band-sequential)

Gotchas:
Channels may also be BGR (OpenCV does this)
May be [width x height], not [height x width]

Figure 4. Sample Image[2]

26
I. Saving and Loading Images

SciPy: skimage.io.imread,skimage.io.imsave
height x width x RGB
PIL / Pillow: PIL.Image.open, Image.save
width x height x RGB
OpenCV: cv2.imread, cv2.imwrite
height x width x BGR

27
RECAP

We just saw how to create arrays, reshape them, and permute axes

Questions so far?

28
RECAP

We just saw how to create arrays, reshape them, and permute axes

Questions so far?

Now: let’s do some math

29
3. Mathematical operators

• Arithmetic operations are element-wise


• Logical operator return a bool array
• In place operations modify the array

30
Mathematical operators

• Arithmetic operations are element-wise


• Logical operator return a bool array
• In place operations modify the array

31
Mathematical operators

• Arithmetic operations are element-wise


• Logical operator return a bool array
• In place operations modify the array

32
Mathematical operators

• Arithmetic operations are element-wise


• Logical operator return a bool array
• In place operations modify the array

33
A. Math, upcasting
Just as in Python and Java, the result of a math operator is cast to the
more general or precise datatype.
uint64 + uint16 => uint64
float32 / int32 => float32

Warning: upcasting does not prevent overflow/underflow. You must


manually cast first.
Use case: images often stored as uint8. You should convert to float32 or
float64 before doing math.

34
B. Math, universal functions
Also called ufuncs, Element-wise
Examples:
• np.exp
• np.sqrt
• np.sin
• np.cos
• np.isnan

35
Math, universal functions

Also called ufuncs, Element-wise


Examples:
• np.exp
• np.sqrt
• np.sin
• np.cos
• np.isnan

Figure 5. Sample Image[2]

36
Math, universal functions

Also called ufuncs, Element-wise


Examples:
• np.exp
• np.sqrt
• np.sin
• np.cos
• np.isnan

37
C. Indexing

x[0,0] # top-left element


x[0,-1] # first row, last column
x[0,:] # first row (many entries)
x[:,0] # first column (many entries)
Notes:
• Zero-indexing
• Multi-dimensional indices are comma-separated (i.e., a tuple)

38
D. Indexing, slices and arrays

I[1:-1,1:-1] # select all but one-pixel


border
I = I[:,:,::-1] # swap channel order
I[I<10] = 0 # set dark pixels to black
I[[1,3], :] # select 2nd and 4th row

1. Slices are views. Writing to a slice overwrites the original array.


2. Can also index by a list or boolean array.

39
E. Python Slicing

Syntax: start:stop:step
a = list(range(10))
a[:3] # indices 0, 1, 2
a[-3:] # indices 7, 8, 9
a[3:8:2] # indices 3, 5, 7
a[4:1:-1] # indices 4, 3, 2 (this one is tricky)

40
F. Axes

a.sum() # sum all entries


a.sum(axis=0) # sum over rows
a.sum(axis=1) # sum over columns
a.sum(axis=1, keepdims=True)
1. Use the axis parameter to control which axis NumPy operates on
2. Typically, the axis specified will disappear, keepdims keeps all
dimensions

41
G. Broadcasting
a = a + 1 # add one to every element

When operating on multiple arrays, broadcasting rules are used.


Each dimension must match, from right-to-left
1. Dimensions of size 1 will broadcast (as if the value was repeated).
2. Otherwise, the dimension must have the same shape.
3. Extra dimensions of size 1 are added to the left as needed.

42
Broadcasting example

Suppose we want to add a color value to an image


a.shape is 100, 200, 3
b.shape is 3
a + b will pad b with two extra dimensions so it has an effective shape
of 1 x 1 x 3.
So, the addition will broadcast over the first and second dimensions.

43
H. Broadcasting failures

If a.shape is 100, 200, 3 but b.shape is 4 then a + b will fail. The trailing
dimensions must have the same shape (or be 1)

44
Tips to avoid bugs

1. Know what your datatypes are.


2. Check whether you have a view or a copy.
3. Use matplotlib for sanity checks.
4. Use pdb to check each step of your computation.
5. Know np.dot vs np.mult.

45
Learning Outcomes
On completion of the course students will be able to understand

• Grasp the characteristics of disruptive technologies and understand building


blocks of artificial intelligence, data science and cloud computing.
• Develop simple intelligent system using available tools and techniques of AI to
analyze and interpret domain knowledge.
• Build effective data visualizations and learn to work with data through the
entire data science process.
• Deploy, build, and monitor cloud-based applications.
• Work in a team that can propose, design, implement and report on their
selected domain.
46
Viva Voice Questions
• Why is NumPy preferred to other programming tools such as Idl,
Matlab, Octave, Or Yorick.
• What are the various features of NumPy?
• How can you Install NumPy on Windows?
• How can you identify the datatype of a given NumPy array?

47
Summary
• Nearly every scientist working in Python draws on the power of
NumPy.
• NumPy brings the computational power of languages like C and
Fortran to Python, a language much easier to learn and use. With this
power comes simplicity: a solution in NumPy is often clear and
elegant.

48
References

1.https://towardsdatascience.com/detecting-cute-animals-with-machin
e-learning-d39a511bd144?gi=e6a385d621ba
2.https://machinelearningmastery.com/how-to-develop-a-face-recognit
ion-system-using-facenet-in-keras-and-an-svm-classifier/

49
THANK YOU

For queries
Email: [email protected]

50

You might also like