Python Crash Course Scipy

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
Python Crash Course Scipy 3 rd year Bachelors V1.0 dd Hour 3.
Uncertainty Representation. Gaussian Distribution variance Standard deviation.
Curve-Fitting Regression
Digital Image Processing Chapter 5: Image Restoration.
1 5.The Gamma Function (Factorial Function ) 5.1 Definition, Simple Properties At least three different, convenient definitions of the gamma function are.
July 3, Department of Computer and Information Science (IDA) Linköpings universitet, Sweden Minimal sufficient statistic.
Linear and generalised linear models Purpose of linear models Least-squares solution for linear models Analysis of diagnostics Exponential family and generalised.
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.
THEORETICAL STUDY OF SOUND FIELD RECONSTRUCTION F.M. Fazi P.A. Nelson.
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.
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.
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.
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.
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.
Python Scripting for Computational Science CPS 5401 Fall 2014 Shirley Moore, Instructor October 6,
Introduction to Vectors and Matrices
PH2150 Scientific Computing Skills
Linear Algebra Review.
Chapter 7. Classification and Prediction
Introduction The central problems of Linear Algebra are to study the properties of matrices and to investigate the solutions of systems of linear equations.
Review of Linear Algebra
786 Data Analysis Options.
Background on Classification
Physics 114: Exam 2 Review Weeks 7-9
Lecture 13 Contents Partial Differential Equations
Lecture: MATLAB Chapter 1 Introduction
LECTURE 10: DISCRIMINANT ANALYSIS
Systems of Ordinary Differential Equations Case I: real eigenvalues of multiplicity 1 MAT 275.
Final Examination (F2017) ME 130 Applied Engineering Analysis
INTRODUCTION TO BASIC MATLAB
(Mohammed Sami) Ashhab
Image Analysis Image Restoration.
CSE Social Media & Text Analytics
Matrices Definition: A matrix is a rectangular array of numbers or symbolic elements In many applications, the rows of a matrix will represent individuals.
Python for Scientific Computing
Statistical Methods For Engineers
Chapter 27.
J.-F. Pâris University of Houston
Final Examination (S2018) ME 130 Applied Engineering Analysis-01
Nonlinear regression.
Singular Value Decomposition SVD
Interpolation and curve fitting
Communication and Coding Theory Lab(CS491)
5.2 Least-Squares Fit to a Straight Line
HYDROLOGY Lecture 12 Probability
Scientific Python Introduction
LECTURE 09: DISCRIMINANT ANALYSIS
Introduction to Vectors and Matrices
Simulation And Modeling
Chapter 2, Linear Systems, Mainly LU Decomposition
Lecture 13 Contents Partial Differential Equations
Presentation transcript:

Python Crash Course Scipy

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 http://docs.scipy.org/doc/scipy/reference/tutorial/

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 : -0.105170 >>> print("Minimum : {0:8.6f}".format(s.min())) Minimum : -3.091940 >>> print("Maximum : {0:8.6f}".format(s.max())) Maximum : 2.193828 >>> print("Variance : {0:8.6f}".format(s.var())) Variance : 0.863475 >>> print("Std. deviation : {0:8.6f}".format(s.std())) Std. deviation : 0.929233 >>> 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: -3.091940 Maximum: 2.193828 >>> print("Mean: {0:8.6f}".format(mean)) Mean: -0.105170 >>> print("Variance: {0:8.6f}".format(var)) Variance: 0.872197 >>> print("Skew : {0:8.6f}".format(skew)) Skew : -0.146500 >>> print("Kurtosis: {0:8.6f}".format(kurt)) Kurtosis: 0.117884

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 (1.1178179380783249, 7.866317182537226e-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 1.11781793809 >>> print abs(result[0]-I) 1.03761443881e-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('1.0 2.0; 3.0 4.0') >>> A [[ 1. 2.] [ 3. 4.]] >>> type(A) # file where class is defined <class 'numpy.matrixlib.defmatrix.matrix'> >>> B = mat('[1.0 2.0; 3.0 4.0]') >>> B >>> type(B) # file where class is defined

SciPy – Linear Algebra Matrix inversion. [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], 8

SciPy – Linear Algebra Solving linear system S = A-1 B where S=[ x y z] and B = [ 10 8 3] >>> 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], 9

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

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: 11

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 + 0.05*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() 12

SciPy – Singular value decomposition 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 = AHA and V= AAH and  is the matrix of eigenvectors >>> 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 [[-0.70710678 -0.70710678] [-0.70710678 0.70710678]] >>> print Sig [[ 5.19615242 0. 0. ] [ 0. 1. 0. ]] >>> print Vh [[ -2.72165527e-01 -6.80413817e-01 -6.80413817e-01] [ -6.18652536e-16 -7.07106781e-01 7.07106781e-01] [ -9.62250449e-01 1.92450090e-01 1.92450090e-01]] 13

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

Introduction to language End