57

Migrating from matlab to python

Embed Size (px)

Citation preview

Page 1: Migrating from matlab to python
Page 2: Migrating from matlab to python

About the Speakers

Diane MuellerDirector, Enterprise ProductManagement at [email protected]

Dr. Mike Müller

CEO, Python Academy

[email protected]

Page 3: Migrating from matlab to python

Agenda

• About ActiveState & Python Academy

• Quick Comparison of Matlab & Python

• Deep Dive into Python’s "Pylab" functionality

• Includes: Python Basics, NumPy, SciPy, IPython, matplotlib

• Other considerations

• Q&A

Page 4: Migrating from matlab to python

About ActiveState

• Founded 1997

• 2 million developers, 97% of Fortune 1000 rely on ActiveState

• Development, management, distribution solutions for dynamiclanguages

• Core languages: Python, Perl, Tcl

• Other: PHP, Ruby, Javascript

Page 5: Migrating from matlab to python

About Python Academy

• specialist for Python training

• open and custom courses

• Python for programmers

• Python for scientists and engineers

• many more advanced topics (extensions, databases, GUI,web)

• Python consulting

Page 6: Migrating from matlab to python

ActiveState Solutions

Page 7: Migrating from matlab to python

Packages Include

• "Pylab": NumPy, SciPy, Matplotlib

• GUI Toolkits: pyQT, wxPython

• Database Connectors: Postgres, MySQL, Oracle, MSSQL,SqlAlchemy, ODBC

• Cryptography: M2Crypto

Page 8: Migrating from matlab to python

Quick ComparisonMATLAB

• Proprietary to Mathworks

• High-level language

• interactive environment

• Deep computationallibraries

• Visualizations

• Workbench

Python - "Pylab"

• Open Source

• High-level language

• Tools for building yourown "PyLab"

• With custom graphicaluser interfaces

• And interactive tools foriterative exploration,design, and problemsolving

• Multiple Developmentenvironments available

Page 9: Migrating from matlab to python

Python - Origin

• 1989/1990 Guido van Rossum

• in between C and shell scripting

• ideas from ABC, C, Smalltalk,Java, (Haskell)

• 1999 version 1.5.2

• now at 2.7 / 3.1 (3.2beta)

• Python Software Foundation

Page 10: Migrating from matlab to python

Open Source

• Python license

• Python Software Foundation

• core developers

• developers of third party libraries

• Python Enhancement Proposals (PEP)

Page 11: Migrating from matlab to python

Features

• simple

• consistent

• readable

• general purpose programming language

• research and implement in the same language

• yet good replacement for MATLAB (NumPy + matplotlib +IPython + SciPy)

• can do way more than MATLAB

Page 12: Migrating from matlab to python

Users

• Open Source

• Google (core developers)

• YouTube

• German Aerospace Center

• NASA

• Walt Disney

• Rackspace

• Financial sector (AQR)

• ...

Page 13: Migrating from matlab to python

Users

• programming novices

• professional software developers

• tester

• system admins

• scientists and engineers, quants

• Python scales

• easy to get started with

• many advanced features if needed (meta programming)

Page 14: Migrating from matlab to python

Popularity

Page 15: Migrating from matlab to python

Popularity

• easy to get started with

• MIT uses Python for "Introduction to CS and Programming"

• Michigan State replaced C++ successfully with Python

• embedded in Blender, Open Office, Inkscape, Gimp

• its just fun for lots of people

Page 16: Migrating from matlab to python

Operating Systems

• Windows

• Linux

• Mac

• Main-Frames

• Mobile

• DOS

Page 17: Migrating from matlab to python

Technology

• interpreter

• platform independent bytecode

• everything at run time

• simple meta-programming

Page 18: Migrating from matlab to python

Implementations

• CPython == Standard Python

• Jython in Java

• IronPython in C#

• PyPy in Python

• Stackless

• more

Page 19: Migrating from matlab to python

Example of File IOthe file data.txt:

a b c1 2 34 5 67 8 9

Page 20: Migrating from matlab to python

Example of File IO

