PYTHON
NUMPY – ARRAY PROCESSING
https://jalammar.github.io/visual-numpy/
LECTURE - 09 Dr. Noor Felemban
© Dr. Irfan, Dr. Nida
NUMPY INTRODUCTION
WHAT IS NUMPY?
• NumPy stands for Numerical Python.
• NumPy is a python library used for working with arrays.
• Includes functions in domain of linear algebra, Fourier transform, and matrices.
WHAT IS NUMPY?
• Lists can serve the purpose of arrays, but they are slow to process. NumPy aims
to provide an array object that is up to 50x faster than lists.
• Array object in NumPy is called ndarray, come up with a lot of supporting
functions
NUMPY INTRODUCTION
INSTALLATION OF NUMPY CHECKING NUMPY VERSION
• pip install numpy
IMPORT NUMPY
• import numpy
OR
• import numpy as np
NUMPY CREATING ARRAYS
CREATE A NUMPY NDARRAY OBJECT
• The array object in NumPy is called ndarray.
• We can create a NumPy ndarray object by using the array() function.
NUMPY CREATING ARRAYS
DIMENSIONS IN ARRAYS
• A dimension in arrays is one level
of array depth (nested arrays).
0-D ARRAYS
• 0-D arrays, or Scalars, where value
in an array is a 0-D array.
NUMPY CREATING ARRAYS
1-D ARRAYS 2-D ARRAYS
NUMPY CREATING ARRAYS CHECK NUMBER OF DIMENSIONS?
3-D ARRAYS NumPy Arrays provides the ndim
attribute that returns an integer
that tells us how many dimensions
the array have.
NUMPY CREATING ARRAYS
HIGHER DIMENSIONAL ARRAYS
• An array can have any number of dimensions.
• When the array is
created, you can define
the number of
dimensions by using the
ndmin argument.
HOW PYTHON HANDLE ARRAY
• In 2D array, the 1st Dim is used as
reference, while 2nd Dim is used for storing
values
• In 3D array, the 1st and 2nd Dim are used
as reference, while 3rd Dim is used for
storing values
• Generally, for any number of dimensional
array, the last dim is used for storing the
values, while rest of the dim are used as a
reference (store address of the next dim).
ACCESSING NUMPY ARRAY VIA INDEXING
• Array indexing is the same as LIST in Python, it have positive indexing
and also negative indexing.
• You can access an array element by referring to its index number (either
positive or negative)
ACCESSING 1-D ARRAYS ACCESSING 2-D ARRAYS
ACCESSING NUMPY ARRAY VIA INDEXING
ACCESSING 3-D ARRAYS
NEGATIVE INDEXING
NUMPY ARRAY SLICING
SLICING ARRAYS
Slicing in python means taking elements from one given index to another
given index.
• We pass slice instead of index like this: [start:end].
• We can also define the step, like this: [start:end:step].
• If we don't pass start its considered 0
• If we don't pass end its considered length of array in that dimension
• If we don't pass step its considered 1
NUMPY ARRAY SLICING
1-D ARRAYS
• Slice elements from index 1 to index 5
from the following array
• Slice elements from index 4 to the end
of the array
• Slice elements from the beginning to
index 4 (not included)
NUMPY ARRAY SLICING
NEGATIVE SLICING
• Use the minus operator to refer to an
index from the end
• Slice from the index -3 from the end to
index -1 from the end.
STEP IN SLICING
• Use the step value to determine the step
of the slicing
• Return every other element from index 1
to index 5
NUMPY ARRAY SLICING
STEP IN SLICING
• Return every other element from the
entire array
WHAT’S THE OUTCOME
NUMPY ARRAY SLICING
2-D ARRAYS
• From the second element, slice elements
from index 1 to index 4 (not included)
Row=1 (fixed), columns=1:4
• From both elements, return index 2
Row=0:2, columns=2 (fixed)
NUMPY ARRAY SLICING
2-D ARRAYS
• From both elements, slice index 1 to
index 4 (not included), this will return a
2-D array
NUMPY DATA TYPES
DATA TYPES IN PYTHON
• strings - used to represent text data, the text is given under quote
marks. eg. "ABCD"
• integer - used to represent integer numbers. eg. -1, -2, -3
• float - used to represent real numbers. eg. 1.2, 42.42
• boolean - used to represent True or False.
• complex - used to represent a number in complex plain. eg. 1.0 +
2.0j, 1.5 + 2.5j
NUMPY DATA TYPES CHECKING THE DATA TYPE
OF AN ARRAY
DATA TYPES IN NUMPY
NumPy has some extra data types, and refer
to data types with one character, like i for
integers, u for unsigned integers etc.
• i - integer • M - datetime
• b - boolean • O - object
• u - unsigned integer • S - string
• f - float • U - unicode string
• c - complex float • V - fixed chunk of memory for other type ( void )
• m - timedelta
NUMPY DATA TYPES
CREATING ARRAYS WITH A DEFINED DATA TYPE
• We use the array() function to create arrays, • For i, u, f, S and U we
this function can take an optional argument: can define size as well.
dtype that allows us to define the expected
data type of the array elements
NUMPY DATA TYPES
WHAT IF A VALUE CAN NOT BE CONVERTED?
• If a type is given in which elements can't be casted then NumPy will raise
a ValueError
ValueError: In Python ValueError is raised when the type of
passed argument to a function is unexpected/incorrect.
• A non integer string like 'a' can not be converted to integer (will raise an
error)
NUMPY DATA TYPES
CONVERTING DATA TYPE ON EXISTING ARRAYS
• The best way to change the data type of an existing array, is to make
a copy of the array with the astype() method.
• The astype() function creates a copy of the array and allows you to
specify the data type as a parameter.
• The data type can be specified
using a string, like 'f' for float, 'i'
for integer etc. or use float for
float and int for integer.
NUMPY ARRAY COPY VS VIEW
THE DIFFERENCE BETWEEN COPY AND VIEW
• Copy create new array from existing
• Any changes made to the copy will not affect original array, and
• Any changes made to the original array will not affect the copy
• View is another pointer to array
• Any changes made to the view will affect the original array, and
• Any changes made to the original array will affect the view
NUMPY ARRAY COPY VS VIEW
COPY
• Make a copy, change the original
array, and display both arrays
• The copy SHOULD NOT be
affected by the changes made to
the original array
NUMPY ARRAY COPY VS VIEW
VIEW
• Make a view, change the original
array, and display both arrays
• The view SHOULD be affected by
the changes made to the original
array
NUMPY ARRAY COPY VS VIEW
CHECK IF ARRAY OWNS IT’S DATA
• COPY own the data, and VIEW
does not own the data,
but how can we check this?
• Every NumPy array has the attribute base that returns NONE if the
array owns the data.
• Otherwise, the base attribute refers to the original object.
NUMPY ARRAY SHAPE
• Shape that returns a tuple with each index having the number of
corresponding elements, (return shape of the array)
NUMPY ARRAY RESHAPE
• Reshaping means changing the shape of an array.
• The shape of an array is the number of elements in each dimension.
• By reshaping we can add or remove dimensions or change number of
elements in each dimension.
NUMPY ARRAY RESHAPE
RESHAPE FROM 1-D TO 2-D EXAMPLE
• Convert the following 1-D array with 12 elements into a 2-D array.
• The outermost dimension will have 4 arrays, each with 3 elements:
NUMPY ARRAY RESHAPE
RESHAPE FROM 1-D TO 3-D EXAMPLE
• Convert the following 1-D array with 12 elements into a 3-D array.
• The outermost dimension will have 2 arrays that contains 3 arrays,
each with 2 elements:
NUMPY ARRAY RESHAPE
CAN WE RESHAPE INTO ANY SHAPE?
• We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D
array but we cannot reshape it into a 3 elements 3 rows 2D array as
that would require 3x3 = 9 elements.
EXAMPLE
• Try converting 1D array with 8 elements to a 2D array with 3
elements in each dimension (will raise an error):
NUMPY ARRAY RESHAPE
FLATTENING THE ARRAYS
• Flattening array means converting a multidimensional array into a 1D
array.
• We can use reshape(-1) to do this.
EXAMPLE
• Convert the array into a 1D array:
NUMPY ARRAY ITERATING
• Iterating means going through elements one by one.
• As we deal with multi-dimensional arrays in numpy, we can do this
using basic FOR loop of python.
NUMPY ARRAY ITERATING
• To return the actual value, we can use separate FOR loop for each
dimension.
NUMPY ARRAY ITERATING
ITERATING ARRAYS USING NDITER()
• The nditer() is used to print array elements from very basic to very
advanced iterations.
NUMPY ARRAY ITERATING
ENUMERATED ITERATION USING NDENUMERATE()
• Enumeration means mentioning sequence number of somethings one by
one.
• The ndenumerate() return both index and value.
NUMPY JOINING ARRAY
• In SQL we join tables based on a key, whereas in NumPy we join
arrays by axes.
• We pass a sequence of arrays that we want to join to the
concatenate() function, along with the axis.
• If axis is not explicitly passed, it is taken as 0..
NUMPY SPLITTING ARRAY
• Splitting is reverse operation of Joining, i.e. Joining merges multiple arrays into
one and Splitting breaks one array into multiple.
• We use array_split() for splitting arrays
• we pass it the array we want to split and the number of splits.
Try the following options:
• Replace array_split() with split()
• Change the number of splits
NUMPY SPLITTING ARRAY
NUMPY SEARCHING ARRAY
• You can search an array for a certain value, and return the indexes
that get a match.
• To search an array, use the where() method
NUMPY SORTING ARRAY
• The NumPy ndarray object has a function called sort(), that will sort a
specified array
NUMPY FILTER ARRAY
• Getting some elements out of an existing array and creating a new
array out of them is called filtering.
• In NumPy, you filter an array using a boolean index list.
• A boolean index list is a list of booleans corresponding to indexes in the array.
• If index is True that element is contained in the filtered array
• If index is False that element is excluded from the filtered array.
OUTPUT
RANDOM NUMBERS IN NUMPY
• NumPy offers the random module to work with random numbers.
SIZE parameter specify the
shape of an array
RANDOM NUMBERS IN NUMPY
• NumPy offers the random module to work with random numbers.
GENERATE RANDOM NUMBER FROM ARRAY
• The choice() method allows you to generate a random value based on
an array of values.
• The choice() method takes an array as a parameter and randomly
returns one of the values.
RANDOM PERMUTATIONS
PERMUTATION Re-arrangement of elements
• The NumPy Random module provides two methods for this: shuffle()
and permutation()
The shuffle() method makes The permutation() method returns a re-arranged
changes to the original array. array (and leaves the original array un-changed).
END OF LECTURE - 09
© Dr. Irfan, Dr. Nida