0% found this document useful (0 votes)
4 views

python1(1)

The document serves as an introduction to scientific computing with Python, covering topics such as Python basics, numeric computing, and libraries like SciPy. It highlights Python's features, its applications in various fields, and provides examples of basic programming concepts including data types, lists, dictionaries, and control structures. The document is a resource for learning Python in the context of scientific computing.

Uploaded by

Douda Samih
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python1(1)

The document serves as an introduction to scientific computing with Python, covering topics such as Python basics, numeric computing, and libraries like SciPy. It highlights Python's features, its applications in various fields, and provides examples of basic programming concepts including data types, lists, dictionaries, and control structures. The document is a resource for learning Python in the context of scientific computing.

Uploaded by

Douda Samih
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 87

Introduction to

Scientific Computing
with Python
Adjusted from:
http://www.nanohub.org/resources/?id=99
Original Authors are: Eric Jones and Travis Oliphant

Many excellent resources on the web


>> google: "learn python"
some good example:
http://www.diveintopython.org/toc/index.html
http://www.scipy.org/Documentation
Topics

• Introduction to Python
• Numeric Computing
• SciPy and its libraries
What Is Python?
ONE LINER

Python is an interpreted programming language that


allows you to do almost anything possible with a
compiled language (C/C++/Fortran) without requiring
all the complexity.
PYTHON HIGHLIGHTS

• Automatic garbage • “Batteries


collection Included”
• Dynamic typing • Free
• Interpreted and • Portable
interactive • Easy to Learn and
• Object-oriented Use
Who is using Python?
NATIONAL SPACE TELESCOPE LAWRENCE LIVERMORE
LABORATORY NATIONAL LABORATORIES
Data processing and calibration Scripting and extending parallel
for instruments on the Hubble physics codes. pyMPI is their
Space Telescope. doing.
INDUSTRIAL LIGHT AND MAGIC WALT DISNEY
Digital Animation Digital animation development
environment.
PAINT SHOP PRO 8 REDHAT
Scripting Engine for JASC Anaconda, the Redhat Linux
PaintShop Pro 8 photo-editing installer program, is written in
software Python.
CONOCOPHILLIPS ENTHOUGHT
Oil exploration tool suite Geophysics and
Electromagnetics engine
scripting, algorithm
development, and visualization
Language Introduction
Interactive Calculator
# adding two values # real numbers
>>> 1 + 1 >>> b = 1.2 + 3.1
2 >>> b
# setting a variable 4.2999999999999998
>>> a = 1 >>> type(b)
>>> a <type 'float'>
1 # complex numbers
# checking a variables type >>> c = 2+1.5j
>>> type(a) >>> c
<type 'int'> (2+1.5j)
# an arbitrarily long integer
>>> a = 1203405503201 The four numeric types in Python on
32-bit architectures are:
>>> a integer (4 byte)
1203405503201L long integer (any precision)
>>> type(a) float (8 byte like C’s double)
complex (16 byte)
<type 'long'> The Numeric module, which we will
see later, supports a larger number
of numeric types.
Complex Numbers
CREATING COMPLEX NUMBERS EXTRACTING COMPONENTS
# Use "j" or "J" for imaginary # to extract real and im
# part. Create by "(real+imagj)", # component
# or "complex(real, imag)" . >>> a=1.5+0.5j
>>> 1j * 1J >>> a.real
(-1+0j) 1.5
>>> 1j * complex(0,1) >>> a.imag
(-1+0j) 0.5
>>> (1+2j)/(1+1j)
(1.5+0.5j)

ABSOLUTE VALUE
>>> a=1.5+0.5j
>>> abs(a)
1.5811388
Strings
CREATING STRINGS STRING LENGTH
# using double quotes >>> s = “12345”
>>> s = “hello world” >>> len(s)
>>> print s 5
hello world
# single quotes also work FORMAT STRINGS
>>> s = ‘hello world’ # the % operator allows you
>>> print s # to supply values to a
hello world # format string. The format
# string follows
STRING OPERATIONS # C conventions.
# concatenating two strings >>> s = “some numbers:”
>>> “hello “ + “world” >>> x = 1.34
‘hello world’ >>> y = 2
>>> s = “%s %f, %d” % (s,x,y)
# repeating a string >>> print s
>>> “hello “ * 3 some numbers: 1.34, 2
‘hello hello hello ’
The string module
>>> import string # replacing text in a string
>>> s = “hello world” >>> string.replace(s,’world’ \
... ,’Mars’)
# split space delimited words ‘hello Mars’
>>> wrd_lst = string.split(s)
>>> print wrd_lst # python2.2 and higher
[‘hello’, ‘world’] >>> s.replace(’world’ ,’Mars’)
‘hello Mars’
# python2.2 and higher
>>> s.split() # strip whitespace from string
[‘hello’, ‘world’] >>> s = “\t hello \n”
>>> string.strip(s)
# join words back together ‘hello’
>>> string.join(wrd_lst)
hello world # python2.2 and higher
>>> s.strip()
# python2.2 and higher ‘hello’
>>> ‘ ‘.join(wrd_lst)
hello world
Multi-line Strings

# triple quotes are used # including the new line


# for mutli-line strings >>> a = “hello\n” \
>>> a = ”””hello ... “world”
... world””” >>> print a
>>> print a hello
hello world
world

# multi-line strings using


