Python Crash Course Scipy 3 rd year Bachelors V1.0 dd 05-09-2013 Hour 3.

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

Eigen Decomposition and Singular Value Decomposition
Python Crash Course Accuracy 3 rd year Bachelors V1.0 dd Hour 7.
Data mining and statistical learning - lecture 6
Refresher: Vector and Matrix Algebra Mike Kirkpatrick Department of Chemical Engineering FAMU-FSU College of Engineering.
Uncertainty Representation. Gaussian Distribution variance Standard deviation.
By Hrishikesh Gadre Session II Department of Mechanical Engineering Louisiana State University Engineering Equation Solver Tutorials.
Curve-Fitting Regression
Digital Image Processing Chapter 5: Image Restoration.
NOTES ON MULTIPLE REGRESSION USING MATRICES  Multiple Regression Tony E. Smith ESE 502: Spatial Data Analysis  Matrix Formulation of Regression  Applications.
Maximum-Likelihood estimation Consider as usual a random sample x = x 1, …, x n from a distribution with p.d.f. f (x;  ) (and c.d.f. F(x;  ) ) The maximum.
Lecture 11 Vector Spaces and Singular Value Decomposition.
July 3, Department of Computer and Information Science (IDA) Linköpings universitet, Sweden Minimal sufficient statistic.
Basic Mathematics for Portfolio Management. Statistics Variables x, y, z Constants a, b Observations {x n, y n |n=1,…N} Mean.
Linear and generalised linear models Purpose of linear models Least-squares solution for linear models Analysis of diagnostics Exponential family and generalised.
Matrix Approach to Simple Linear Regression KNNL – Chapter 5.
Stats & Linear Models.
By. What advantages has it? The Reasons for Choosing Python  Python is free  It is object-oriented  It is interpreted  It is operating-system independent.
EE513 Audio Signals and Systems Statistical Pattern Classification Kevin D. Donohue Electrical and Computer Engineering University of Kentucky.
Colorado Center for Astrodynamics Research The University of Colorado STATISTICAL ORBIT DETERMINATION Project Report Unscented kalman Filter Information.
Chapter 10 Review: Matrix Algebra
THEORETICAL STUDY OF SOUND FIELD RECONSTRUCTION F.M. Fazi P.A. Nelson.
Commented Demonstrations of Scilab, The free platform for Numerical Computation Michaël Baudin 3 November 2010.
Matrix Algebra. Quick Review Quick Review Solutions.
11.8 Power Series 11.9 Representations of Functions as Power Series Taylor and Maclaurin Series.
Principles of Helical Reconstruction David Stokes 2 DX Workshop University of Washington 8/15-8/19/2011.
Bessel Functions  Bessel functions, are canonical solutions y(x) of Bessel's differential equation: α (the order of the Bessel function) Bessel functions.
Course 12 Calibration. 1.Introduction In theoretic discussions, we have assumed: Camera is located at the origin of coordinate system of scene.
Review of Matrices Or A Fast Introduction.
418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.
Physics 114: Exam 2 Review Lectures 11-16
Scientific Computing with NumPy & SciPy NumPy Installation and Documentation  Not much on the home page—don’t buy the guide, it’s.
Eng Ship Structures 1 Introduction to Matlab.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Curve Fitting and Regression EEE 244. Descriptive Statistics in MATLAB MATLAB has several built-in commands to compute and display descriptive statistics.
ES 240: Scientific and Engineering Computation. Chapter 13: Linear Regression 13. 1: Statistical Review Uchechukwu Ofoegbu Temple University.
Curve-Fitting Regression
Multivariate Statistics Matrix Algebra I W. M. van der Veld University of Amsterdam.
SUPA Advanced Data Analysis Course, Jan 6th – 7th 2009 Advanced Data Analysis for the Physical Sciences Dr Martin Hendry Dept of Physics and Astronomy.
Mathematics and Statistics Boot Camp II David Siroky Duke University.
1 Multiple Regression A single numerical response variable, Y. Multiple numerical explanatory variables, X 1, X 2,…, X k.
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Introduction to Matrices and Matrix Approach to Simple Linear Regression.
Linear Algebra Libraries: BLAS, LAPACK, ScaLAPACK, PLASMA, MAGMA
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Chapter 2-OPTIMIZATION G.Anuradha. Contents Derivative-based Optimization –Descent Methods –The Method of Steepest Descent –Classical Newton’s Method.
Chapter 20 Statistical Considerations Lecture Slides The McGraw-Hill Companies © 2012.
Python & NetworkX Youn-Hee Han
Colorado Center for Astrodynamics Research The University of Colorado 1 STATISTICAL ORBIT DETERMINATION Kalman Filter with Process Noise Gauss- Markov.
Numerical Analysis – Data Fitting Hanyang University Jong-Il Park.
NumPy, SciPy, Mpi4Py Shepelenko Olha. History of NumPy Originally, Python was not developed as a language for numerical computing. However, due to its.
Part 3: Estimation of Parameters. Estimation of Parameters Most of the time, we have random samples but not the densities given. If the parametric form.
Introduction to Vectors and Matrices
PH2150 Scientific Computing Skills
Linear Algebra Review.
INTRODUCTION TO BASIC MATLAB
Two-view geometry Computer Vision Spring 2018, Lecture 10
Image Analysis Image Restoration.
Introduction to MATLAB
Matrices Definition: A matrix is a rectangular array of numbers or symbolic elements In many applications, the rows of a matrix will represent individuals.
J.-F. Pâris University of Houston
Nonlinear regression.
Singular Value Decomposition SVD
Communication and Coding Theory Lab(CS491)
5.2 Least-Squares Fit to a Straight Line
Python Crash Course Scipy
HYDROLOGY Lecture 12 Probability
Scientific Python Introduction
Introduction to Vectors and Matrices
Simulation And Modeling
Presentation transcript:

Python Crash Course Scipy 3 rd year Bachelors V1.0 dd Hour 3

SciPy SciPy is an Open Source library of scientific tools for Python. It depends on the NumPy library, and it gathers a variety of high level science and engineering modules together as a single package. SciPy provides modules for –file input/output –statistics –optimization –numerical integration –linear algebra –Fourier transforms –signal processing –image processing –ODE solvers –special functions –and more... See

SciPy: Statistics scipy.stats The main public methods for continuous Random Variables are: rvs: Random Variates pdf: Probability Density Function cdf: Cumulative Distribution Function sf: Survival Function (1-CDF) ppf: Percent Point Function (Inverse of CDF) isf: Inverse Survival Function (Inverse of SF) stats: Return mean, variance, (Fisher’s) skew, or (Fisher’s) kurtosis moment: non-central moments of the distribution >>> from scipy import stats >>> dir(stats)

SciPy: Statistics scipy.stats The >>> s = sp.randn(100) # same as numpy.random.randn >>> print len(s) 100 >>> print("Mean : {0:8.6f}".format(s.mean())) Mean : >>> print("Minimum : {0:8.6f}".format(s.min())) Minimum : >>> print("Maximum : {0:8.6f}".format(s.max())) Maximum : >>> print("Variance : {0:8.6f}".format(s.var())) Variance : >>> print("Std. deviation : {0:8.6f}".format(s.std())) Std. deviation : >>> from scipy import stats >>> n, min_max, mean, var, skew, kurt = stats.describe(s) >>> print("Number of elements: {0:d}".format(n)) Number of elements: 100 >>> print("Minimum: {0:8.6f} Maximum: {1:8.6f}".format(min_max[0], min_max[1])) Minimum: Maximum: >>> print("Mean: {0:8.6f}".format(mean)) Mean: >>> print("Variance: {0:8.6f}".format(var)) Variance: >>> print("Skew : {0:8.6f}".format(skew)) Skew : >>> print("Kurtosis: {0:8.6f}".format(kurt)) Kurtosis:

Example: Numerical integration Bessel's equation arises when finding separable solutions to Laplace's equation and the Helmholtz equation in cylindrical or spherical coordinates. Bessel functions are therefore especially important for many problems of wave propagation and static potentials. In solving problems in cylindrical coordinate systems, one obtains Bessel functions of integer order (α = n); in spherical problems, one obtains half-integer orders (α = n+1/2). For example: –Electromagnetic waves in a cylindrical waveguide –Heat conduction in a cylindrical object –Modes of vibration of a thin circular (or annular) artificial membrane (such as a drum or other membranophone) –Diffusion problems on a lattice –Solutions to the radial Schrödinger equation (in spherical and cylindrical coordinates) for a free particle –Solving for patterns of acoustical radiation –Frequency-dependent friction in circular pipelines Bessel functions also appear in other problems, such as signal processing (e.g., see FM synthesis, Kaiser window, or Bessel filter).

Example: Numerical integration Integrate a Bessel function jv(2.5,x) from 0 to 4.5: >>> import numpy as np >>> import scipy as sp >>> from scipy import integrate # scipy sub-packages have to be imported separately >>> from scipy import special >>> result = integrate.quad(lambda x: special.jv(2.5,x), 0, 4.5) >>> print result ( , e-09) >>> I = sqrt(2/pi)*(18.0/27*sqrt(2)*cos(4.5)-4.0/27*sqrt(2)*sin(4.5)+ :... sqrt(2*pi)*special.fresnel(3/sqrt(pi))[0]) >>> print I >>> print abs(result[0]-I) e-11

SciPy: Linear Algebra scipy.linalg SciPy is built using the optimized ATLAS LAPACK (Linear Algebra PACKage) and BLAS (Basic Linear Algebra Subprograms) libraries, it has very fast linear algebra capabilities. All of these linear algebra routines expect an object that can be converted into a 2-dimensional array. The matrix class is initialized with the SciPy command mat which is just convenient short-hand for matrix. >>> A = matrix(' ; ') >>> A [[ 1. 2.] [ 3. 4.]] >>> type(A) # file where class is defined >>> B = mat('[ ; ]') >>> B [[ 1. 2.] [ 3. 4.]] >>> type(B) # file where class is defined

SciPy – Linear Algebra >>> A = mat('[1 3 5; 2 5 1; 2 3 8]') >>> A matrix([[1, 3, 5], [2, 5, 1], [2, 3, 8]]) >>> A.I matrix([[-1.48, 0.36, 0.88], [ 0.56, 0.08, -0.36], [ 0.16, -0.12, 0.04]]) >>> from scipy import linalg >>> linalg.inv(A) array([[-1.48, 0.36, 0.88], [ 0.56, 0.08, -0.36], [ 0.16, -0.12, 0.04]]) Matrix inversion.

SciPy – Linear Algebra >>> A = mat('[1 3 5; 2 5 1; 2 3 8]') >>> b = mat('[10;8;3]') >>> A.I*b matrix([[-9.28], [ 5.16], [ 0.76]]) >>> linalg.solve(A,b) array([[-9.28], [ 5.16], [ 0.76]]) Solving linear system S = A -1 B where S=[ x y z] and B = [ ]

SciPy – Linear Algebra >>> A = mat('[1 3 5; 2 5 1; 2 3 8]') >>> linalg.det(A) Finding Determinant

SciPy – Linear Algebra Solving linear least-squares problems The model Minimize After some algebra, vector/matrix representaions of this problem As an example solve for model:

SciPy – Linear Algebra from numpy import * from scipy import linalg import matplotlib.pyplot as plt c1,c2= 5.0,2.0 i = r_[1:11] xi = 0.1*i yi = c1*exp(-xi)+c2*xi zi = yi *max(yi)*random.randn(len(yi)) A = c_[exp(-xi)[:,newaxis],xi[:,newaxis]] c,resid,rank,sigma = linalg.lstsq(A,zi) xi2 = r_[0.1:1.0:100j] yi2 = c[0]*exp(-xi2) + c[1]*xi2 plt.plot(xi,zi,'x',xi2,yi2) plt.axis([0,1.1,3.0,5.5]) plt.xlabel('$x_i$') plt.title('Data fitting with linalg.lstsq') plt.show()

