Scientific Programming. Lecture A08 Numpy

Similar documents
(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB

Programming for Engineers in Python

NumPy. Daniël de Kok. May 4, 2017

CME 193: Introduction to Scientific Python Lecture 5: Numpy, Scipy, Matplotlib

Part VI. Scientific Computing in Python. Alfredo Parra : Scripting with Python Compact Max-PlanckMarch 6-10,

CME 193: Introduction to Scientific Python Lecture 6: Numpy, Scipy, Matplotlib

5 File I/O, Plotting with Matplotlib

The SciPy Stack. Jay Summet

Intro to Research Computing with Python: Visualization

Episode 8 Matplotlib, SciPy, and Pandas. We will start with Matplotlib. The following code makes a sample plot.

Numerical Calculations

NumPy. Arno Proeme, ARCHER CSE Team Attributed to Jussi Enkovaara & Martti Louhivuori, CSC Helsinki

LECTURE 19. Numerical and Scientific Packages

Lecture 15: High Dimensional Data Analysis, Numpy Overview

Python for Scientists

Effective Programming Practices for Economists. 10. Some scientific tools for Python

MO101: Python for Engineering Vladimir Paun ENSTA ParisTech

Introduction to NumPy

NumPy Primer. An introduction to numeric computing in Python

HW0 v3. October 2, CSE 252A Computer Vision I Fall Assignment 0

LECTURE 22. Numerical and Scientific Packages

NumPy quick reference

Plotting With matplotlib

Problem Based Learning 2018

Visualisation in python (with Matplotlib)

CME 193: Introduction to Scientific Python Lecture 5: Object Oriented Programming

Numpy fast array interface

python 01 September 16, 2016

NumPy is suited to many applications Image processing Signal processing Linear algebra A plethora of others

NumPy and SciPy. Lab Objective: Create and manipulate NumPy arrays and learn features available in NumPy and SciPy.

Chapter 5 : Informatics Practices. Class XII ( As per CBSE Board) Numpy - Array. New Syllabus Visit : python.mykvs.in for regular updates

Derek Bridge School of Computer Science and Information Technology University College Cork

Python Matplotlib. MACbioIDi February March 2018

NAVIGATING UNIX. Other useful commands, with more extensive documentation, are

python numpy tensorflow tutorial

Part VI. Scientific Computing in Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Why NumPy / SciPy? NumPy / SciPy / Matplotlib. A Tour of NumPy. Initializing a NumPy array

Advanced Python on Abel. Dmytro Karpenko Research Infrastructure Services group Department for Scientific Computing USIT, UiO

Lab 1 - Basic ipython Tutorial (EE 126 Fall 2014)

Scientific Computing with Python. Quick Introduction

Implement NN using NumPy

MS6021 Scientific Computing. TOPICS: Python BASICS, INTRO to PYTHON for Scientific Computing

Using the Matplotlib Library in Python 3

Python Crash Course Numpy, Scipy, Matplotlib

NumPy. Computational Physics. NumPy

MLCV 182: Practical session 1 Ron Shapira Weber Computer Science, Ben-Gurion University

cosmos_python_ Python as calculator May 31, 2018

Pandas and Friends. Austin Godber Mail: Source:

Skills Quiz - Python Edition Solutions

Introduction to Python: The Multi-Purpose Programming Language. Robert M. Porsch June 14, 2017

NumPy and SciPy. Shawn T. Brown Director of Public Health Applications Pittsburgh Supercomputing Center Pittsburgh Supercomputing Center

Scientific Programming. Lecture A07 Pandas


Introducing Python Pandas

Introduction to Python and NumPy I

Data Science with Python Course Catalog

Homework 11 - Debugging

Week Two. Arrays, packages, and writing programs

Derek Bridge School of Computer Science and Information Technology University College Cork

Image Processing in Python

(Creating Arrays & Matrices) Applied Linear Algebra in Geoscience Using MATLAB

Introduction to Artificial Neural Networks and Deep Learning

Introduction to Wavelets

MAS212 Scientific Computing and Simulation

F21SC Industrial Programming: Python: Python Libraries

Bi 1x Spring 2014: Plotting and linear regression

AMath 483/583 Lecture 28 June 1, Notes: Notes: Python scripting for Fortran codes. Python scripting for Fortran codes.

4. BASIC PLOTTING. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

MATPLOTLIB. Python for computational science November 2012 CINECA.

IAP Python - Lecture 4

Handling arrays in Python (numpy)

Ch.1 Introduction. Why Machine Learning (ML)? manual designing of rules requires knowing how humans do it.

Computational Physics

Python Numpy (1) Intro to multi-dimensional array & numerical linear algebra. Harry Lee January 29, 2018 CEE 696

LECTURE 22. Numerical and Scientific Computing Part 2

Phys Techniques of Radio Astronomy Part 1: Python Programming LECTURE 3

Exercise: Introduction to NumPy arrays

Command Line and Python Introduction. Jennifer Helsby, Eric Potash Computation for Public Policy Lecture 2: January 7, 2016

L15. 1 Lecture 15: Data Visualization. July 10, Overview and Objectives. 1.2 Part 1: Introduction to matplotlib

Introduction to Scientific Computing with Python, part two.

numpy-scipy February 11, 2015

Python Tools for Science

INTRODUCTION TO DATA SCIENCE

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

A. Python Crash Course

Activity recognition and energy expenditure estimation

Introduction to Python

PYTHON NUMPY TUTORIAL CIS 581

Partial Differential Equations II: 2D Laplace Equation on 5x5 grid

Paraphrase Identification; Numpy; Scikit-Learn

Python for Data Analysis. Prof.Sushila Aghav-Palwe Assistant Professor MIT

PATTERN RECOGNITION AND MACHINE LEARNING

Basic Beginners Introduction to plotting in Python

A New Cyberinfrastructure for the Natural Hazards Community

Intro to scientific Python in 45'

Python (version 3.6) for R Users: Stat Modules I

Python for Data Analysis

LEARNING TO PROGRAM WITH MATLAB. Building GUI Tools. Wiley. University of Notre Dame. Craig S. Lent Department of Electrical Engineering

Computer Lab 1: Introduction to Python

David J. Pine. Introduction to Python for Science & Engineering

Transcription:

Scientific Programming Lecture A08 Alberto Montresor Università di Trento 2018/10/25 Acknowledgments: Stefano Teso, Documentation http://disi.unitn.it/~teso/courses/sciprog/python_appendices.html https://docs.scipy.org/doc/numpy-1.13.0/reference/ This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Table of contents 1 2 Matplotlib

What is? is a freely available library for performing efficient numerical computations over scalars, vectors, matrices, and more generally N-dimensional tensors. Features Flexible indexing and manipulation of arbitrary dimensional data. Many numerical routines (linear algebra, Fourier analysis, local and global optimization, etc.) using a variety of algorithms. Clean Python interface to many underlying libraries, which are used as efficient implementations of numerical algorithms. Support for random sampling from a variety of distributions. Alberto Montresor (UniTN) SP - 2018/10/25 1 / 43

Some links Official website http://www.numpy.org/ Official documentation https://docs.scipy.org/doc/numpy/ Source code https://github.com/numpy/numpy Alberto Montresor (UniTN) SP - 2018/10/25 2 / 43

Importing numpy The customary idiom to import numpy is: import numpy as np Importing specific sub-modules also makes sense, so feel free to write (in this case for the linear algebra module): import numpy.linalg as la Alberto Montresor (UniTN) SP - 2018/10/25 3 / 43

The array class ndarray An ndarray is an n-dimensional array (a.k.a. tensor) Concepts encountered in analysis and algebra can be implemented as arrays: vectors are 1D arrays, matrices are 2D arrays. Given an array, its dimensionality and type can be ascertained by using the shape, ndim and dtype attributes: import numpy as np x = np.zeros(10) print(x) print(x.shape) print(x.ndim) print(x.dtype) [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] (10,) 1 float64 Alberto Montresor (UniTN) SP - 2018/10/25 4 / 43

[[ 0. 0.] [ 0. 0.]]] Alberto Montresor (UniTN) SP - 2018/10/25 5 / 43 Creating arrays with various shapes Creating arrays from scratch can be useful to start off your mathematical code or for debugging purposes. Creating an all-zero array with np.zeros() Creating an all-one array with np.ones() import numpy as np print( np.zeros(2) ) print( np.ones( (2,2) ) ) print( np.zeros( (2,2,2) ) ) [ 0. 0.] [[ 1. 1.] [ 1. 1.]] [[[ 0. 0.] [ 0. 0.]]

Creating arrays with various shapes To create a diagonal matrix, use the diag() and ones() methods together. import numpy as np print( np.diag( np.ones(3) ) [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] Alberto Montresor (UniTN) SP - 2018/10/25 6 / 43

Creating arrays with various shapes The numpy (more powerful) analogue of range() is np.arange(). import numpy as np print( np.arange(10,100,10) ) print( np.arange(0.0,1.0,0.1)) print( np.linspace(0.0,1.0,11)) print( np.diag( np.arange(5) ) ) 10 20 30 40 50 60 70 80 90] [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ] [[0 0 0 0 0] [0 1 0 0 0] [0 0 2 0 0] [0 0 0 3 0] [0 0 0 0 4]] Alberto Montresor (UniTN) SP - 2018/10/25 7 / 43

Creating random arrays 5-element vector from a uniform distribution in [0,..., 10[ 5-element vector from a normal distribution with µ = 1 and σ = 0.2 A 3x3 matrix from a uniform distribution over {0, 1} import numpy as np print(np.random.uniform(0, 10, size=5)) print(np.random.normal(1, 0.2, size=5)) print(np.random.randint(0, 2, size=(3, 3))) [ 7.91700684 7.41652128 8.1393401 0.8123227 5.50427964] [ 1.14191823 0.89203955 1.09505607 0.8081311 0.82282836] [[0 0 0] [0 1 1] [0 1 1]] Alberto Montresor (UniTN) SP - 2018/10/25 8 / 43

Creating random arrays import numpy as np import matplotlib.pyplot as plt np.random.seed(0) rm = np.random.normal(0, 1, size=(100, 100)) plt.imshow(rm, cmap="gray", interpolation="nearest") plt.savefig("random-matrix.png") Alberto Montresor (UniTN) SP - 2018/10/25 9 / 43

Creating random arrays Alberto Montresor (UniTN) SP - 2018/10/25 10 / 43

From pandas to numpy and back It is very easy to convert a Series or DataFrame into an array: as a matter of facts, these Pandas types are built on numpy arrays: their underlying array is accessible through the values attribute. import pandas as pd import numpy as np iris = pd.read_csv("iris.csv") print(type(iris.petalwidth.values)) <type numpy.ndarray > Alberto Montresor (UniTN) SP - 2018/10/25 11 / 43

Re-shaping, un-raveling The arrangement of entries into rows, columns, tubes, etc. can be changed at any time using the reshape() and ravel() methods: reshape() turns a vector into an array ravel() turns an array into a vector import numpy as np x = np.arange(9) print(x) y = x.reshape((3, 3)) print(y) z = y.ravel() print(z) [0 1 2 3 4 5 6 7 8] [[0 1 2] [3 4 5] [6 7 8]] [0 1 2 3 4 5 6 7 8] Alberto Montresor (UniTN) SP - 2018/10/25 12 / 43

Iterating over an array It is possible to iterate over arrays in several ways: Iteration over all elements of an ndarray for element in A.flat: print(element) Iteration over multidimensional arrays is done on slices in the first dimension for row in A: print(row) Alberto Montresor (UniTN) SP - 2018/10/25 13 / 43

Indexing x[i,j] extracts the element in row i, column j just like in standard matrix algebra. You can also use the colon : notation to select specify all the elements on an axis (e.g. a column, a row, a sub-array). import numpy as np x = np.arange(9).reshape((3, 3)) print(x) print(x[0,:]) # First row print(x[:,0]) # First column print(x[1:3, :]) # Subarray [[0 1 2] [3 4 5] [6 7 8]] [0 1 2] [0 3 6] [[3 4 5] [6 7 8]] Alberto Montresor (UniTN) SP - 2018/10/25 14 / 43

Indexing The same syntax applies to the n-dimensional case import numpy as np x = np.arange(5**5).reshape((5,)*5) print(x.shape) print(x[0,0,:,:,0]) (5, 5, 5, 5, 5) [[ 0 5 10 15 20] [ 25 30 35 40 45] [ 50 55 60 65 70] [ 75 80 85 90 95] [100 105 110 115 120]] Alberto Montresor (UniTN) SP - 2018/10/25 15 / 43

Multi-dimensional arrays It can be difficult to conceptualize operations over n-dimensional arrays, when n is larger than 3, unless they have concrete semantics. Example Assume that a 4D array holds the performance of several sequence alignment algorithms over multiple sequence clusters in multiple DBs: The first axis is the index of the algorithm The second axis is the index of the database The third axis is the index of a cluster of sequences The fourth axis is one of three measures of performance: precision, recall, and accuracy Alberto Montresor (UniTN) SP - 2018/10/25 16 / 43

Multi-dimensional arrays Your tensor looks like this: performances[alg][db][cluster][measure] You may define symbolic constants to identify the indexes of the columns. E.g., NEEDLEMAN = 0 # Needleman-Wunsch SMITH = 1 # Smith-Waterman PRECISION = 0 RECALL = 1 ACCURACY = 2 To extract the accuracy of the Needleman-Wunsch algorithm over all databases and sequence clusters, you can just do: print(performances[needleman,:,:,accuracy]) Alberto Montresor (UniTN) SP - 2018/10/25 17 / 43

Arithmetic broadcasting Operations between scalars and arrays are broadcast, like in Pandas (and more generally in linear algebra) import numpy as np x = np.arange(5)*10 print(x) y = -x print(y) z = x+y print(z) [ 0 10 20 30 40] [ 0-10 -20-30 -40] [0 0 0 0 0] Alberto Montresor (UniTN) SP - 2018/10/25 18 / 43

Arithmetic broadcasting Operations can update the sub-arrays automatically. import numpy as np x = np.arange(16).reshape((4, 4)) print(x) x[1:3,1:3] += 100 print(x) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] [[ 0 1 2 3] [ 4 105 106 7] [ 8 109 110 11] [ 12 13 14 15]] Alberto Montresor (UniTN) SP - 2018/10/25 19 / 43

Compatible sizes Operations between arrays of different shapes but compatible sizes are broadcast by matching the last (right-most) dimension. e.g, addition between a matrix x and a vector y broadcasts y over all rows of x: import numpy as np x = np.arange(9).reshape((3, 3)) print(x) y = np.arange(3) print(y) z = x + y print(z) [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] [0 1 2] [[ 0 2 4] [ 3 5 7] [ 6 8 10] Alberto Montresor (UniTN) SP - 2018/10/25 20 / 43

Incompatible sizes Operations between arrays of incompatible sizes raise an error. >>> np.arange(3) + np.arange(2) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: operands could not be broadcast together with shapes (3,) (2,) Alberto Montresor (UniTN) SP - 2018/10/25 21 / 43

Mathematical functions contains several mathematical functions, that can be applied to tensors (besides being applicable to scalar values). https://docs.scipy.org/doc/numpy/reference/routines.math.html Alberto Montresor (UniTN) SP - 2018/10/25 22 / 43

Mathematical functions import numpy as np import matplotlib.pyplot as plt # The x values x = 2 * np.pi /100 * np.arange(100) # The y values, uncorrupted y = np.sin(x) plt.plot(x, y) # The y values, now corrupted by Gaussian noise y += np.random.normal(0, 0.05, size=100) plt.plot(x, y) plt.savefig("sin.png") Alberto Montresor (UniTN) SP - 2018/10/25 23 / 43

Mathematical functions Alberto Montresor (UniTN) SP - 2018/10/25 24 / 43

Linear Algebra provides a number of linear algebraic functions, including dot products, matrix-vector multiplication, and matrix-matrix multiplication. # dot product between vectors x = np.array([0, 0, 1, 1]) y = np.array([1, 1, 0, 0]) print(np.dot(x, x)) print(np.dot(y, y)) print(np.dot(x, y)) # Matrix-vector product a = 2 * np.diag(np.ones(4)) print(np.dot(a, x)) print(np.dot(a, y)) # Matrix-matrix product b = np.ones((4, 4)) - a print(np.dot(a, b)) 2 2 0 [ 0. 0. 2. 2.] [ 2. 2. 0. 0.] [[-2. 2. 2. 2.] [ 2. -2. 2. 2.] [ 2. 2. -2. 2.] [ 2. 2. 2. -2.]] Alberto Montresor (UniTN) SP - 2018/10/25 25 / 43

Linear Algebra also provides routines for dealing with eigenvalues/eigenvects, singular value decompositions, and other decompositions. import numpy as np import numpy.linalg as la eigenvalues, eigenvectors = la.eig(np.ones((4, 4))) print(eigenvalues) print(eigenvectors) [ 0.00000000e+00 4.00000000e+00 0.00000000e+00 2.80731443e-32] [[-0.8660254-0.5-0.8660254-0.64641535] [ 0.28867513-0.5 0.28867513-0.32788993] [ 0.28867513-0.5 0.28867513 0.48715264] [ 0.28867513-0.5 0.28867513 0.48715264]] Alberto Montresor (UniTN) SP - 2018/10/25 26 / 43

Table of contents 1 2 Matplotlib

Matplotlib Why use MatPlotLib? https://xkcd.com/1895/ Alberto Montresor (UniTN) SP - 2018/10/25 27 / 43

Matplotlib Why use MatPlotLib? Alberto Montresor (UniTN) SP - 2018/10/25 28 / 43

Matplotlib What is MatPlotLib? MatPlotLib is a plotting library for the Python programming language and its numerical mathematics extension NumPy Two approaches The underlying general purpose MatPlotLib provides the general framework for plotting several types of figures The MatPlotLib module pyplot provides a declarative way of plotting data, really similar to the syntax of MatLab pyplot is simpler than the general framework, and allows you to produce figures very quickly. However, the full power is given by the entire framework. Alberto Montresor (UniTN) SP - 2018/10/25 29 / 43

Matplotlib MatPlotLib plotting script General structure import the required modules (numpy as np and matplotlib.pyplot as plt), load or generate some data, (optional) customize the appearance of your figure, generate a figure (plot(), bar(), pie() etc), display it or save it in a file (many formats: PNG, PDF, SVG etc) Alberto Montresor (UniTN) SP - 2018/10/25 30 / 43

Matplotlib Example 1 import numpy as np import matplotlib.pyplot as plt x = np.linspace(-np.pi, np.pi, 200) y1 = np.sin(x) y2 = np.cos(x) plt.title("my first plot") plt.plot(x, y1, "r-") plt.plot(x, y2, color="blue", linewidth=2.0, linestyle=":") plt.ylabel( y value ) plt.xlabel( x value ) plt.savefig("figure1.pdf", bbox_inches= tight ) Alberto Montresor (UniTN) SP - 2018/10/25 31 / 43

Matplotlib Example 1 1.00 My first plot 0.75 0.50 0.25 y value 0.00 0.25 0.50 0.75 1.00 3 2 1 0 1 2 3 x value Alberto Montresor (UniTN) SP - 2018/10/25 32 / 43

Matplotlib Example 2 plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r $-\pi$, r $-\pi/2$, r $0$, r $+\pi/2$, r $+\pi$ ]) plt.yticks([-1, 0, +1], [r $-1$, r $0$, r $+1$ ]) plt.plot(x, y1, "r-") plt.plot(x, y2, color="blue", linewidth=2.0, linestyle=":") ax = plt.gca() ax.spines[ right ].set_color( none ) ax.spines[ top ].set_color( none ) ax.xaxis.set_ticks_position( bottom ) ax.spines[ bottom ].set_position(( data,0)) ax.yaxis.set_ticks_position( left ) ax.spines[ left ].set_position(( data,0)) Alberto Montresor (UniTN) SP - 2018/10/25 33 / 43

Matplotlib Example 2 +1 0 /2 0 + /2 + 1 Alberto Montresor (UniTN) SP - 2018/10/25 34 / 43

Matplotlib Example 3 plt.plot(x, y1, "r-", label="sine") plt.plot(x, y2, "b:", label="cosine") plt.legend(loc= upper left, frameon=false) Alberto Montresor (UniTN) SP - 2018/10/25 35 / 43

Matplotlib Example 3 sine cosine +1 0 /2 0 + /2 + 1 Alberto Montresor (UniTN) SP - 2018/10/25 36 / 43

Matplotlib Example 4 t = 2*np.pi/3 plt.plot([t,t],[0,np.cos(t)], "b--") plt.scatter([t,],[np.cos(t),], 50, color = blue ) plt.annotate(r $\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$, xy=(t, np.sin(t)), xycoords= data, xytext=(+10, +30), textcoords= offset points, fontsize=16, arrowprops=dict(arrowstyle="->")) plt.plot([t,t],[0,np.sin(t)], "r--") plt.scatter([t,],[np.sin(t),], 50, color = red ) plt.annotate(r $\cos(\frac{2\pi}{3})=-\frac{1}{2}$, xy=(t, np.cos(t)), xycoords= data, xytext=(-90, -50), textcoords= offset points, fontsize=16, arrowprops=dict(arrowstyle="->")) Alberto Montresor (UniTN) SP - 2018/10/25 37 / 43

Matplotlib Example 4 sine cosine +1 sin( 2 3 ) = 3 2 0 /2 0 + /2 + 1 cos( 2 3 ) = 1 2 Alberto Montresor (UniTN) SP - 2018/10/25 38 / 43

Matplotlib Example 5 import numpy as np import matplotlib.pyplot as plt n = 1024 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) plt.scatter(x,y,s=1) plt.savefig("figure5.pdf") Alberto Montresor (UniTN) SP - 2018/10/25 39 / 43