# “\” to indicate
continuation
>>> a = “hello ” \
... “world”
>>> print a
hello world
List objects
LIST CREATION WITH BRACKETS range( start, stop, step)
>>> l = [10,11,12,13,14] # the range method is helpful
>>> print l # for creating a sequence
[10, 11, 12, 13, 14] >>> range(5)
[0, 1, 2, 3, 4]
CONCATENATING LIST
>>> range(2,7)
# simply use the + operator
[2, 3, 4, 5, 6]
>>> [10, 11] + [12,13]
[10, 11, 12, 13]
>>> range(2,7,2)
[2, 4, 6]
REPEATING ELEMENTS IN LISTS
# the multiply operator
# does the trick.
>>> [10, 11] * 3
[10, 11, 10, 11, 10, 11]
Indexing
RETREIVING AN ELEMENT NEGATIVE INDICES
# list # negative indices count
# indices: 0 1 2 3 4 # backward from the end of
>>> l = [10,11,12,13,14] # the list.
>>> l[0] #
10 # indices: -5 -4 -3 -2 -1
>>> l = [10,11,12,13,14]
SETTING AN ELEMENT
>>> l[1] = 21 >>> l[-1]
>>> print l 14
[10, 21, 12, 13, 14] >>> l[-2]
13
OUT OF BOUNDS
>>> l[10] The first element in an
Traceback (innermost last):
File "<interactive input>",line 1,in ? array has index=0 as
IndexError: list index out of range in C. Take note Fortran
programmers!
More on list objects
LIST CONTAINING MULTIPLE LENGTH OF A LIST
TYPES
>>> len(l)
# list containing integer, 3
# string, and another list.
>>> l = [10,’eleven’,[12,13]] DELETING OBJECT FROM LIST
>>> l[1]
# use the del keyword
‘eleven’
>>> del l[2]
>>> l[2]
>>> l
[12, 13]
[10,’eleven’]
# use multiple indices to DOES THE LIST CONTAIN x ?
# retrieve elements from
# use in or not in
# nested lists.
>>> l = [10,11,12,13,14]
>>> l[2][0]
>>> 13 in l
12
1
>>> 13 not in l
0
Slicing
var[lower:upper]
Slices extract a portion of a sequence by specifying a
lower and upper bound. The extracted elements start
at lower and go up to, but do not include, the upper
element.
SLICING LISTSMathematically the OMITTING
range isINDICES
[lower,upper).
# indices: 0 1 2 3 4 # omitted boundaries are
>>> l = [10,11,12,13,14] # assumed to be the beginning
# [10,11,12,13,14] # (or end) of the list.
>>> l[1:3]
[11, 12] # grab first three elements
>>> l[:3]
# negative indices work also [10,11,12]
>>> l[1:-2] # grab last two elements
[11, 12] >>> l[-2:]
>>> l[-4:3] [13,14]
[11, 12]
A few methods for list objects
some_list.append( x ) some_list.remove( x )

Add the element x to the Delete the first


end occurrence of x from the
of the list, some_list. list.
some_list.count( x ) some_list.reverse( )

Count the number of Reverse the order of


times x elements in the list.
occurs in the list.
some_list.index( x ) some_list.sort( cmp )

Return the index of the By default, sort the


first elements in ascending
occurrence of x in the list. order. If a compare
function is given, use it to
sort
List methods in action
>>> l = [10,21,23,11,24]

# add an element to the list # remove the first 11


>>> l.append(11) >>> l.remove(11)
>>> print l >>> print l
[10,21,23,11,24,11] [10,21,23,24,11]

# how many 11s are there? # sort the list


>>> l.count(11) >>> l.sort()
2 >>> print l
[10,11,21,23,24]

# where does 11 first occur? # reverse the list


>>> l.index(11) >>> l.reverse()
3 >>> print l
[24,23,21,11,10]
Mutable vs. Immutable

MUTABLE OBJECTS IMMUTABLE OBJECTS


# Mutable objects, such as # Immutable objects, such as
# lists, can be changed # strings, cannot be changed
# in-place. # in-place.

# insert new values into list # try inserting values into


>>> l = [10,11,12,13,14] # a string
>>> l[1:3] = [5,6] >>> s = ‘abcde’
>>> print l >>> s[1:3] = ‘xy’
[10, 5, 6, 13, 14] Traceback (innermost last):
File "<interactive input>",line 1,in ?
TypeError: object doesn't support
The cStringIO module treats slice assignment
strings like a file buffer
and allows insertions. It’s # here’s how to do it
useful when working with >>> s = s[:1] + ‘xy’ + s[3:]
large strings or when speed >>> print s
is paramount. 'axyde'
Dictionaries
Dictionaries store key/value pairs. Indexing a
dictionary by a key returns the value associated with
it.
DICTIONARY EXAMPLE
# create an empty dictionary using curly brackets
>>> record = {}
>>> record[‘first’] = ‘Jmes’
>>> record[‘last’] = ‘Maxwell’
>>> record[‘born’] = 1831
>>> print record
{'first': 'Jmes', 'born': 1831, 'last': 'Maxwell'}
# create another dictionary with initial entries
>>> new_record = {‘first’: ‘James’, ‘middle’:‘Clerk’}
# now update the first dictionary with values from the new one
>>> record.update(new_record)
>>> print record
{'first': 'James', 'middle': 'Clerk', 'last':'Maxwell', 'born':
1831}
A few dictionary methods
some_dict.clear( ) some_dict.keys( )

Remove all key/value Return a list of all the


pairs from keys in the
the dictionary, dictionary.
some_dict.
some_dict.copy( ) some_dict.values( )

Create a copy of the Return a list of all the


dictionary values in the dictionary.

some_dict.has_key( x ) some_dict.items( )

Test whether the Return a list of all the


dictionary contains the key/value pairs in the
key x. dictionary.
Dictionary methods in action
>>> d = {‘cows’: 1,’dogs’:5,
... ‘cats’: 3}

# create a copy. # get a list of all values


>>> dd = d.copy() >>> d.values()
>>> print dd [3, 5, 1]
{'dogs':5,'cats':3,'cows': 1}

# test for chickens. # return the key/value pairs


>>> d.has_key(‘chickens’) >>> d.items()
0 [('cats', 3), ('dogs', 5),
('cows', 1)]

# get a list of all keys # clear the dictionary


>>> d.keys() >>> d.clear()
[‘cats’,’dogs’,’cows’] >>> print d
{}
Tuples
Tuples are a sequence of objects just like lists.
Unlike lists, tuples are immutable objects. While
there are some functions
and statements that require tuples, they are rare. A
good rule of thumb is to use lists whenever you need
TUPLE EXAMPLE
a generic sequence.
# tuples are built from a comma separated list enclosed by ( )
>>> t = (1,’two’)
>>> print t
(1,‘two’)
>>> t[0]
1
# assignments to tuples fail
>>> t[0] = 2
Traceback (innermost last):
File "<interactive input>", line 1, in ?
TypeError: object doesn't support item assignment
Assignment
Assignment creates object
references.
>>> x = [0, 1, 2] x 0 1 2
# y = x cause x and y to point
# at the same list
>>> y = x
y
# changes to y also change x x
>>> y[1] = 6 0 6 2
>>> print x y
[0, 6, 2]

