Data Analytics with Python
Mathematical Computing Using NumPy
Learning Objectives
By the end of this lesson, you will be able to:
Explain NumPy and its importance
Discuss the basics of NumPy, including its fundamental objects
Demonstrate how to create and print a NumPy array
Analyze and perform basic operations in NumPy
Utilize shape manipulation and copying methods
Demonstrate how to execute linear algebraic functions
Build basic programs using NumPy
NumPy
Quick Recap: Lists
Below are some of the properties of lists:
List
Collection of values
Multiple types (heterogeneous)
Add, remove, update
Limitations of Lists
Though you can change individual values in a list, you cannot apply a mathematical operation over the entire list.
Mathematical operation over the entire distance and time lists
Error
Why NumPy?
Numerical Python (NumPy) supports multidimensional arrays over which you can easily apply mathematical operations.
Numpy
26 43 52
Arrays like lists, but BETTER
Import NumPy
Create “distance” and “time” NumPy arrays
Mathematical function applied over the entire “distance” and “time” arrays
Output
NumPy: Overview
NumPy is the foundational package for mathematical computing in Python.
It has the following properties:
Supports fast and efficient
multidimensional arrays (ndarray)
Executes element-wise
computations and mathematical
calculations
Performs linear algebraic
operations, fourier transforms,
and random number generation
Has tools for reading or writing
array-based datasets to disk
Stores and manipulates data
efficiently
Has tools for integrating language
codes (C, C++)
Properties of ndarray
An array in NumPy has the following properties:
Collection of values Multidimensional
Add, remove, change ndarray Mathematical function support
Single type (homogeneous) Fast and efficient
Purpose of ndarray
The ndarray in Python is used as the primary container to exchange data between algorithms.
Algorithm
[1, 2, 1]
Question/Problem
[[ 1, 0, 0],
[ 0, 1, 2]]
Sharing
Data
([[ 2, 8, 0, 6],
Algorithm [ 4, 5, 1, 1],
[ 8, 9, 3, 6]])
ndarray
Write Program
Algorithm
Types of Arrays
Arrays can be one-dimensional, two-dimensional, three-dimensional, or multidimensional.
One-Dimensional Array Two-Dimensional Array Three-Dimensional Array
Printed as rows Printed as matrices (2x3) Printed as list of matrices (3x3x3)
array([[[ 0, 1, 2],
1 axis [ 3, 4, 5],
array([5, 7,9]) array([[ 0, 1, 2], 2 axes
rank 1 [ 6, 7, 8]],
[ 5, 6, 7]]) rank 2
Length = 3 [[ 9, 10, 11],
3 axes
[12, 13, 14],
rank 3
Length = 3 [15, 16, 17]],
[[18, 19, 20],
5 7 9 0 1 2 [21, 22, 23],
y axis (0,0) (0,1) (0,2) [24, 25, 26]]])
0 1 2 5 6 7
x axis (1,0) (1,1) (1,2) y axis
Length = 3
x axis
x axis
z axis
Create and Print NumPy Arrays
Objective: Create the following types of NumPy arrays:
- One-dimensional array
- Array with zeros
- Array with ones
- Two-dimensional array
- Three-dimensional array
Access: To execute the practice, follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Class and Attributes of ndarray: .ndim
Numpy array class is ndarray, also referred to as numpy.ndarray. The attributes of ndarray are:
This refers to the number of axes (dimensions) of the array. It is also called the rank of the
ndarray.ndim array.
ndarray.shape
ndarray.size
Two axes or 2D array Three axes or 3D array
ndarray.dtype
Concept Example
Class and Attributes of ndarray: .ndim
The array np_city is one-dimensional, while the array np_city_with_state is two-dimensional.
ndarray.ndim
ndarray.shape
ndarray.size
ndarray.dtype
Concept Example
Class and Attributes of ndarray: .shape
Numpy array class is ndarray, also referred to as numpy.ndarray. The attributes of ndarray are:
This consists of a tuple of integers showing the size of the array in each dimension. The
ndarray.ndim length of the shape tuple is the rank or ndim.
ndarray.shape
2 rows, 3 columns 2 rows, 3 columns, 2 ranks
ndarray.size
Shape: (2, 3) Shape: (2, 3, 2)
ndarray.dtype
Concept Example
Class and Attributes of ndarray: .shape
Numpy’s array class is “ndarray,” also referred to as “numpy.ndarray.” The attributes of ndarray are:
The shape tuple of both the arrays indicate their size along each dimension.
ndarray.ndim
ndarray.shape
ndarray.size
ndarray.dtype
Concept Example
Class and Attributes of ndarray: .size
Numpy’s array class is “ndarray,” also referred to as “numpy.ndarray.” The attributes of ndarray are:
It gives the total number of elements in the array. It is equal to the product of the elements
ndarray.ndim of the shape tuple.
ndarray.shape
Array contains 6 elements Array contains 12 elements
ndarray.size
Array a = (2, 3) Array b = (2, 3, 2)
Size = 6 Size = 12
ndarray.dtype
Concept Example
Class and Attributes of ndarray: .size
Numpy’s array class is “ndarray,” also referred to as “numpy.ndarray.” The attributes of ndarray are:
Look at the examples to see how shape tuples of the arrays are used to calculate their size.
ndarray.ndim
ndarray.shape
ndarray.size
ndarray.dtype
Concept Example
Class and Attributes of ndarray: .dtype
Numpy’s array class is “ndarray,” also referred to as “numpy.ndarray.” The attributes of ndarray are:
It’s an object that describes the type of the elements in the array. It can be created or
ndarray.ndim specified using Python.
ndarray.shape
Array contains integers Array contains floats
ndarray.size Array a = [3, 7, 4] Array b = [1.3, 5.2, 6.7]
[2, 1, 0] [0.2, 8.1, 9.4]
[2.6, 4.2, 3.9]
[7.8, 3.4, 0.8]
ndarray.dtype
Concept Example
Class and Attributes of ndarray: .dtype
Numpy’s array class is “ndarray,” also referred to as “numpy.ndarray.” The attributes of ndarray are:
Both the arrays are of string data type (dtype) and the longest string is of length 7, which is
ndarray.ndim Houston.
ndarray.shape
ndarray.size
ndarray.dtype
Concept Example
Operations
Basic Operations
Using the following operands, you can easily apply various mathematical, logical, and comparison operations
on an array.
Mathematical Operations Logical Operations Comparison Operations
Addition + And & Greater >
Subtraction - Or | Greater or equal >=
Multiplication * Not ~ Less <
Division / Less or equal <=
Exponentiation ** Equal ==
Not equal !=
Basic Operations: Example
NumPy uses the indices of the elements in each array to carry out basic operations. In this case, where we are
looking at a dataset of four cyclists during two trials, vector addition of the arrays gives the required output.
First trial
Second trial
Total distance
Array (First trial) Array (Second trial) Array (Total distance)
10 15 17 26 + 12 11 21 24 = 22 26 38 50
0 1 2 3 0 1 2 3
Index Index
Vector addition
Executing Basic Operations in NumPy Array
Objective: Create a NumPy array and perform the following basic operations:
- Mathematical operations
- Comparison operations
- Logical operations
Access: To execute the practice, follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Performing Operations Using NumPy Array
Objective: Perform the following operations using NumPy array:
- Count the number of times each value appears in an array of integers
- Create a NumPy array [[0, 1, 2], [ 3, 4, 5], [ 6, 7, 8],[ 9, 10, 11]]) and filter the elements greater than five
- Create a NumPy array having NaN(Not a Number) and print it; also, print the same array omitting all
elements which are NaN
Example: array([ nan, 1., 2., nan, 3., 4., 5.])
- Create a 10x10 array with random values and find the minimum and maximum values
Access: To execute the practice, follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Unassisted Practice: Operations Using NumPy Array
Counts the occurrence of each element
Output
Checks the elements greater than five
Output
Unassisted Practice: Operations Using NumPy Array
NumPy array with NaN
Eliminate the NaN from the array
Output
NumPy array of random values
Minimum and maximum value in the array
Output
Accessing Array Elements: Indexing
You can access an entire row of an array by referencing its axis index.
1st set data 2nd set data
Create 2D array using cyclist trial data shown earlier
First trial data
Second trial data
2D array containing cyclists’ data
10 15 17 26 First trial (axis 0)
12 11 21 24 Second trial (axis 1)
Accessing Array Elements: Indexing
You can refer the indices of the elements in an array to access them. You can also select a particular index of more
than one axis at a time.
First cyclist: first trial data
First cyclist: all trial data
(Use “:” to select all the rows of an array)
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
Cyclist 1: first 10 15 17 26 Cyclist 1: all 10 15 17 26
trial data trials data
12 11 21 24 12 11 21 24
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
Accessing Array Elements: Slicing
Use the slicing method to access a range of values within an array.
Shape of the array
10 15 17 26
2 rows
Shape of the array 12 11 21 24
4 columns
Slicing the array
Use “:” to
10 15 17 26
select all rows 12 11 21 24
Slicing the array data [ : , 1 : 3] 0 1 2 3
where 1 is inclusive but 3 is not
Starting Ending
index (1) index (2)
Accessing Array Elements: Iteration
Use the iteration method to go through each data element present in the dataset.
Iterate with “for loop”
through the entire
dataset
Iterate with “for loop” through
the “two cyclist” datasets
Indexing With Boolean Arrays
Boolean arrays are useful when you need to select a dataset according to a set criteria.
Here, the original dataset contains test scores of two students. A Boolean array is used
to choose only the scores that are above a given value.
Test scores
Student 1 83 71 57 63 83 71 57 63
Student 2 54 68 81 45 54 68 81 45
Test score > 60
Test 1 Test 2 Test 3 Test 4
Set the passing score
Shows data elements which fit the
criteria (Boolean array)
Send passing score as an argument to test scores object
Copy and Views
When working with arrays, data is copied into new arrays only in some cases.
Following are the three possible scenarios:
In this method, a variable is directly assigned the value of another variable. No new copy is made.
Simple Assignments
Original dataset
View or Shallow Copy
Assigned dataset
Shows both objects are the same
Deep Copy
Copy and Views
A view, also referred to as a shallow copy, creates a new array object.
Simple Assignments
Original dataset
View or Shallow Copy Change value in “view” object
Original dataset
Deep Copy
changed
Copy and Views
Copy is also called deep copy because it entirely copies the original dataset. Any change in the copy
will not affect the original dataset.
Simple Assignments
Shows copy and original
object are different
Shows copy object data is not
owned by the original dataset
View or Shallow Copy Change value in copy
Copy object changed
Original dataset
Deep Copy retained
Demonstrate the Use of Copy and Views
Objective: Demonstrate how the following copies and views are generated from a memory location:
- Simple Assignment
- View or Shallow Copy
- Deep Copy
Access: To execute the practice, follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Universal Functions (ufunc)
NumPy provides useful mathematical functions called universal functions. These functions operate element-wise on
an array, producing another array as output. Some of these functions are:
sqrt function provides the square sqrt cos function gives cosine values for all
root of every element in the array. elements in the array.
cos
floor
floor function returns the largest exp exp function performs exponentiation
integer value of every element in the
on every element in the array.
array.
ufunc: Examples
Numbers for which square root will be calculated
Square root values
Import pi*
Trigonometric functions
Return the floor of the input element-wise
Exponential functions for complex
mathematical calculations
Shape Manipulation
You can use certain functions to manipulate the shape of an array.
The shape of an array can be changed according to the requirement using the NumPy library functions.
1 2 3
1 2 3 4 5 6 7 8 9 4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
Array shape Data
manipulation Wrangling
methods
Shape Manipulation
Some common methods for manipulating shapes are:
Flatten
Split Resize
Shape
Manipulation
Stack Reshape
Manipulate the Shape of an Array
Objective: Use common manipulation functions like ravel, reshape, resize, hsplit, and hstack to manipulate
the shape of a NumPy array.
Access: To execute the practice, follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Broadcasting
NumPy uses broadcasting to carry out arithmetic operations between arrays of different shapes. In this method,
NumPy automatically broadcasts the smaller array over the larger array.
array_a
2 3 5 8
Element-wise
multiplication
0.3 0.3 0.3 0.3
array_b
array_a
2 3 5 8
Broadcasting
0.3
scalar_c
If the shape doesn’t match with array_a, NumPy doesn’t have to create copies of scalar values.
Instead, broadcast scalar value over the entire array to find the product.
Broadcasting: Constraints
Though broadcasting can help carry out mathematical operations between different-shaped arrays, they are subject
to certain constraints as listed below:
• When NumPy operates on two arrays, it compares their
shapes element-wise. It finds these shapes compatible
only if:
• Their dimensions are the same or
• One of them has a dimension of size 1
• If these conditions are not met, a ValueError is thrown,
indicating that the arrays have incompatible shapes.
Broadcasting: Example
Let’s look at an example to see how broadcasting works to calculate the number of working hours of a worker per
day in a certain week.
Week one earnings
Week two earnings
Element-wise operation
Total earning for 2 weeks
Calculate week one hours
Hourly wage
Number of working hours
per day in week one
Linear Algebra: Transpose
NumPy can carry out linear algebraic functions as well. The transpose() function can help you interchange rows as
columns, and vice-versa.
transpose()
Axis 1 Axis 1
83 71 57 63
54 68 81 45
Axis 0
Axis 0
Linear Algebra: Inverse and Trace Functions
Using NumPy, you can also find the inverse of an array and add its diagonal data elements.
np.linalg.inv()
Inverse of the given array
* Can be applied only on a square matrix
np.trace()
Sum of diagonal elements “10” and “31”
Key Takeaways
You are now able to:
Explain NumPy and its importance
Discuss the basics of NumPy, including its fundamental objects
Demonstrate how to create and print a NumPy array
Analyze and perform basic operations in NumPy
Utilize shape manipulation and copying methods
Demonstrate how to execute linear algebraic functions
Build basic programs using NumPy
Knowledge Check
Knowledge
Check
Which of the following arrays is valid?
1
a. [1, 0.3, 8, 6.4]
b. [“Lucy”, 16, “Susan”, 23, “Carrie”, 37]
c. [True, False, “False”, True]
d. [3.14j, 7.3j, 5.1j, 2j]
Knowledge
Check
Which of the following arrays is valid?
1
a. [1, 0.3, 8, 6.4]
b. [“Lucy”, 16, “Susan”, 23, “Carrie”, 37]
c. [True, False, “False”, True]
d. [3.14j, 7.3j, 5.1j, 2j]
The correct answer is d
A NumPy ndarray can hold only a single data type, which makes it homogenous. NumPy supports integers, floats, Booleans,
and even complex numbers. Of all the options provided, only the array containing complex numbers is homogenous. All the
other options contain more than one data type.
Knowledge
Check Which function is most useful to convert a multidimensional array into a one-dimensional
array?
2
a. ravel()
b. reshape()
c. resize() and reshape()
d. All of the above
Knowledge
Check Which function is most useful to convert a multidimensional array into a one-dimensional
array?
2
a. ravel()
b. reshape()
c. resize() and reshape()
d. All of the above
The correct answer is a
The function ravel() is used to convert a multidimensional array into a one-dimensional array. Though reshape() also functions
in a similar way, it creates a new array instead of transforming the input array.
Knowledge
Check
The np.trace() method gives the sum of _____.
3
a. the entire array
b. the diagonal elements from left to right
c. the diagonal elements from right to left
d. consecutive rows of an array
Knowledge
Check
The np.trace() method gives the sum of _____.
3
a. the entire array
b. the diagonal elements from left to right
c. the diagonal elements from right to left
d. consecutive rows of an array
The correct answer is b
The trace() function is used to find the sum of the diagonal elements in an array. It is carried out in an incremental order of the
indices. Therefore, it can only add diagonal values from left to right and not vice-versa.
Knowledge
Check
The function np.transpose() when applied on a one-dimensional array gives _____.
4
a. a reverse array
b. an unchanged original array
c. an inverse array
d. all elements with zeros
Knowledge
Check
The function np.transpose() when applied on a one-dimensional array gives _____.
4
a. a reverse array
b. an unchanged original array
c. an inverse array
d. all elements with zeros
The correct answer is b
Transposing a one-dimensional array does not change it in any way. It returns an unchanged view of the original array.
Country GDP
Evaluate the dataset containing the GDPs of different countries to:
• Find and print the name of the country with the highest GDP
• Find and print the name of the country with the lowest GDP
• Print out text and input values iteratively
• Print out the entire list of the countries with their GDPs
• Print the highest GDP value, lowest GDP value, mean GDP value,
standardized GDP value, and the sum of all the GDPs
Olympic 2012 Medal Tally
Evaluate the dataset of the Summer Olympics, London 2012, to:
• Find and print the name of the country that won maximum number
of gold medals
• Find and print the countries who won more than 20 gold medals
• Print the medal tally
• Print each country name with the corresponding number of gold
medals
• Print each country name with the total number of medals won
Thank You