Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
272 views

Data Science With Python Lesson 05 Mathematical Computing With Python NumPy

This document provides an overview of NumPy, the fundamental package for numerical computing in Python. NumPy supports multidimensional arrays (ndarrays) that allow fast and efficient execution of element-wise operations and mathematical functions over entire arrays. The key attributes of a NumPy ndarray include its dimensionality (.ndim), shape (.shape), and total size (.size). NumPy arrays are homogeneous and support common linear algebra and mathematical operations, making them superior to native Python lists for numerical tasks.

Uploaded by

Elie Mrad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
272 views

Data Science With Python Lesson 05 Mathematical Computing With Python NumPy

This document provides an overview of NumPy, the fundamental package for numerical computing in Python. NumPy supports multidimensional arrays (ndarrays) that allow fast and efficient execution of element-wise operations and mathematical functions over entire arrays. The key attributes of a NumPy ndarray include its dimensionality (.ndim), shape (.shape), and total size (.size). NumPy arrays are homogeneous and support common linear algebra and mathematical operations, making them superior to native Python lists for numerical tasks.

Uploaded by

Elie Mrad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Data Science with Python

Mathematical Computing with Python (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

You can change individual values in a list, but 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

Tools for reading/writing array


based datasets to disk

Efficient way of storing and


manipulating data

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 multi-dimensional.

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
Activity: Sequence it Right!

The code here is buggy. You must correct its sequence to debug it.

4
Activity: Sequence it Right!

4
Creating and Printing an ndarray

Demonstrate how to create and print a ndarray.


Classes and Attributes of ndarray: .ndim

Numpy’s 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
Classes and Attributes of ndarray: .ndim

The array np_city is one-dimensional, while the array np_city_with_state is two-


ndarray.ndim dimensional.

ndarray.shape

ndarray.size

ndarray.dtype
Concept Example
Classes and Attributes of ndarray: .shape

Numpy’s 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
Classes 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
Classes 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
Classes 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 the shape tuples of the arrays are used to calculate their
ndarray.ndim size.

ndarray.shape

ndarray.size

ndarray.dtype
Concept Example
Classes 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
Classes 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 !=
Executing Basic Operations

Demonstrate how to apply some basic operations on an array.


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
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 select


10 15 17 26
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)
Activity: Slice It!

Select any two elements from the array to see how the statement required to slice the range changes.

Rules of the Game


• Choose the first element of the range. Then, choose the element that ends the range.
• See how the values in the statement change according to your choices.
• Refresh to try again.

5 8 10 21

Select any two elements from the array.


Accessing Array Elements: Iteration

Use the iteration method to go through each data element present in the dataset.

Iterate with for loop


through 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 set criteria.

True False
Indexing with Boolean Arrays

Here, the original dataset contains test scores of two students. You can use a Boolean array 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

Setting 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/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/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/Shallow Copy Change value in copy

Copy object changed

Original dataset
Deep Copy retained
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
integer value of every element in the
exponentiation on each element.
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
Shape Manipulation: Example

You can use certain functions to manipulate the shape of an array to do the following:

Flattens the dataset

Changes or reshapes the dataset to 3 rows and 4 columns

Resizes again to 2 rows and 6 columns

Splits the array into two

Stacks the arrays together


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 value.
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


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 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
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 zeroes


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 zeroes

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.
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


Thank You

You might also like