# re-assigning y to a new list x 0 6 2


# decouples the two lists
>>> y = [3, 4] y 3 4
Multiple assignments
# creating a tuple without () # multiple assignments from a
>>> d = 1,2,3 # tuple
>>> d >>> a,b,c = d
(1, 2, 3) >>> print b
2

# also works for lists


# multiple assignments >>> a,b,c = [1,2,3]
>>> a,b,c = 1,2,3 >>> print b
>>> print b 2
2
If statements

if/elif/else provide conditional


execution of code blocks.

IF STATEMENT FORMAT IF EXAMPLE


# a simple if statement
if <condition>: >>> x = 10
<statements> >>> if x > 0:
... print 1
elif <condition>: ... elif x == 0:
<statements> ... print 0
else: ... else:
... print –1
<statements> ... < hit return >
1
Test Values
• True means any non-zero number
or non-empty object
• False means not true: zero, empty
object, or None
EMPTY OBJECTS

# empty objects evaluate false


>>> x = []
>>> if x:
... print 1
... else:
... print 0
... < hit return >
0
For loops
For loops iterate over a sequence
of for
objects.
<loop_var> in <sequence>:
<statements>

TYPICAL SCENARIO LOOPING OVER A LIST


>>> for i in range(5): >>> l=[‘dogs’,’cats’,’bears’]
... print i, >>> accum = ‘’
... < hit return > >>> for item in l:
0 1 2 3 4 ... accum = accum + item
... accum = accum + ‘ ‘
LOOPING OVER A STRING
... < hit return >
>>> for i in ‘abcde’: >>> print accum
... print i, dogs cats bears
... < hit return >
a b c d e
While loops
While loops iterate until a
conditionwhile
is met.
<condition>:
<statements>

WHILE LOOP BREAKING OUT OF A LOOP


# the condition tested is # breaking from an infinite
# whether lst is empty. # loop.
>>> lst = range(3) >>> i = 0
>>> while lst: >>> while 1:
... print lst ... if i < 3:
... lst = lst[1:] ... print i,
... < hit return > ... else:
[0, 1, 2] ... break
[1, 2] ... i = i + 1
[2] ... < hit return >
0 1 2
Anatomy of a function

The keyword def Function arguments are listed


indicates the start separated by commas. They are passed
of a function. by assignment. More on this later.

def add(arg0, arg1):


Indentation is
used to indicate
a = arg0 + arg1
the contents of return a A colon ( : ) terminates
the function. It the function definition.
is not optional,
but a part of the
syntax. An optional return statement specifies
the value returned from the function. If
return is omitted, the function returns the
special value None.
Our new function in action
# We’ll create our function # how about strings?
# on the fly in the >>> x = ‘foo’
# interpreter. >>> y = ‘bar’
>>> def add(x,y): >>> add(x,y)
... a = x + y ‘foobar’
... return a

# test it out with numbers # functions can be assigned


>>> x = 2 # to variables
>>> y = 3 >>> func = add
>>> add(x,y) >>> func(x,y)
5 ‘foobar’

# how about numbers and strings?


>>> add(‘abc',1)
Traceback (innermost last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in add
TypeError: cannot add type "int" to string
More about functions
# Every function returns # FUNCTIONAL PROGRAMMING:
# a value (or NONE) # "map(function, sequence)"
# but you don't need to >>> def cube(x): return
# specify returned type! x*x*x ...
>>> map(cube, range(1, 6))
# Function documentation [1, 8, 27, 64, 125]
>>> def add(x,y):
... """this function # "reduce (function,
... adds two numbers""" sequence)"
... a = x + y >>> def add(x,y): return x+y
... return a ...
>>> reduce(add, range(1, 11))
# You can always retrieve 55
# function documentation # "filter (function,
>>> print add.__doc__ sequence)"
>>> def f(x): return x % 2 !=
this function 0
adds two numbers ...
>>> filter(f, range(2, 10))
Even more on functions
# Lambda function:
# buld-in function "dir" is
# Python supports one-line mini-
# used to list all
# functions on the fly.
# definitions in a module
# Borrowed from Lisp, lambda
>>> import scipy
# functions can be used anywhere
>>> dir(scipy)
# a function is required.
.......................
>>> def f(x): return x*x
...<a lot of stuf>...
>>> f(3)
.......................
9
>> g = lambda x:x*x
>> g(3)
9
# more on lambda function:
>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>> print reduce(lambda x, y: x + y, foo)
139
Modules
EX1.PY FROM SHELL

# ex1.py [ej@bull ej]$ python ex1.py


6, 3.1416
PI = 3.1416 FROM INTERPRETER

def sum(lst): # load and execute the module


tot = lst[0] >>> import ex1
for value in lst[1:]: 6, 3.1416
tot = tot + value # get/set a module variable.
return tot >>> ex1.PI
3.1415999999999999
l = [0,1,2,3] >>> ex1.PI = 3.14159
print sum(l), PI >>> ex1.PI
3.1415899999999999
# call a module variable.
>>> t = [2,3,4]
>>> ex1.sum(t)
9
Modules cont.
INTERPRETER EDITED EX1.PY

# load and execute the module # ex1.py version 2


