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

Lab Sheet 05 - Numpy and Matplotlib

This document provides an introduction to the NumPy package in Python. It discusses how NumPy arrays differ from regular Python lists, how to perform common array operations like creation, indexing, mathematical functions, and statistics. NumPy allows for efficient vector and matrix operations. Key differences between lists and arrays include homogeneous data types and vectorized operations in NumPy. The document also demonstrates common NumPy functions for linear algebra, solving systems of equations, and evaluating polynomials.

Uploaded by

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

Lab Sheet 05 - Numpy and Matplotlib

This document provides an introduction to the NumPy package in Python. It discusses how NumPy arrays differ from regular Python lists, how to perform common array operations like creation, indexing, mathematical functions, and statistics. NumPy allows for efficient vector and matrix operations. Key differences between lists and arrays include homogeneous data types and vectorized operations in NumPy. The document also demonstrates common NumPy functions for linear algebra, solving systems of equations, and evaluating polynomials.

Uploaded by

Sasmitha Kalhara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Computing GP106

General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

1 Numpy
1.1 Introduction
1. Numpy (Numerical Python) is the most basic and a powerful package for working with multi-dimensional
data in Python.

2. You can create and manipulate multi-dimensional objects using numpy. It also provides functionalities
for vector element-wise operations and operations related to high dimensional vectors (arrays, matrices,
tensors etc.)
3. Let’s start exploring the functionalities of numpy library using the Python terminal. Since this is not
an inbuilt module in Python, you have to install it by issuing the pip install numpy command in the
command line interface (CMD). [You won’t need this step for the lab machines.]
4. As any usual module, first, you have to import the module (library) using the import command.
1 >>> import numpy as np

5. Since we have already used Python lists to create matrices and lists, let’s identify key differences between
Python lists and numpy arrays.

• numpy arrays (lists) can handle vectorized operations. To create a numpy array, we use the array()
method.
1 >>> ls = [1 ,2 ,3 ,4]
2 >>> ls +1
3 TypeError : can only concatenate list ( not " int ") to list
4 >>> np_ls = np . array ( ls )
5 >>> np_ls + 2
6 array ([3 , 4 , 5 , 6])
7 >>> 1/ np_ls
8 array ([0.33333333 , 0.25 , 0.2 , 0.16666667])

• Unlike Python lists, items inside the numpy array should be of the same type. If not, the library
itself will convert the items to a unified data type. This conversion might vary depending on the
data types in the given list. [Below, in the example ’< U 21’ is for Unicode strings]
1 >>> ls = [1 ,2 , True ,1]
2 >>> np_arr = np . array ( ls )
3 >>> np_arr
4 array ([1 , 2 , 1 , 1])
5 >>> ls = [1 ,2 ," Hello " ,1]
6 >>> np_arr = np . array ( ls )
7 >>> np_arr
8 array ([ ‘1 ’ , ‘2 ’ , ‘ Hello ’ , ‘1 ’] , dtype = ’ < U21 ’)

• Once a numpy array is created, you can’t add additional items to the array. However, in Python
lists, we can use the append() function to add new items to the existing array. Adding a new element
to a numpy array, can be done in 3 steps. First, you to convert it to a Python list, then add an item
and lastly convert it again to a numpy array.
1 >>> np_arr = np . array ([1 ,2 ,3 ,4])
2 >>> new_arr = np_arr . tolist ()
3 >>> new_arr . append (9)
4 [1 ,2 ,3 ,4 ,9]
5 >>> np_arr = np . array ( new_arr )
6 array ([1 ,2 ,3 ,4 ,9])

Department of Computer Engineering, University of Peradeniya 1


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

6. Numpy has it’s own set of inbuilt methods to create matrices and vectors. The size of the desired
matrix/array is given as a tuple object (r × c).
1 >>> np . ones ((3 ,3) )
2 [[1. 1. 1.]
3 [1. 1. 1.]
4 [1. 1. 1.]]
5 >>> np . zeros ((3 ,3) )
6 [[0. 0. 0.]
7 [0. 0. 0.]
8 [0. 0. 0.]]

7. Furthermore, we can use np.arange() method to create arrays/lists which is same as the inbuilt function
range(). The only difference is arange() method will return a numpy.ndarray object instead of a Python
list object.
1 >>> np . arange (0 ,19 ,1)
2 [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]
3 >>> type ( np . arange (1 ,5 ,1) )
4 < class ‘ numpy . ndarray ’ >

