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

Week 13 Numpy

Uploaded by

a.ahgv1453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 13 Numpy

Uploaded by

a.ahgv1453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Introduction to Computer Programing

Dr. Hilal ARSLAN

NumPy

H. Arslan Scientific Computing May 25, 2023 1 / 41


Outline

• NumPy (short for Numerical Python): Numerical Computing in


Python
• Also known as array oriented computing
• Typed multidimentional arrays (matrices)
• Fast numerical computations (matrix math)
• High-level math functions
• Libraries written in lower-level languages such as C

H. Arslan Scientific Computing May 25, 2023 2 / 41


NumPy

• NumPy (short for Numerical Python): Numerical Computing in


Python
• Created by Travis Oliphant in 2005
• NumPy library is an essential library for scientific computing in Python

H. Arslan Scientific Computing May 25, 2023 3 / 41


What does NumPy make attractive ?

• Working with large dataset, Python is extremely slow


• Most machine learning applications written in Python use a library
known as Numpy for the data manipulation
• NumPy is written in C so it runs far faster than python and proves
highly advantageous for speeding up applications written in Python
• NumPy provides a convenient Python interface for working with
multi-dimensional array data structures efficiently
• NumPy array data structure is also called ndarray, which is short for
n-dimensional array
• provides speeds-up data processing

H. Arslan Scientific Computing May 25, 2023 4 / 41


NumPy Data Structure: Array

• We have learnt about various data types like list, tuple, and dictionary
• Now we discuss another datatype ”Array”
• An array is a data type used to store multiple values using a single
identifier (variable name)
• An array contains an ordered collection of data elements where each
element is of the same type and can be referenced by its index
(position)

H. Arslan Scientific Computing May 25, 2023 5 / 41


Array

• Each element of the array is same data type, though the values
stored in them may be different
• The entire array is stored contiguously in memory. This makes
operations on array fast
• Each element of the array is identified or referred using the name of
the array along with the index of that element, which is unique for
each element. The index of an element is an integral value associated
with the element, based on the element’s position in the array. For
example consider an array with 5 numbers: [ 10, 9, 99, 71, 90 ]

H. Arslan Scientific Computing May 25, 2023 6 / 41


Array

H. Arslan Scientific Computing May 25, 2023 7 / 41


NumPy Array

• NumPy arrays are used to store lists of numerical data, vectors and
matrices
• The NumPy library has a large set of routines (built-in functions) for
creating, manipulating, and transforming NumPy arrays
• NumPy array is officially called ndarray but commonly known as array.

H. Arslan Scientific Computing May 25, 2023 8 / 41


Why do we need NumPy ?
• Python does numerical computations slowly

H. Arslan Scientific Computing May 25, 2023 9 / 41


Creation of NumPy Arrays from List

Observe that since there is a string value in the list, all integer and float
values have been promoted to string, while converting the list to array.

H. Arslan Scientific Computing May 25, 2023 10 / 41


H. Arslan Scientific Computing May 25, 2023 11 / 41
Creating 2-D Arrays

H. Arslan Scientific Computing May 25, 2023 12 / 41


H. Arslan Scientific Computing May 25, 2023 13 / 41
H. Arslan Scientific Computing May 25, 2023 14 / 41
Arrays

H. Arslan Scientific Computing May 25, 2023 15 / 41


Atributes of NumPy Array
• ndarray.ndim: gives the
number of dimensions of
the array as an
integer value. Arrays can
be 1-D, 2-D or n-D. We
focus on 1-D and
2-D arrays only
• ndarray.shape: It gives the
sequence of integers
indicating the size of
the array for each
dimension

Scientific Computing May 25, 2023 16 / 41


Atributes of NumPy Array

• ndarray.size: It gives the total number of elements of the array. This


is equal to the product of the elements of shape
• ndarray.dtype: is the data type of the elements of the array. All the
elements of an array are of same data type. Common data types are
int32, int64, float32, float64, U32, etc.

H. Arslan Scientific Computing May 25, 2023 17 / 41


Other Ways of Creating NumPy Arrays
1 We can specify data type (integer, float, etc.)

2 We can create an array with all elements initialised to 0 using the


function zeros()
3 We can create an array with all elements initialised to 1 using the
function ones()

H. Arslan Scientific Computing May 25, 2023 18 / 41


Other Ways of Creating NumPy Arrays

We can create an array with numbers in a given range and sequence using
the arange() function. This function is similar to the range() function of
Python

H. Arslan Scientific Computing May 25, 2023 19 / 41


Indexing and Slicing
• Indexing

• Slicing

H. Arslan Scientific Computing May 25, 2023 20 / 41


Arithmetic Operations

H. Arslan Scientific Computing May 25, 2023 21 / 41


Arithmetic Operations

H. Arslan Scientific Computing May 25, 2023 22 / 41


Reshaping Arrays

• The size of NumPy arrays is fixed, but the shape is not


• NumPy provides a reshape methods that allow us to obtain a view of
an array with a different shape
• For example, we can reshape a one-dimensional array into a
two-dimensional one using reshape as follows:

H. Arslan Scientific Computing May 25, 2023 23 / 41


Reshaping Arrays
We do not need to specify the number elements in each axis; NumPy is
smart enough to figure out how many elements to put along an axis if only
one axis is unspecified (by using the placeholder -1)

H. Arslan Scientific Computing May 25, 2023 24 / 41


Statistical Operations on Arrays

1 The max() function finds the maximum element from an array


2 The min() function finds the minimum element from an array
3 The sum() function finds the sum of all elements of an array
4 The mean() function finds the average of elements of the array
5 The std() function is used to find standard deviation of an array of
elements

H. Arslan Scientific Computing May 25, 2023 25 / 41


Functions to create arrays

• The ones function

H. Arslan Scientific Computing May 25, 2023 26 / 41


The arange function

H. Arslan Scientific Computing May 25, 2023 27 / 41


H. Arslan Scientific Computing May 25, 2023 28 / 41
Arithmetic Operations
NumPy uses operator overloading so that we can use mathematical
operators (+, -, /, *, and **) directly:

H. Arslan Scientific Computing May 25, 2023 29 / 41


Random Numbers

• In machine learning and deep learning, we often have to generate


arrays of random numbers
• for example, the initial values of our model parameters before
optimization.
• NumPy has a random subpackage to create random numbers and
samples from a variety of distributions conveniently.

H. Arslan Scientific Computing May 25, 2023 30 / 41

You might also like