>>> import ex1
6, 3.1416 PI = 3.14159
< edit file >
# import module again def sum(lst):
>>> import ex1 tot = 0
# nothing happens!!! for value in lst:
tot = tot + value
return tot
# use reload to force a
# previously imported library l = [0,1,2,3,4]
# to be reloaded. print sum(l), PI
>>> reload(ex1)
10, 3.14159
Modules cont. 2
Modules can be executable scripts or libraries
or both.
EX2.PY EX2.PY CONTINUED
“ An example module “ def add(x,y):
” Add two values.”
PI = 3.1416 a = x + y
return a
def sum(lst):
””” Sum the values in a def test():
list. l = [0,1,2,3]
””” assert( sum(l) == 6)
tot = 0 print ‘test passed’
for value in lst:
tot = tot + value # this code runs only if this
return tot # module is the main program
if __name__ == ‘__main__’:
test()
Classes
SIMPLE PARTICLE CLASS
>>> class particle:
... # Constructor method
... def __init__(self,mass, velocity):
... # assign attribute values of new object
... self.mass = mass
... self.velocity = velocity
... # method for calculating object momentum
... def momentum(self):
... return self.mass * self.velocity
... # a “magic” method defines object’s string representation
... def __repr__(self):
... msg = "(m:%2.1f, v:%2.1f)" % (self.mass,self.velocity)
... return msg
EXAMPLE
>>> a = particle(3.2,4.1)
>>> a
(m:3.2, v:4.1)
>>> a.momentum()
13.119999999999999
Reading files
FILE INPUT EXAMPLE PRINTING THE RESULTS
>>> results = [] >>> for i in results: print i
>>> f = open(‘c:\\rcs.txt’,’r’) [100.0, -20.30…, -31.20…]
[200.0, -22.70…, -33.60…]
# read lines and discard header
>>> lines = f.readlines()[1:]
>>> f.close()
EXAMPLE FILE: RCS.TXT
>>> for l in lines: #freq (MHz) vv (dB) hh (dB)
... # split line into fields
100 -20.3 -31.2
... fields = line.split()
... # convert text to numbers
200 -22.7 -33.6
... freq = float(fields[0])
... vv = float(fields[1])
... hh = float(fields[2])
... # group & append to results
... all = [freq,vv,hh]
... results.append(all)
... < hit return >
More compact version
ITERATING ON A FILE AND LIST COMPREHENSIONS
>>> results = []
>>> f = open(‘c:\\rcs.txt’,’r’)
>>> f.readline()
‘#freq (MHz) vv (dB) hh (dB)\n'
>>> for l in f:
... all = [float(val) for val in l.split()]
... results.append(all)
... < hit return >
>>> for i in results:
... print i
... < hit return >

EXAMPLE FILE: RCS.TXT


#freq (MHz) vv (dB) hh (dB)
100 -20.3 -31.2
200 -22.7 -33.6
Same thing, one line
OBFUSCATED PYTHON CONTEST…
>>> print [[float(val) for val in l.split()] for
... l in open("c:\\temp\\rcs.txt","r")
... if l[0] !="#"]

EXAMPLE FILE: RCS.TXT


#freq (MHz) vv (dB) hh (dB)
100 -20.3 -31.2
200 -22.7 -33.6
Sorting
THE CMP METHOD CUSTOM CMP METHODS

# The builtin cmp(x,y) # define a custom sorting


# function compares two # function to reverse the
# elements and returns # sort ordering
# -1, 0, 1 >>> def descending(x,y):
# x < y --> -1 ... return -cmp(x,y)
# x == y --> 0
# x > y --> 1
>>> cmp(0,1)
-1

# By default, sorting uses # Try it out


# the builtin cmp() method >>> x.sort(descending)
>>> x = [1,4,2,3,0] >>> x
>>> x.sort() [4, 3, 2, 1, 0]
>>> x
[0, 1, 2, 3, 4]
Sorting
SORTING CLASS INSTANCES

# Comparison functions for a variety of particle values


>>> def by_mass(x,y):
... return cmp(x.mass,y.mass)
>>> def by_velocity(x,y):
... return cmp(x.velocity,y.velocity)
>>> def by_momentum(x,y):
... return cmp(x.momentum(),y.momentum())

# Sorting particles in a list by their various properties


>>> x = [particle(1.2,3.4),particle(2.1,2.3),particle(4.6,.7)]
>>> x.sort(by_mass)
>>> x
[(m:1.2, v:3.4), (m:2.1, v:2.3), (m:4.6, v:0.7)]
>>> x.sort(by_velocity)
>>> x
[(m:4.6, v:0.7), (m:2.1, v:2.3), (m:1.2, v:3.4)]
>>> x.sort(by_momentum)
>>> x
[(m:4.6, v:0.7), (m:1.2, v:3.4), (m:2.1, v:2.3)]
Criticism of Python
FUNCTION ARGUMENTS
# All function arguments are called by reference. Changing data in
# subroutine effects global data!
>>> def sum(lst):
... tot=0
... for i in range(0,len(lst)):
... lst[i]+=1
... tot += lst[i]
... return tot
>>> a=range(1,4)
>>> sum(a)
9
>>> a
[2,3,4]
# Can be fixed by
>>> a=range(1,4)
>>> a_copy = a[:] # be careful: a_copy = a would not work
>>> sum(a_copy)
9
>>> a
[1,2,3]
Criticism of Python
FUNCTION ARGUMENTS

Python does not support something like "const" in C+


+. If users checks function declaration, it has no clue
which arguments are meant as input (unchanged on
exit) and
COPYING which are output
DATA
User has "no direct contact" with data structures.
User might not be aware of data handling. Python is
optimized for speed -> references.
>>> a=[1,2,3,[4,5]] # Can be fixed by
>>> b=a[:] >>> import copy
>>> a[0]=2 >>> a=[1,2,3,[4,5]]
>>> b >>> b = copy.deepcopy(a)
[1,2,3,[4,5]] >>> a[3][0]=0
>>> a[3][0]=0 >>> b
>>> b [1,2,3,[4,5]]
[1,2,3,[0,5]]
Criticism of Python
CLASS DATA

In C++ class declaration uncovers all important


information about the class - class members (data and
methods). In Python, data comes into
existence when used. User needs to read
implementation of the class (much more code) to find
class data
RELODING and understand the logic of the class.
MODULES
This is particularly important in large scale codes.
If you import a module in command-line interpreter, but
the module was later changed on disc, you can reload
the module by typing
reload modulexxx
This reloads the particular modulexxx, but does not
recursively reload modules that might also be changed
NumPy
NumPy and SciPy
In 2005 Numarray and Numeric were merged into common
project called "NumPy". On top of it, SciPy was build
recently and spread very fast in scientific community.
Home: http://www.scipy.org/SciPy