8. After creating arrays, we can use the np.reshape() method to reshape it as we want. In here, the dimensions
are specified using the second argument (a tuple).
1 >>> np . reshape ( np . arange (0 ,20 ,1) ,(4 ,5) )
2 [[ 0 1 2 3 4]
3 [ 5 6 7 8 9]
4 [10 11 12 13 14]
5 [15 16 17 18 19]]

1.2 Matrix Operations


1. We can use the same np.array() function to construct matrices. To view the size, we use the shape option.
1 >>> matrix = np . array ([[1 ,2 ,3] ,[ -1 , -2 , -3] ,[6 ,7 ,8]])
2 >>> matrix
3 matrix ([[ 1 , 2 , 3] ,
4 [ -1 , -2 , -3] ,
5 [ 6 , 7 , 8]])
6 >>> matrix . shape
7 (3 , 3)

2. You can extract specific portions on an array using indexing starting with 0, something similar to how
you would do with Python lists slicing. For this, we use the : operator.
1 # All rows , second column
2 >>> matrix [: ,2]
3 matrix ([[ 3] ,
4 [ -3] ,
5 [ 8]])
6 # All rows , one column
7 >>> matrix [: ,:1]
8 matrix ([[ 1] ,
9 [ -1] ,

Department of Computer Engineering, University of Peradeniya 2


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

10 [ 6]])
11 # All rows , two columns
12 >>> matrix [: ,:2]
13 matrix ([[ 1 , 2] ,
14 [ -1 , -2] ,
15 [ 6 , 7]])

3. Furthermore, we can have some complex operations such as 2D sub-array manipulations.


1 >>> matrix [: ,0:2]
2 matrix ([[ 1 , 2] ,
3 [ -1 , -2] ,
4 [ 6 , 7]])
5 >>> matrix [:1 ,:]
6 matrix ([[1 , 2 , 3]])
7 >>> matrix [:2 ,:]
8 matrix ([[ 1 , 2 , 3] ,
9 [ -1 , -2 , -3]])
10 >>> matrix [2: ,:]
11 matrix ([[6 , 7 , 8]])
12 >>> matrix [1: ,:]
13 matrix ([[ -1 , -2 , -3] ,
14 [ 6 , 7 , 8]])

1.3 Mathematical Operations


1. We can use numpy inbuilt functions for element wise operations on numpy arrays. Let’s use the np.sin()
function to convert elements inside a vector to its sin() values.
1 >>> vector = np . array ([0.1 ,0.2 ,0.3 ,0.4 ,0.5 ,0.6])
2 >>> np . sin ( vector )
3 [0.09983342 0.19866933 0.29552021 0.38941834 0.47942554 0.56464247]

2. We can use the np.dot() function to compute the dot product of two matrices.
1 >>> mat1 = np . array ([[1 ,2] ,[ -1 , -2]])
2 >>> mat2 = np . array ([[4 ,5] ,[ -8 , -9]])
3 >>> np . dot ( mat1 , mat2 )
4 [[ -12 -13]
5 [ 12 13]]

3. To calculate the determinant of a matrix, we use the det() method provided in the numpy.linalg module.
1 >>> np . linalg . det ( mat2 )
2 3.9999

4. Often in Linear Algebra, we have to compute the inverse of a matrix. For this numpy.linalg module
provides inv() method.
1 >>> np . linalg . inv ( mat2 )
2 [[ -2.25 -1.25]
3 [ 2. 1. ]]

5. Moving on, we can use the solve() function to solve a system of linear equations provided in the form of
Ax = b.

x+y+z =6 2y + 5z = 4 2x + 5y − z = 27

Department of Computer Engineering, University of Peradeniya 3


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

1 >>> A = np . array ([[1 ,1 ,1] ,[0 ,2 ,5] ,[2 ,5 , -1]])


2 >>> B = np . array ([6 ,4 ,27])
3 >>> np . linalg . solve (A , B )
4 [2.71428571 4.14285714 -0.85714286]

6. We can use the same programming structure (with for loops) to iterate through a numpy matrix.
1 # GP106 Lab
2 # numpy with for loop
3

4 # import the module


5 import numpy as np
6

7 # create a matrix of size (3 ,3)


8 mat1 = np . reshape ( np . arange (0 ,9 ,1) ,(3 ,3) )
9

10 # get the shape of the matrix


11 (r , c ) = mat1 . shape
12

13 # print all the values


14 for x in range ( r ) :
15 for y in range ( c ) :
16 print ( " Item @ (% d ,% d ) : % d " %( x ,y , mat1 [x , y ]) )