>>> fobj = open('data.txt')>>> header = fobj.next().split()>>> header['a', 'b', 'c']>>> for line in fobj:... numbers.append([int(entry) for entry in line.split()])...>>> numbers[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Page 21: Migrating from matlab to python

Connecting to Your Data Sources

• csv module in standard library

• read and write Excel files (xlrd, xlwt)

• or use COM with MS Office

• read and write HDF5 files (PyTables, h5py)

• netCDF (pynetcdf, nectdf4-python, pupynere)

• binary files (struct from standard library)

• more (just google for desired format and Python)

Page 22: Migrating from matlab to python

Databases

• connect to all major database

• Oracle

• SQL-Server

• MySQL

• PostgresSQL

• DB2

• SQLite

• more

Page 23: Migrating from matlab to python

Object Relational Mappers

• SQLAlchemy

• SQLObject

• use object-oriented programming

• no need to write SQL by hand

• no SQL injection

Page 24: Migrating from matlab to python

NumPy

• work with numeric arrays

• MATLAB-like features

• implemented in C

• fast

• interactive

• 21 data types

Page 25: Migrating from matlab to python

Create Arrays

>>> import numpy as np>>> a = np.array([[1, 2, 3], [4, 5, 6]])>>> aarray([[1, 2, 3], [4, 5, 6]])>>> a.shape(2, 3)>>> a.dtypedtype('int32')>>> a.size6

Page 26: Migrating from matlab to python

Create Arrays

>>> b = np.arange(10)>>> barray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> c = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)>>> carray([[ 1., 2., 3.], [ 4., 5., 6.]], dtype=float32)

Page 27: Migrating from matlab to python

Create Arrays

>>> np.arange(16, dtype=np.float).reshape(4, 4)array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [ 12., 13., 14., 15.]])>>> np.zeros((3, 4))array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])

Page 28: Migrating from matlab to python

Create Arrays

>>> np.ones((3, 4))array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])>>> np.zeros((3, 4)) + 5array([[ 5., 5., 5., 5.], [ 5., 5., 5., 5.], [ 5., 5., 5., 5.]])

Page 29: Migrating from matlab to python

Data Types

• 21 data types

• signed and unsigned integer (8, 16, 32, 64 bit)

• floats (32, 64 an 96 bit)

• complex (64, 128, 192 bit)

• bool

• string

• character

• objects

Page 30: Migrating from matlab to python

Data Types

>>> a = np.array([1, 2, 3], dtype=np.int64)>>> aarray([1, 2, 3], dtype=int64)>>> a.dtypedtype('int64')>>> a.dtype.type<type 'numpy.int64'>>>> a.dtype.char'q'>>> a.dtype.str'<i8'

Page 31: Migrating from matlab to python

Slicing

>>> one_d = np.arange(10)>>> one_darray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> one_d[0]0>>> one_d[1]1>>> one_d[-1]9>>> one_d[2:5]array([2, 3, 4])

Page 32: Migrating from matlab to python

Slicing

>>> two_d = np.arange(25).reshape(5, 5)>>> two_darray([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])>>> two_d[0, 0]0>>> two_d[0]array([0, 1, 2, 3, 4])

Page 33: Migrating from matlab to python

Slicing

>>> two_d[:,0]array([ 0, 5, 10, 15, 20])>>> two_d[1:-1,1:-1]array([[ 6, 7, 8], [11, 12, 13], [16, 17, 18]])

• advanced selection (integers, booleans, strings)

• "loop-less" programming

Page 34: Migrating from matlab to python

Broadcasting