IMPORT NUMPY AND SCIPY

>>> from numpy import *


>>> import numpy
>>> numpy.__version__
’1.0.1’
or better
>>> from scipy import *
>>> import scipy
>>> scipty.__version__
'0.5.2'
Array Operations
SIMPLE ARRAY MATH MATH FUNCTIONS
>>> a = array([1,2,3,4]) # Create array from 0 to 10
>>> b = array([2,3,4,5]) >>> x = arange(11.)
>>> a + b
array([3, 5, 7, 9]) # multiply entire array by
# scalar value
>>> a = (2*pi)/10.
>>> a
0.628318530718
>>> a*x
array([ 0.,0.628,…,6.283])

# apply functions to array.


>>> y = sin(a*x)
NumPy defines the following
constants:
pi = 3.14159265359
e = 2.71828182846
Introducing Numeric Arrays
SIMPLE ARRAY CREATION ARRAY SHAPE
>>> a = array([0,1,2,3]) >>> a.shape
>>> a (4,)
array([0, 1, 2, 3]) >>> shape(a)
(4,)

CHECKING THE TYPE CONVERT TO PYTHON LIST


>>> type(a) >>> a.tolist()
<type 'array'> [0, 1, 2, 3]

NUMERIC TYPE OF ELEMENTS ARRAY INDEXING


>>> a.typecode() >>> a[0]
'l‘ # ‘l’ = Int 0
>>> a[0] = 10
BYTES IN AN ARRAY ELEMENT >>> a
[10, 1, 2, 3]
>>>
a.itemsize()
4
Multi-Dimensional Arrays
MULTI-DIMENSIONAL ARRAYS ADDRESS FIRST ROW USING
>>> a = array([[ 0, 1, 2, 3], SINGLE INDEX
[10,11,12,13]]) >>> a[1]
>>> a array([10, 11, 12, 13])
array([[ 0, 1, 2, 3],
[10,11,12,13]]) FLATTEN TO 1D ARRAY
(ROWS,COLUMNS) >>> a.flat
>>> shape(a) array(0,1,2,3,10,11,12,-1)
(2, 4) >>> ravel(a)
array(0,1,2,3,10,11,12,-1)
GET/SET ELEMENTS
>>> a[1,3] A.FLAT AND RAVEL()
13 column REFERENCE ORIGINAL
row MEMORY
>>> a[1,3] = -1 >>> a.flat[5] = -2
>>> a >>> a
array([[ 0, 1, 2, 3], array([[ 0, 1, 2, 3],
[10,11,12,-1]]) [10,-2,12,-1]])
Array Slicing
SLICING WORKS MUCH LIKE
STANDARD PYTHON SLICING
>>> a[0,3:5]
array([3, 4])

>>> a[4:,4:] 0 1 2 3 4 5
array([[44, 45],
10 11 12 13 14 15
[54, 55]])
20 21 22 23 24 25
>>> a[:,2]
array([2,12,22,32,42,52]) 30 31 32 33 34 35

STRIDES ARE ALSO POSSIBLE 40 41 42 43 44 45

>>> a[2::2,::2] 50 51 52 53 54 55
array([[20, 22, 24],
[40, 42, 44]])
Slices Are References
Slices are references to memory in original array. Changing
values in a slice also changes the original array.

>>> a = array([0,1,2])

# create a slice containing only the


# last element of a
>>> b = a[2:3]
>>> b[0] = 10

# changing b changed a!
>>> a
array([ 1, 2, 10])
Array Constructor
array(sequence, typecode=None, copy=1, savespace=0)

sequence - any type of Python sequence. Nested list create multi-


dimensional arrays.

typecode - character (string). Specifies the numerical type of the array.


If it is None, the constructor makes its best guess at the numeric type.

copy - if copy=0 and sequence is an array object, the returned


array is a reference that data. Otherwise, a copy of the data in sequence
is made.

savespace - Forces Numeric to use the smallest possible numeric type for
the array. Also, it prevents upcasting to a different type during math
operations with scalars. (see coercion section for more details)
Array Constructor Examples
FLOATING POINT ARRAYS USE TYPECODE TO REDUCE
DEFAULT TO DOUBLE PRECISION
PRECISION
>>> a = array([0,1.,2,3]) >>> a = array([0,1.,2,3],'f')
>>> a.dtype() >>> a.dtype()
‘d‘ 'f‘
notice decimal
>>> len(a.flat)*a.itemsize()
16

BYTES FOR MAIN ARRAY ARRAYS REFERENCING SAME


STORAGE DATA
# flat assures that
# multidimensional arrays >>> a = array([1,2,3,4])
# work >>> b = array(a,copy=0)
>>>len(a.flat)*a.itemsize >>> b[1] = 10
32 >>> a
array([ 1, 10, 3, 4])
32-bit Typecodes
Character Bits (Bytes) Identifier
D 128 (16) Complex, Complex64
F 64 (8) Complex0, Complex8, Complex16, Complex32

d 64 (8) Float, Float64


f 32 (4) Float0, Float8, Float16, Float32
l 32 (4) Int
i 32 (4) Int32
s 16 (2) Int16
1 (one) 8 (1) Int8
u 32 (4) UnsignedInt32
w 16 (2) UnsignedInt16
b 8 (1) UnsignedInt8
O 4 (1) PyObject

Highlighted typecodes correspond to Python’s standard Numeric types.


Array Creation Functions
arange(start,stop=None,step=1,typecode=None)
Nearly identical to Python’s range(). Creates an array of values in the
range [start,stop) with the specified step value. Allows non-integer
values for start, stop, and step. When not specified, typecode is
derived from the start, stop, and step values.
>>> arange(0,2*pi,pi/4)
array([ 0.000, 0.785, 1.571, 2.356, 3.142,
3.927, 4.712, 5.497])

ones(shape,typecode=None,savespace=0)
zeros(shape,typecode=None,savespace=0)
shape is a number or sequence specifying the dimensions of the array. If
typecode is not specified, it defaults to Int.
>>> ones((2,3),typecode=Float32)
array([[ 1., 1., 1.],
[ 1., 1., 1.]],'f')
Array Creation Functions
(cont.)
identity(n,typecode=‘l’)
Generates an n by n identity matrix with typecode = Int.