SciPy – Singular value decomposition >>> A = mat('[1 3 2; 1 2 3]') >>> M,N = A.shape >>> U,s,Vh = linalg.svd(A) >>> Sig = mat(linalg.diagsvd(s,M,N)) >>> U, Vh = mat(U), mat(Vh) >>> print U [[ ] [ ]] >>> print Sig [[ ] [ ]] >>> print Vh [[ e e e-01] [ e e e-01] [ e e e-01]] Singular Value Decompostion (SVD) can be thought of as an extension of the eigenvalue problem to matrices that are not square. Let A be an M x N matrix. SVD of A can be written as With U = A H A and V= AA H and  is the matrix of eigenvectors

Example: Linear regression import scipy as sp from scipy import stats import pylab as plt n=50 # number of points x=sp.linspace(-5,5,n) # create x axis data a, b=0.8, -4 y=sp.polyval([a,b],x) yn=y+sp.randn(n) #add some noise (ar,br)=sp.polyfit(x,yn,1) yr=sp.polyval([ar,br],x) err=sp.sqrt(sum((yr-yn)**2)/n) #compute the mean square error print('Linear regression using polyfit') print(‘Input parameters: a=%.2f b=%.2f’ % (a,b)) print(‘Regression: a=%.2f b=%.2f, ms error= %.3f' % (ar,br,err)) plt.title('Linear Regression Example') plt.plot(x,y,'g--') plt.plot(x,yn,'k.') plt.plot(x,yr,'r-') plt.legend(['original','plus noise', 'regression']) plt.show() (a_s,b_s,r,xx,stderr)=stats.linregress(x,yn) print('Linear regression using stats.linregress') print('parameters: a=%.2f b=%.2f’ % (a,b)) print(‘regression: a=%.2f b=%.2f, std error= %.3f' % (a_s,b))

Example: Least squares fit from pylab import * from numpy import * from matplotlib import * from scipy.optimize import leastsq fp = lambda v, x: v[0]/(x**v[1])*sin(v[2]*x) # parametric function v_real = [1.7, 0.0, 2.0] fn = lambda x: fp(v_real, x) # fn to generate noisy data e = lambda v, x, y: (fp(v,x)-y) # error function n, xmin, xmax = 30, 0.1, 5 # Generate noisy data to fit x = linspace(xmin,xmax,n) y = fn(x) + rand(len(x))*0.2*(fn(x).max()-fn(x).min()) v0 = [3., 1, 4.] # Initial parameter values v, success = leastsq(e, v0, args=(x,y), maxfev=10000) # perform fit print ‘Fit parameters: ', v print ‘Original parameters: ', v_real X = linspace(xmin,xmax,n*5) # plot results plot(x,y,'ro', X, fp(v,X)) show()

SciPy: Ordinary Differential Equations Simple differential equations can be solved numerically using the Euler-Cromer method, but more complicated differential equations may require a more sophisticated method. The scipy library for Python contains numerous functions for scientific computing and data analysis. It includes the function odeint for numerically solving sets of first-order, ordinary differential equations (ODEs) using a sophisticated algorithm. rewritten as the following two first-order differential equations, The function y will be the first element y[0] (remember that the lowest index of an array is zero, not one) and the derivative y ′ will be the second element y[1]. You can think of the index as how many derivatives are taken of the function. In this notation, the differential equiations are from scipy import odeint from pylab import * # for plotting commands def deriv(y,t): # return derivatives of the array y a = -2.0 b = -0.1 return array([ y[1], a*y[0]+b*y[1] ]) time = linspace(0.0,10.0,1000) yinit = array([0.0005,0.2]) # initial values y = odeint(deriv,yinit,time) figure() plot(time,y[:,0]) # y[:,0] is the first column of y xlabel(‘t’) ylabel(‘y’) show()

Introduction to language End