7. Numpy also provides methods for statistical calculations. Following code segment describes the use of
mean() method. For this function we have to provide an extra argument called axis. This argument
specifies whether we are considering the row-wise mean or the column-wise mean in the matrix. By
default, it will compute the mean of the whole matrix.
1 # GP106 Lab
2 # Numpy with statistical methods
3

4 # import the module


5 import numpy as np
6

7 # create a matrix of size (3 ,3)


8 mat1 = np . reshape ( np . arange (0 ,9 ,1) ,(3 ,3) )
9

10 print ( mat1 )
11 print ( " Mean value of all items %.2 f " % np . mean ( mat1 ) )
12 print ( " Mean value of columns " , np . mean ( mat1 , axis =0) )
13 print ( " Mean value of rows " , np . mean ( mat1 , axis =1) )

8. Numpy polynomial and roots modules provides a set of useful functions for solving higher order polyno-
mials.

• To find the roots of a polynomial we use numpy.roots() function. The coefficients for the polynomial
should be passed as a Python list. Let’s find the roots of x3 + 3x + 1
1 >>> import numpy as np
2 >>> coeff = [1 ,0 ,3 ,1]
3 >>> np . roots ( coeff )
4 array ([ -3.27+0. j , 0.13+0.94 j , 0.13 -0.94 j ])

• To evaluate a polynomial at a given point, we use the polyval() function. As arguments, you have to
provide the evaluation point and the coefficient vector. For instance, if we want to evaluate x2 +4x+9
at 2 and 1, then the calculation looks like this,

Department of Computer Engineering, University of Peradeniya 4


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

1 >>> from numpy . polynomial . polynomial import polyval


2 >>> polyval (2 ,[9 ,4 ,1])
3 21.0
4 >>> polyval (1 ,[9 ,4 ,1])
5 14.0

9. A complete documentation of numpy library can be found in the official link: https://numpy.org/ or
simply, you can use the help() function.

2 Plotting 2D and 3D Graphs


We use Matplotlib to plot graphs in Python. You can use pip install matplotlib command to install it on
your machine. (you won’t need this step for the lab machines). A complete documentation on matplotlib can
be found in the https://matplotlib.org/3.1.1/.

Linear 2D Graphs
1. To plot a linear graph, we are using several helper functions provided in matplotlib.

• plot(x,y,color) : Function used to plot a graph. (x,y can be numpy arrays or a Python lists)
• title(str,fontsize) : Title of the graph.
• xlabel(str,fontsize) : X axis label of the graph.
• ylabel(str,fontsize) : Y axis label of the graph.
• grid() : Add a grid to the graph (optional).

2. Let’s plot the sin(x) where x is between [0, 3π]. For this step, first you have to import some libraries,
and then you have to use above functions.
3. We use linspace(start,end,n) function from numpy to generate n number of spaces between start to end.

Figure 1: Linear Plot of sin(x)

1 # GP106 Linear Plots


2 # import libraries
3 import matplotlib . pyplot as plt
4 import numpy as np
5

6 # create data arrays


7 x = np . linspace (0 ,3* np . pi ,1000)
8 y = np . sin ( x )

Department of Computer Engineering, University of Peradeniya 5


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

10 # plot the graph


11 plt . plot (x ,y , " g " )
12 plt . title ( " sin ( x ) x in range [0 ,3 pi ] " )
13 plt . xlabel ( " angle ( radians ) " )
14 plt . ylabel ( " sin ( x ) " )
15 plt . grid ()
16 plt . show ()

4. Figure 1 shows the rendered plot. After rendering, you can save it using the save button.

Linear Plots with Multiple Series


1. We can use the same set of functions to render multiple graphs in the same figure.

Figure 2: Linear Plots with Multiple Series

2. Figure 2 shows a figure containing multiple plots (sin(x) and cos(x)). To achieve this task we can call the
same plot() function again.
3. Since we are plotting two series, we have to add a legend to the graph. As shown in the following code
segment, we use legend([str]) to add a legend to the graph. You have to make sure that the header (graph
legend text) list is in the same order as the plotting order.
1 # GP106 Linear Plots
2

3 # import libraries
4 import matplotlib . pyplot as plt
5 import numpy as np
6

7 # create data arrays


8 x = np . linspace (0 ,3* np . pi ,100)
9 y1 = np . sin ( x )
10 y2 = np . cos ( x )
11

12 # plot the graphs


13 plt . plot (x , y1 , ’ - or ’)
14 plt . plot (x , y2 , ’ --g ’)
15

16 # format the figure