>>> identity(4)
array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
>>> identity(4,’f’)
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]],'f')
Mathematic Binary Operators
a + b  add(a,b) a * b  multiply(a,b)
a - b  subtract(a,b) a / b  divide(a,b)
a % b  remainder(a,b) a ** b  power(a,b)

MULTIPLY BY A SCALAR ADDITION USING AN OPERATOR


FUNCTION
>>> a = array((1,2))
>>> a*3. >>> add(a,b)
array([3., 6.]) array([4, 6])

ELEMENT BY ELEMENT IN PLACE OPERATION


ADDITION
# Overwrite contents of a.
>>> a = array([1,2])
# Saves array creation
>>> b = array([3,4])
# overhead
>>> a + b
>>> add(a,b,a) # a += b
array([4, 6])
array([4, 6])
>>> a
array([4, 6])
Comparison and Logical
Operators
equal (==) not_equal (!=) greater (>)
greater_equal (>=) less (<) less_equal (<=)
logical_and (and) logical_or (or) logical_xor
logical_not (not)
2D EXAMPLE

>>> a = array(((1,2,3,4),(2,3,4,5)))
>>> b = array(((1,2,5,4),(1,3,4,5)))
>>> a == b
array([[1, 1, 0, 1],
[0, 1, 1, 1]])
# functional equivalent
>>> equal(a,b)
array([[1, 1, 0, 1],
[0, 1, 1, 1]])
Bitwise Operators
bitwise_and (&) invert (~) right_shift(a,shifts)
bitwise_or (|) bitwise_xor left_shift (a,shifts)
BITWISE EXAMPLES
>>> a = array((1,2,4,8))
>>> b = array((16,32,64,128))
>>> bitwise_and(a,b)
array([ 17, 34, 68, 136])

# bit inversion
>>> a = array((1,2,3,4),UnsignedInt8)
>>> invert(a)
array([254, 253, 252, 251],'b')
Changed from
# surprising type conversion
UnsignedInt8
>>> left_shift(a,3)
to Int32
array([ 8, 16, 24, 32],'i')
Trig and Other Functions
TRIGONOMETRIC OTHERS
sin(x) sinh(x) exp(x) log(x)
cos(x) cosh(x) log10(x) sqrt(x)
arccos(x) arccosh(x) absolute(x) conjugate(x)
negative(x) ceil(x)
arctan(x) arctanh(x) floor(x) fabs(x)
arcsin(x) arcsinh(x) hypot(x,y) fmod(x,y)
arctan2(x,y) maximum(x,y) minimum(x,y)
hypot(x,y)
Element by element distance
calculation using x2  y2
SciPy
Overview
CURRENT PACKAGES

•Special Functions •Input/Output (scipy.io)


(scipy.special) •Genetic Algorithms
•Signal Processing (scipy.signal) (scipy.ga)
•Fourier Transforms •Statistics (scipy.stats)
(scipy.fftpack) •Distributed Computing
•Optimization (scipy.optimize) (scipy.cow)
•General plotting (scipy.[plt, •Fast Execution (weave)
xplt, gplt]) •Clustering Algorithms
•Numerical Integration (scipy.cluster)
(scipy.integrate) •Sparse Matrices*
•Linear Algebra (scipy.linalg) (scipy.sparse)
Basic Environment
CONVENIENCE FUNCTIONS
info help system for scipy
>>> info(linspace)
similar to dir for the rest of python
linspace(start, stop, num=50, endpoint=1, retstep=0)

Evenly spaced samples.

Return num evenly spaced samples from start to stop. If endpoint=1


then
last sample is stop. If retstep is 1 then return the step value used.

>>> linspace(-1,1,5) linspace get equally spaced


points.
array([-1. , -0.5, 0. , 0.5, 1. ])
r_[] also does this (shorthand)
>>> r_[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])

>>> logspace(0,3,4) logspace get equally spaced


array([ 1., 10., 100., 1000.]) points in log10 domain
>>> info(logspace)
logspace(start, stop, num=50, endpoint=1)

Evenly spaced samples on a logarithmic scale.

Return num evenly spaced samples from 10**start to 10**stop. If


endpoint=1 then last sample is 10**stop.
Basic Environment
CONVENIENT MATRIX GENERATION AND MANIPULATION

>>> A = mat(‘1,2,4;4,5,6;7,8,9’) Simple creation of matrix


with “;” meaning row
>>> A=mat([[1,2,4],[4,5,6],[7,8,9]])
separation
>>> print A
Matrix([[1, 2, 4],
[2, 5, 3],
[7, 8, 9]])
Matrix Power

>>> print A**4


Matrix([[ 6497, 9580, 9836],
[ 7138, 10561, 10818], Matrix Multiplication and
[18434, 27220, 27945]]) Matrix Inverse
>>> print A*A.I
Matrix([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])

>>> print A.T Matrix Transpose


Matrix([[1, 2, 7],
[2, 5, 8],
[4, 3, 9]])
More Basic Functions
TYPE HANDLING OTHER USEFUL FUNCTIONS

iscomplexobj real_if_close isnan select unwrap roots


iscomplex isscalar nan_to_num extract sort_complex poly
isrealobj isneginf common_type insert trim_zeros any
isreal isposinf cast fix fliplr all
imag isinf typename mod flipud disp
real isfinite amax rot90 unique
amin eye extract
SHAPE MANIPULATION
ptp diag insert

squeeze vstack split sum factorial nansum

atleast_1d hstack hsplit cumsum factorial2 nanmax

atleast_2d column_stack vsplit prod comb nanargmax

atleast_3d dstack dsplit cumprod pade nanargmin

apply_over_ expand_dims apply_along_ diff derivative nanmin


axes axis angle limits.XXXX
Input and Output
scipy.io --- Reading and writing ASCII files

textfile.txt

Student Test1 Test2 Test3 Test4

Jane 98.3 94.2 95.3 91.3