>>> two_darray([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])>>> two_d + 5array([[ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [25, 26, 27, 28, 29]])

Page 35: Migrating from matlab to python

Broadcasting

>>> a = np.arange(8).reshape(2, 4)>>> aarray([[0, 1, 2, 3], [4, 5, 6, 7]])>>> b = np.arange(4)>>> barray([0, 1, 2, 3])>>> a + barray([[ 0, 2, 4, 6], [ 4, 6, 8, 10]])

Page 36: Migrating from matlab to python

Universal Functions

>>> np.sin(a)array([[ 0. , 0.84147098, 0.90929743, 0.14112001], [-0.7568025 , -0.95892427, -0.2794155 , 0.6569866 ]])>>> np.sin.nin1>>> np.sin.nout1>>> np.sin.types['f->f', 'd->d', 'g->g', 'F->F', 'D->D', 'G->G', 'O->O']

Page 37: Migrating from matlab to python

Matrices

• always 2D

>>> M = np.matrix('1 2 3; 4 5 6')>>> Mmatrix([[1, 2, 3], [4, 5, 6]])>>> M.Imatrix([[-0.94444444, 0.44444444], [-0.11111111, 0.11111111], [ 0.72222222, -0.22222222]])

Page 38: Migrating from matlab to python

SciPy

• big package

• interpolation

• linear algebra

• FFT

• statistics

• optimization

• much more

Page 39: Migrating from matlab to python

IPython

• improved interactive shell

• object introspection

• system shell access

• command system for adding functionality

• allows interactive work with matplotlib ipython -pylab

Page 40: Migrating from matlab to python

Basic matplotlib-plotting

• simple, yet powerful plotting package

In [1]: plot(random(100))Out[1]: [<matplotlib.lines.Line2D object at 0x02A3EA90>]

Page 41: Migrating from matlab to python

Properties

• colors

• line styles

• markers

• legend

Page 42: Migrating from matlab to python

Properties

In [1]: x = arange(100)In [2]: linear = xIn [3]: square = arange(0, 10, 0.1) ** 2In [4]: lines = plot(x, linear, 'g:+', x, square, 'r--o')In [5]: leg = legend(('linear', 'square'), loc='upper left')

Page 43: Migrating from matlab to python

Properties

Page 44: Migrating from matlab to python

Ticks and Formatters

In [6]: major_locator = MultipleLocator(10)In [7]: major_formatter = FormatStrFormatter('%5.2f')In [8]: minor_locator = MultipleLocator(5)In [9]: ax = gca()In [10]: ax.xaxis.set_major_locator(major_locator)In [11]: ax.xaxis.set_minor_locator(minor_locator)In [12]: ax.xaxis.set_major_formatter(major_formatter)In [13]: draw()

Page 45: Migrating from matlab to python

Nice Plots

Page 46: Migrating from matlab to python

Nice Plots

Page 47: Migrating from matlab to python

Nice Plots

Page 48: Migrating from matlab to python

Pylab is no total replacement MATLAB (yet)

• Pylab = NumPy + SciPy + IPython + matplotlib

• no Simulink

• documentation, especially for SciPy not as comprehensive butgrowing

• no equivalent library for all toolboxes

• smaller user-base but growing

• array and matrix distinction can cause more verboseness

Page 49: Migrating from matlab to python

Pylab is no total replacement MATLAB (yet)

• same functionality sometimes duplicated

• finding the right library might take some time

• IDEs not as polished, less support for profiling

Page 50: Migrating from matlab to python

Python is no Island

• extensible in C/C++, Java, C#

• SWIG, SIP, Boost.Python

• .NET, COM

• Cython

• embedding

• access DLLS / shared libraries

Page 51: Migrating from matlab to python

C/C++ integration with Cython

• call C-functions

cdef extern from "math.h": double sin(double)

cdef double f(double x): return sin(x*x)

• works also with C++

Page 52: Migrating from matlab to python

Glue Language

• connect (heterogeneous) systems

• generate input data

• start and control processes

• read output data

• communicate over the network

• ...

• fast, few lines of code

Page 53: Migrating from matlab to python

Performance and Future Developments

• multi-cores

• multiprocessing (standard library)

• GPUs

• theano

• pyCUDA

• Clyther

Page 54: Migrating from matlab to python

More Scientific Libraries

• RPy /RPy2 (use the R language from Python)

• Scikits (times series and many others)

• pandas (time series and statistical analysis)

• SAGE (free open source alternative to Magma, Maple,Mathematica and Matlab.)

• PyPi - 766 packages for science and engineering

Page 55: Migrating from matlab to python

What else?

• standard library - batteries included

• Python Package Index: > 12.000 packages

• wrapper for GUI-Toolkits (wxPython, PyQt, Tkinter etc.)

• web frameworks (Django, Zope, TurboGears, Pylons etc.)

• access to all major database systems

• useful library for nearly all purposes

Page 56: Migrating from matlab to python

Thank you! Questions?

• Questions?

• Next Steps:

• Find out more about ActivePython Business Edition:http://www.activestate.com/activepython

• Download ActivePython:http://www.activestate.com/activepython/downloads

• Request Information:[email protected] or 1-866-510-2914

Page 57: Migrating from matlab to python

We've Got Your Universe Covered