0% found this document useful (0 votes)
13 views51 pages

Is5312 Week10-V2

NumPy is a Python library that is used for working with multi-dimensional arrays and matrices. It contains powerful functions for operations such as linear algebra, Fourier transforms, and generating random numbers. NumPy aims to provide fast operations on arrays by using optimized C code under the hood. NumPy arrays, called ndarrays, can represent vectors, matrices, tensors, and multidimensional data. NumPy allows performing operations on whole arrays rather than individual elements, making data processing and scientific computing with Python much faster.

Uploaded by

lengbiao111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views51 pages

Is5312 Week10-V2

NumPy is a Python library that is used for working with multi-dimensional arrays and matrices. It contains powerful functions for operations such as linear algebra, Fourier transforms, and generating random numbers. NumPy aims to provide fast operations on arrays by using optimized C code under the hood. NumPy arrays, called ndarrays, can represent vectors, matrices, tensors, and multidimensional data. NumPy allows performing operations on whole arrays rather than individual elements, making data processing and scientific computing with Python much faster.

Uploaded by

lengbiao111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

IS5312 Week 10:

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

• arr = np.array([[1, 2, 3], [4, 5, 6]]) ndim: the number of dimensions


• 3-D arrays
• arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
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.

[[[[[1 2 3 4]]]]]
number of dimensions : 5
Access Array Elements

2
7 6

2nd element on 1st row: 2


5th element on 2nd row: 10
Slicing arrays

[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

 From lists using numpy.array


 .shape: get the array size
 .dtype: get the data type

 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

IS5312 - CityU WK10 PYTHON DATA VISUALIZATION 9


Numpy – Creating arrays from file

IS5312 - CityU WK10 PYTHON DATA VISUALIZATION 9


Numpy – Creating arrays from file

IS5312 - CityU WK10 PYTHON DATA VISUALIZATION 9


Numpy – Creating arrays from file

IS5312 - CityU WK10 PYTHON DATA VISUALIZATION 9


Numpy – Creating arrays from file

IS5312 - CityU WK10 PYTHON DATA VISUALIZATION 9


Numpy – Creating arrays from file

IS5312 - CityU WK10 PYTHON DATA VISUALIZATION 9


In-class exercise3
Use loadtxt() or genfromtxt() functions to read data from names.txt file. Display the names and associated
Frequencies.

[8 4 6]]
IS5312 - CityU 8

You might also like