Jon 47.2 49.1 54.2 34.7
Jim 84.2 85.3 94.1 76.4
Read from column 1 to the end
Read from line 3 to the end
>>> a = io.read_array(‘textfile.txt’,columns=(1,-1),lines=(3,-1))
>>> print a
[[ 98.3 94.2 95.3 91.3]
[ 47.2 49.1 54.2 34.7]
[ 84.2 85.3 94.1 76.4]]
>>> b = io.read_array(‘textfile.txt’,columns=(1,-2),lines=(3,-2))
>>> print b
[[ 98.3 95.3] Read from column 1 to the end every second column
[ 84.2 94.1]]
Read from line 3 to the end every second line
Input and Output
scipy.io --- Reading and writing raw binary files

fid = fopen(file_name, permission='rb', format='n')

Class for reading and writing binary files into Numeric arrays.
Methods
•file_name The complete path name to read read data from file and return
the file to open. Numeric array
•permission Open the file with given write write to file from Numeric array
permissions: ('r', 'w', 'a') fort_read read Fortran-formatted binary data
for reading, writing, or from the file.
appending. This is the same fort_write write Fortran-formatted binary data
as the mode argument in the to the file.
builtin open command. rewind rewind to beginning of file
•format The byte-ordering of the file: size get size of file
(['native', 'n'], ['ieee-le', 'l'], seek seek to some position in the file
['ieee-be', 'b']) for native, little- tell return current position in file
endian, or big-endian. close close the file
Few examples

Examples of SciPy use


Integration
x
uppose we want to integrate Bessel function
dtJ (t ) / t
0
1

>>> info(integrate)
.....<documentation of integrate module>.....
>>> integrate.quad(lambda t:
special.j1(t)/t,0,pi)
(1.062910971494,1.18e-14)
j1int.py module:
from scipy import *
def fun(x):
return integrate.quad(lambda t: special.j1(t)/t,0,x)

x=r_[0:30:0.01]
for tx in x:
print tx, fun(tx)[0]
Minimization
(x 
uppose we want to minimize the function a ) 2  ( y  b) 2 min
>>> from scipy import *
>>> import scipy
>>> info(scipy)
.... <documentation of all available modules>
>>> info(optimize)
>>> info(optimize.fmin_powell)

>>> def func((x,y),(a,b)): return (x-a)**2+(y-b)**2


Starting guess
>>> optimize.fmin_powell(func, (0,0), ((5,6),))
Opimization terminated successfully,
Current function value: 0.00000
Iterations: 2 additional arguments
Function evaluations: 38
array([5.,6.])
Root finding and integration
x

dtJ (t ) / t
0
1

1.0

0.8

0.6

0.4
x

dtJ (t ) / t 1
0.2

The function
1 0.0
0 5 10 15 20 25
0

has many solutions. Suppose we want to find all solution in the range [0:100]
Put it all together
from scipy import *
"""
Finds all solutions of the equation Integrate[j1(t)/t,{t,0,x}] == 1
in the range x=[0,100]
"""

def func(x,a):
" Computes Integrate[j1(t)/t,{t,0,x}] - a"
return integrate.quad(lambda t: special.j1(t)/t, 0, x)[0] - a

# Finds approxiate solutions of the equation in the range [0:100]


x = r_[0:100:0.2] # creates an equaly spaced array
b = map(lambda t: func(t,1), x) # evaluates function on this array

z = []; # approximate solutions of the equation


for i in range(1,len(b)): # if the function changes sign,
if (b[i-1]*b[i]<0): z.append(x[i]) # the solution is bracketed

print "Zeros of the equation in the interval [0:100] are"


j=0
for zt in z:
print j, optimize.fsolve(func,zt,(1,)) # calling root finding
routine, finds all zeros.
j+=1
It takes around 2 seconds to
get Zeros of the equation in the interval [0:100] are
0 2.65748482457
1 5.67254740317
2 8.75990144967
3 11.872242395
4 14.9957675329
5 18.1251662422
6 21.2580027553
7 24.3930147628
8 27.5294866728
9 30.666984016
10 33.8052283484
11 36.9440332549
12 40.0832693606
13 43.2228441315
14 46.362689668
15 49.5027550388
16 52.6430013038
17 55.7833981883
18 58.9239218038
19 62.0645530515
20 65.2052764808
21 68.3460794592
22 71.4869515584
23 74.6278840946
24 77.7688697786
25 80.9099024466
26 84.0509768519
27 87.1920884999
28 90.3332335188
29 93.4744085549
30 96.615610689
31 99.7568373684
Linear Algebra
scipy.linalg --- FAST LINEAR ALGEBRA

•Uses ATLAS if available --- very fast


•Low-level access to BLAS and LAPACK routines in
modules linalg.fblas, and linalg.flapack (FORTRAN order)
•High level matrix routines
•Linear Algebra Basics: inv, solve, det, norm, lstsq, pinv
•Decompositions: eig, lu, svd, orth, cholesky, qr, schur
•Matrix Functions: expm, logm, sqrtm, cosm, coshm, funm (general
matrix functions)
Some simple examples
>>> A=matrix(random.rand(5,5)) # creates random matrix
>>> A.I
<inverse of the random matrix>
>>> linalg.det(A)
<determinant of the matrix>
>>> linalg.eigvals(A)
<eigenvalues only>
>>> linalg.eig(A)
<eigenvalues and eigenvectors>
>>> linalg.svd(A)
<SVD decomposition>
>>> linalg.cholesky(A)
<Cholesky decomposition for positive definite A>
>>> B=matrix(random.rand(5,5))
>>> linalg.solve(A,B)
<Solution of the equation A.X=B>
Special Functions
scipy.special

Includes over 200 functions:


Airy, Elliptic, Bessel, Gamma, HyperGeometric, Struve, Error,
Orthogonal Polynomials, Parabolic Cylinder, Mathieu,
Spheroidal Wave, Kelvin
FIRST ORDER BESSEL EXAMPLE

#environment setup
>>> import gui_thread
>>> gui_thread.start()
>>> from scipy import *
>>> import scipy.plt as plt

>>> x = r_[0:100:0.1]
>>> j0x = special.j0(x)
>>> plt.plot(x,j0x)
Special Functions
scipy.special