17 plt . title ( " sin ( x ) and cos ( x ) in range [0 ,3 pi ] " , fontsize =18)
18 plt . xlabel ( " angle ( radians ) " , fontsize =15)

Department of Computer Engineering, University of Peradeniya 6


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

19 plt . ylabel ( " sin ( x ) , cos ( x ) " , fontsize =15)


20 plt . legend ([ " sin ( x ) " ," cos ( x ) " ] , fontsize =15)
21 plt . grid ()
22 plt . show ()

4. Additionally, matplotlib provides options to format the plotting line. Here, we can use the -or to specify
a continuous red line segment with o pattern (scatter). Further, we can use - - to specify a dashed line
plot.

Bar Charts
1. To plot a bar chart matplotlib provides a function called bar(). This function has several mandatory
arguments, and the same function can be used to modify the bar chart as well (alignment, width etc).

Figure 3: Bar chart indicating the living cost in USA

2. Additionally, we use xticks() function to specify the x labels of the graph. As same as the plot() function,
wev have to provide the x location of the bar. Then the next argument is a tuple describing the independent
variable (i.e x).
3. The following code segment can be used to render a bar chart shown in Figure 3.
1 # GP106 Bar Plots
2

3 # import libraries
4 from matplotlib . ticker import FuncFormatter
5 import matplotlib . pyplot as plt
6 import numpy as np
7

8 # x locations of bars
9 x = np . arange (7)
10

11 money = [100 ,200 ,100 ,150 ,70 ,40 ,80]


12

13 plt . bar (x , money , color =[ " b " ]*7)


