Is5312 Week10-V2
Is5312 Week10-V2
NumPy
What is NumPy?
• NumPy stands for Numerical Python.
• NumPy is a Python library used for working with arrays.
• It also has functions for working in the domain of linear algebra,
Fourier transform, and matrices.
Why use NumPy?
• In Python, we have lists that 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
traditional Python lists.
• The array object in NumPy is called ndarray, it provides many
supporting functions that make working with ndarray very easy.
Import NumPy
import numpy import numpy as np
arr = numpy.array([1, 2, 3, 4, 5]) arr = np.array((1, 2, 3, 4, 5))
print(arr) print(arr)
-- [1 2 3 4 5] -- [1 2 3 4 5]
print(type(arr))
-- <class 'numpy.ndarray'>
Dimensions in Arrays
• 0-D Arrays
• arr = np.array(42)
• 1-D Arrays 0
1
• arr = np.array([1, 2, 3, 4, 5]) 2
• 2-D Arrays 3
[[[[[1 2 3 4]]]]]
number of dimensions : 5
Access Array Elements
2
7 6
[7 8 9]
[3 8]
[[2 3 4]
[7 8 9]]
Data Types in NumPy
• i - integer
• b - boolean
• u - unsigned integer
• f - float
int64
• c - complex float
• m - timedelta
• M - datetime
• O - object
• S - string
• U - unicode string
• V - fixed chunk of memory for other type ( void )
Shape of an Array
(2, 4)
[[[[[1 2 3 4]]]]]
shape of array : (1, 1, 1, 1, 4)
Numpy – Creating Vectors
Vector: 1D Array
Matrix: 2D Array
Tensor: 3D or higher Array
Axes: Dimensions
Shape: Tuple of integers giving
the size of the array along with
each dimension
Numpy – Creating Matrices
Numpy – Creating Tensors
Numpy – More arrays
Numpy – Array Type Conversion
Numpy – Indexing and slicing-1D
IS5312 - CityU 8
Numpy – Indexing and slicing-2D
[8 4 6]]
IS5312 - CityU 8
Numpy – Basic Array Operations
Numpy – Basic Array Operations
Numpy – Basic Array Operations
Numpy – Basic Array Operations
Broadcasting: between array and scalar
Numpy – Basic Array Operations
Numpy – Basic Array Operations
Numpy – Basic Array Operations
Creating Arrays with Random Numbers
Creating Arrays with Random Numbers
Creating Arrays with Random Numbers
Creating Arrays with Random Numbers
Creating Arrays with Random Numbers
Creating Arrays with Random Numbers
Array Transposing and Reshaping
Array Transposing and Reshaping
Reverse Arrays
Reverse Arrays
Flattening multidimensional arrays
Numpy – More indexing with conditions
[8 4 6]]
IS5312 - CityU 8
where(): processing elements with conditions
[8 4 6]]
IS5312 - CityU 8
In-class exercise1
• Create a 2-D numpy array with random integers (0-100) inclusive using random.randint(). Display the array
and
• then create a new array by replacing all odd numbers with -1. Display the new array and return the positions
where elements of two arrays match.
• Create a (3,3) 2-D ndarray with arange(). Then swap 1 and 2 columns and 1 and 2 row.
• Create a 1-D ndarray with (1-50) values using uniform(). Replace all >30 values to 30 and all <10 values to 10
• Create a (3,10) 2-D array with values (1-10) both inclusive. Count the frequency of each number per row and
display
• the frequencies similar as follows:
IS5312 - CityU 8
Numpy – Implementing formulas
IS5312 - CityU 8
Numpy – unique() function
IS5312 - CityU 8
Numpy – unique() function
IS5312 - CityU 8
In-class exercise2
• Create a (3,10) 2-D array with values (1-10) both inclusive. Count the frequency of each number per row and
display
• the frequencies similar as follows:
IS5312 - CityU 8
Numpy – Implementing formulas
IS5312 - CityU 8
Numpy – Creating arrays from file
[8 4 6]]
IS5312 - CityU 8