AIRY FUNCTIONS EXAMPLE

>>> z = r_[-5:1.5:100j]
>>> vals = special.airy(z)
>>> xplt.figure(0, frame=1,

color='blue')
>>> xplt.matplot(z,vals)
>>> xplt.legend(['Ai', 'Aip',
‘Bi‘,'Bip'],
color='blue')
>>> xplt.xlabel('z',
color='magenta')
>>> xplt.title('Airy
Functions and Derivatives‘)
Statistics
scipy.stats --- Continuous Distributions

over 80
continuous
distributions!

Methods

pdf
cdf
rvs
ppf
stats
Statistics
scipy.stats --- Discrete Distributions

10 standard
discrete
distributions
(plus any
arbitrary
finite RV)

Methods
pdf
cdf
rvs
ppf
stats
Statistics
scipy.stats --- Basic Statistical Calculations for samples

•stats.mean (also mean) compute the sample mean


•stats.std (also std) compute the sample
standard deviation
•stats.var sample variance
•stats.moment sample central moment
•stats.skew sample skew
•stats.kurtosis sample kurtosis
Interpolation
scipy.interpolate --- General purpose Interpolation

•1-d linear Interpolating Class


•Constructs callable function from data points
•Function takes vector of inputs and returns linear
interpolants
•1-d and 2-d spline interpolation (FITPACK)
•Splines up to order 5
•Parametric splines
Integration
scipy.integrate --- General purpose Integration

•Ordinary Differential Equations (ODE)


integrate.odeint, integrate.ode

•Samples of a 1-d function


integrate.trapz (trapezoidal Method), integrate.simps
(Simpson Method), integrate.romb (Romberg Method)

•Arbitrary callable function


integrate.quad (general purpose), integrate.dblquad
(double integration), integrate.tplquad (triple integration),
integrate.fixed_quad (fixed order Gaussian integration),
integrate.quadrature (Gaussian quadrature to tolerance),
integrate.romberg (Romberg)
Integration
scipy.integrate --- Example

>>> def func(x):


return integrate.quad(cos,0,x)[0]
>>> vecfunc = vectorize(func)

>>> x = r_[0:2*pi:100j]
>>> x2 = x[::5]
>>> y = sin(x)
>>> y2 = vecfunc(x2)
>>> xplt.plot(x,y,x2,y2,'rx')
Optimization
scipy.optimize --- unconstrained minimization and root finding

• Unconstrained Optimization
fmin (Nelder-Mead simplex), fmin_powell (Powell’s method), fmin_bfgs
(BFGS quasi-Newton method), fmin_ncg (Newton conjugate gradient),
leastsq (Levenberg-Marquardt), anneal (simulated annealing global
minimizer), brute (brute force global minimizer), brent (excellent 1-D
minimizer), golden, bracket
• Constrained Optimization
fmin_l_bfgs_b, fmin_tnc (truncated newton code), fmin_cobyla
(constrained optimization by linear approximation), fminbound (interval
constrained 1-d minimizer)
• Root finding
fsolve (using MINPACK), brentq, brenth, ridder, newton, bisect,
fixed_point (fixed point equation solver)
Optimization
EXAMPLE: MINIMIZE BESSEL FUNCTION

# minimize 1st order bessel


# function between 4 and 7
>>> from scipy.special import j1
>>> from scipy.optimize import \
fminbound

>>> x = r_[2:7.1:.1]
>>> j1x = j1(x)
>>> plt.plot(x,j1x,’-’)
>>> plt.hold(‘on’)
>>> j1_min = fminbound(j1,4,7)
>>> plt.plot(x,j1_min,’ro’)
Optimization
EXAMPLE: SOLVING NONLINEAR EQUATIONS

Solve the non-linear equations

>>> def nonlin(x,a,b,c):


>>> x0,x1,x2 = x
>>> return [3*x0-cos(x1*x2)+ a,
>>> x0*x0-81*(x1+0.1)**2
+ sin(x2)+b,
>>> exp(-x0*x1)+20*x2+c]
>>> a,b,c = -0.5,1.06,(10*pi-3.0)/3
starting location for search >>> root = optimize.fsolve(nonlin,
[0.1,0.1,-0.1],args=(a,b,c))
>>> print root
>>> print nonlin(root,a,b,c)
[ 0.5 0. -0.5236]
[0.0, -2.231104190e-12, 7.46069872e-14]
Optimization
EXAMPLE: MINIMIZING ROSENBROCK FUNCTION

Rosenbrock function

WITHOUT DERIVATIVE USING DERIVATIVE


>>> rosen = optimize.rosen >>> rosen_der = optimize.rosen_der
>>> import time >>> x0 = [1.3,0.7,0.8,1.9,1.2]
>>> x0 = [1.3,0.7,0.8,1.9,1.2] >>> start = time.time()
>>> start = time.time() >>> xopt = optimize.fmin_bfgs(rosen,
>>> xopt = optimize.fmin(rosen, x0, fprime=rosen_der, avegtol=1e-7)
x0, avegtol=1e-7) >>> stop = time.time()
>>> stop = time.time() >>> print_stats(start, stop, xopt)
>>> print_stats(start, stop, xopt) Optimization terminated successfully.
Optimization terminated successfully. Current function value: 0.000000
Current function value: 0.000000 Iterations: 111
Iterations: 316 Function evaluations: 266
Function evaluations: 533 Gradient evaluations: 112
Found in 0.0805299282074 seconds Found in 0.0521121025085 seconds
Solution: [ 1. 1. 1. 1. 1.] Solution: [ 1. 1. 1. 1. 1.]
Function value: 2.67775760157e-15 Function value: 1.3739103475e-18
Avg. Error: 1.5323906899e-08 Avg. Error: 1.13246034772e-10
GA and Clustering
scipy.ga --- Basic Genetic Algorithm Optimization

Routines and classes to simplify setting up a


genome and running a genetic algorithm evolution

scipy.cluster --- Basic Clustering Algorithms

•Observation whitening cluster.vq.whiten

•Vector quantization cluster.vq.vq

•K-means algorithm cluster.vq.kmeans

You might also like