14 plt . xticks (x , ( " New York " , " California " , " Texas " , " Arizona " ,"
Pennsylvania " ," Ohio " ," Washington " ) , fontsize =12)
15 plt . grid ()
16 plt . ylabel ( " Cost ( $ ) " , fontsize =13)
17 plt . xlabel ( " City " , fontsize =13)
18 plt . title ( " Cost of Living " , fontsize =14)

Department of Computer Engineering, University of Peradeniya 7


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

19 plt . show ()

4. Edit the same code providing the width argument (enter the argument as width=0.8 ).

Pie Charts
1. Matplotlib provides pie() method to plot pie charts. Following code segment describes how to plot a pie
chart using matplotlib.

Figure 4: Pie chart for GP106 lab difficulty level

2. Here, the pie() method multiple arguments as shown in the code segment, and it will return some objects
which will be used as arguments to the legend() function.
1 # GP106 Pie chart example
2 import matplotlib . pyplot as plt
3

4 # plot labels
5 labels = [ " Too Easy " , " Easy " , " Moderate " , " Difficult " , " Very Difficult " ]
6

7 # data ( statistics )
8 sizes = [4.6 , 57.0 , 21.7 , 27.1 ,0.6]
9

10 # colours
11 colors = [ " yellowgreen " , " gold " , " lightskyblue " , " lightcoral " , " b " ]
12

13 # plot setup
14 patches , texts = plt . pie ( sizes , colors = colors , shadow = True , startangle
=90)
15 plt . legend ( patches , labels , loc = " best " )
16 plt . axis ( " equal " )
17 plt . title ( " GP106 Labs " , fontsize =14)
18 plt . show ()

3. See the difference after changing the shadow argument to False.

Department of Computer Engineering, University of Peradeniya 8


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

Sub-Plots
1. Often, you have to plot multiple graphs in the same figure. We can plot each graph on top of the first
graph (as shown in Multiple Series section) or we can plot it as a separate graph.
2. For this we use the subplot(r,c,loc) method. The r argument specifies the number of rows in the subplot,
c specifies the number columns and loc specifies the location of the graph/plot.
3. For instance, if you want to plot 3 graphs, we can specify the subplot() pattern as subplot(3,1,loc) or
either subplot(1,3,loc). As you can see, it is kind of a matrix of graphs where the loc parameter indicate
the plotting location.
4. The following code segment describes how to plot multiple graphs in the same figure using subplot()
1 import matplotlib . pyplot as plt
2 import numpy as np
3

4 x = np . linspace (0 ,5* np . pi ,100)


5 y1 = np . sin ( x ) , y2 = np . cos ( x )
6

7 # plot the graphs


8 plt . subplot (1 ,2 ,1)
9 plt . plot (x , y1 , " -r " )
10 plt . title ( " sin ( x ) " , fontsize =18)
11 plt . xlabel ( " angle ( radians ) " , fontsize =15)
12 plt . ylabel ( " sin ( x ) " , fontsize =15)
13 plt . grid ()
14

15 plt . subplot (1 ,2 ,2)


16 plt . plot (x , y2 , " g " )
17 plt . title ( " cos ( x ) " , fontsize =18)
18 plt . xlabel ( " angle ( radians ) " , fontsize =15)
19 plt . ylabel ( " cos ( x ) " , fontsize =15)
20 plt . grid ()

5. The resulting figure looks like this (see Figure 5).

Figure 5: Subplots

Additional Information
1. Colours other than basics:
matplotlib provides a basic set of colours which can be specified using a single letter (b: blue, g: green,

Department of Computer Engineering, University of Peradeniya 9


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

r: red, c: cyan). Additionally, it provides a set of named colours as well (https://matplotlib.org/3.


1.0/gallery/color/named_colors.html).
2. Real-time plots using ion() method.
For real-time interactive plots you can use the ion() method provided by matplotlib.pyplot. A working
example can be found in the link: https://pythonspot.com/matplotlib-update-plot/.
3. 3D plots:
Matplotlib provides the functionality to plot various types of 3D plots (3D Scatter, 3D Mesh, 3D Wire-
frame, 3D Surface etc.). More information on how to plot 3D plots using matplotlib can be found in the
https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html.
p Let’s plot a 3D surface plot of the
function z = cos( x2 + y 2 )

Figure 6: 3D surface plot

1 import matplotlib . pyplot as plt


2 from matplotlib . ticker import LinearLocator , Fo rma tS tr Fo rm at te r
3 import numpy as np
4

5 fig = plt . figure () , ax = fig . gca ( projection = " 3 d " )


6

7 # create the data X , Y


8 X = np . arange ( -10 , 10 , 0.25)
9 Y = np . arange ( -10 , 10 , 0.25)
10 X , Y = np . meshgrid (X , Y )
11 Z = np . cos ( np . sqrt ( X **2 + Y **2) )
12

13 # Plot the surface .


14 surf = ax . plot_surface (X , Y , Z , cmap = " YlGnBu " ,
15 linewidth =0 , antialiased = False )
16

17 # Customize the z axis .


18 ax . set_zlim ( -1.01 , 1.01)
19 ax . zaxis . se t_majo r_loca tor ( LinearLocator (10) )
20 ax . zaxis . s et _ m aj o r _f o r ma t t er ( For ma tS tr Fo rm at te r ( " %.02 f " ) )

Department of Computer Engineering, University of Peradeniya 10


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

21

22 # Add a color bar which maps values to colors .


23 fig . colorbar ( surf , shrink =0.5 , aspect =5)
24 plt . show ()

3 Bonus: Polynomial Curve Fitting


Often in Engineering measurement tasks, we fit the parameter variation to a some kind of a polynomial to
understand the behavior of it. Usually, we fit a set of data points (x, y) to a first order polynomial (i.e a line)
to see whether it is increasing or not. However, since, most of the engineering problems which we are dealing
with comprise of non-linear behavior, some times we fit the data set to a 2nd order polynomial. The following
code segment shows how to fit a data set (x, y readings) to a first and second order polynomials using numpy
curve fitting functions. [More information on these numerical computing methods can be found via the link:
https://en.wikipedia.org/wiki/Curve_fitting
1 import numpy as np
2 from numpy . polynomial . polynomial import polyval , polyfit
3 import matplotlib . pyplot as plt
4

6 # generate x , y using the polynomial 10 + x -2 x ^2 + x ^3


7 x = np . linspace (1 ,10 ,30)
8 y = [ polyval ( x_ ,[10 ,1 , -2 ,1]) for x_ in x ]
9

10 # fit the data to 1 st and 2 nd order polynomials


11 f1 = polyfit (x ,y , deg =1)
12 f2 = polyfit (x ,y , deg =2)
13

14 # evaluate them to estimate y


15 y_es_1 = [ polyval ( x_ , f1 ) for x_ in x ]
16 y_es_2 = [ polyval ( x_ , f2 ) for x_ in x ]
17

18 # plot
19 plt . plot (x ,y , " - og " ) , plt . plot (x , y_es_1 , " b " ) , plt . plot (x , y_es_2 , " r " )
20 plt . legend ([ " Data Points " , " 1 st Order " , " 2 nd Order " ])
21 plt . xlabel ( " X " ) , plt . ylabel ( " Y " )
22 plt . grid ()
23

Department of Computer Engineering, University of Peradeniya 11


Computing GP106
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020

Figure 7: Polynomial Fitting

Department of Computer Engineering, University of Peradeniya 12

You might also like