Matplotlib Example 5 3 2 1 0 1 2 3 2 1 0 1 2 3 Alberto Montresor (UniTN) SP - 2018/10/25 40 / 43

Matplotlib Example 6 import numpy as np import matplotlib.pyplot as plt x1 = np.linspace(0.0, 5.0) x2 = np.linspace(0.0, 2.0) y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) y2 = np.cos(2 * np.pi * x2) plt.subplot(2, 1, 1) plt.plot(x1, y1, o- ) plt.title( A tale of 2 subplots ) plt.ylabel( Damped oscillation ) plt.subplot(2, 1, 2) plt.plot(x2, y2,.- ) plt.xlabel( time (s) ) plt.ylabel( Undamped ) plt.savefig("figure6.pdf") Alberto Montresor (UniTN) SP - 2018/10/25 41 / 43

Matplotlib Example 6 Damped oscillation Undamped 1.0 0.5 0.0 0.5 1.0 0.5 0.0 0.5 1.0 A tale of 2 subplots 0 1 2 3 4 5 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 time (s) Alberto Montresor (UniTN) SP - 2018/10/25 42 / 43

Matplotlib Tutorials http://www.labri.fr/perso/nrougier/teaching/matplotlib/ http://journals.plos.org/ploscompbiol/article?id=10.1371/ journal.pcbi.1003833 Alberto Montresor (UniTN) SP - 2018/10/25 43 / 43