Python for Scientific Computing - PowerPoint PPT Presentation

1 / 324
About This Presentation
Title:

Python for Scientific Computing

Description:

hello world s = '12345' len(s) 5. CREATING STRINGS ... hello' REMOVE WHITESPACE. 11 # list available methods on a string dir(s) 'capitalize' ... – PowerPoint PPT presentation

Number of Views:624
Avg rating:3.0/5.0
Slides: 325
Provided by: matt303
Category:

less

Transcript and Presenter's Notes

Title: Python for Scientific Computing


1
Python for Scientific Computing
  • Eric Jones and Travis Oliphant
  • eric_at_enthought.com
  • oliphant_at_enthought.com
  • Enthought, Inc.
  • www.enthought.com


2
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 collection
  • Dynamic typing
  • Interpreted and interactive
  • Object-oriented
  • Batteries Included
  • Free
  • Portable
  • Easy to Learn and Use
  • Truly Modular

3
Who is using Python?
SPACE TELESCOPE SCIENCE INSTITUTE
GOOGLE
One of top three languages used at Google along
with C and Java. Guido works there.
Data processing and calibration for instruments
on the Hubble Space Telescope.
HOLLYWOOD
PAINT SHOP PRO 9
Paint Shop Pro Scripting Engine for photo-editing
software
Digital animation and special effects Industrial
Light and Magic Imageworks Tippett
Studios Disney Dreamworks
REDHAT
Anaconda, the Redhat Linux installer program, is
written in Python.
PROCTER GAMBLE
PETROLEUM INDUSTRY
Fluid dynamics simulation tools.
Geophysics and exploration toolsConocoPhillips,
Shell
4
Programming Language Book Market
Programming language book sales for Q4, 2007.
Sizes of squares are relative to market size.
Percentages displayed indicate growth from Q4,
2006. Green squares indicate a growing market.
Red squares indicate a shrinking market. Used
with permission from Oreilly Publishing
http//radar.oreilly.com/Language_all.jpg
5
Getting Started
  • Get Python from http//www.python.org
  • Python comes with a lot of programming
    cabability (web, data-base, file, string
    manipulation, hash-tables etc.)
  • To make it suitable for Scientific Work requires
    extra packages
  • NumPy and SciPy
  • Matplotlib and IPython (plotting and interactive)
  • WxPython or PyQT (basic GUI)
  • Traits, Chaco, Enable, and Kiva (rapid GUI
    building with 2-d visualization)
  • VTK and MayaVi (advanced 3-d visualization)


6
OR EPD
  • Alternatively Grab the Enthought Python
    Distribution (EPD) http//www.enthought.com/epd

7
IPython
8
Interactive Calculator
adding two values 1 1 2 setting a
variable a 1 a 1 checking a variables
type type(a) an arbitrarily
long integer a 1203405503201
a 1203405503201L type(a)
real numbers b 1.2 3.1
b 4.2999999999999998 type(b)
complex numbers c 21.5j c (21.5j)
The four numeric types in Python on 32-bit
architectures are integer (4 byte) long
integer (any precision) float (8 byte like Cs
double) complex (16 byte) The numpy module,
which we will see later, supports a larger number
of numeric types.
9
Strings
CREATING STRINGS
STRING LENGTH
using double quotes s hello world
print s hello world single quotes also work
s hello world print s hello world
s 12345 len(s) 5
SPLIT/JOIN STRINGS
split space delimited words wrd_lst
s.split() print wrd_lst hello, world
join words back together with a space
in-between .join(wrd_lst) hello world
STRING OPERATIONS
concatenating two strings hello
world hello world repeating a string
hello 3 hello hello hello
10
A few string methods
REPLACING TEXT
s hello world s.replace(world,Mars
) hello Mars
CONVERT TO UPPER CASE
s.upper() HELLO MARS
REMOVE WHITESPACE
s \t hello \n s.strip() hello
11
Available string methods
list available methods on a string
dir(s) 'capitalize', 'center', 'count', 'decode
', 'encode', 'endswith', 'expandtabs', 'find', 'in
dex', 'isalnum', 'isalpha', 'isdigit',
'rsplit', 'rstrip', 'split', 'splitlines', 'starts
with', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill'
'islower', 'isspace', 'istitle', 'isupper', 'join'
, 'ljust', 'lower', 'lstrip', 'replace', 'rfind',
'rindex', 'rjust',
12
Multi-line Strings
strings in triple quotes retain line
breaks a hello ... world print
a hello world multi-line strings using \
to indicate continuation a hello \ ...
world print a hello world
including the new line a hello\n \ ...
world print a hello world
13
String Formatting
FORMAT STRINGS
NAMED VARIABLES
the operator allows you to supply values to
a format string. The format string follows
C conventions. s some numbers x
1.34 y 2 t s f, d
(s,x,y) print tsome numbers 1.340000, 2
It also supports a named variable approach
where (var_name)f will output var_name in
float format. (s)s (x)f, (y)d ...
locals() print tsome numbers 1.340000, 2
NAMED VARIABLES
As of 2.4, Python supports templates
from string import Template t Template(s
x y) t.substitute(locals()) some numbers
1.34, 2
For more information, see http//docs.python.org/
lib/typesseq-strings.html http//docs.python.org/l
ib/node40.html More advanced templating engines
such as Cheetah also exist http//www.cheetahte
mplate.org/
14
List objects
LIST CREATION WITH BRACKETS
range( start, stop, step)
l 10,11,12,13,14 print l 10, 11, 12,
13, 14
the range method is helpful for creating a
sequence range(5) 0, 1, 2, 3, 4
range(2,7) 2, 3, 4, 5, 6 range(2,7,2) 2,
4, 6
CONCATENATING LIST
simply use the operator 10, 11 12,
13 10, 11, 12, 13
REPEATING ELEMENTS IN LISTS
the multiply operator does the trick.
10, 11 3 10, 11, 10, 11, 10, 11
15
Indexing
RETREIVING AN ELEMENT
NEGATIVE INDICES
list indices 0 1 2 3 4 l
10,11,12,13,14 l0 10
negative indices count backward from the end
of the list. indices -5 -4 -3 -2 -1
l 10,11,12,13,14 l-1 14
l-2 13
SETTING AN ELEMENT
l1 21 print l 10, 21, 12, 13, 14
OUT OF BOUNDS
l10 Traceback (innermost last) File
"",line 1,in ?IndexError
list index out of range
The first element in an array has index0 as in
C. Take note Fortran programmers!
16
More on list objects
LIST CONTAINING MULTIPLE TYPES
LENGTH OF A LIST
len(l) 3
list containing integer, string, and another
list. l 10,eleven,12,13
l1 eleven l2 12, 13 use multiple
indices to retrieve elements from nested
lists. l20 12
DELETING OBJECT FROM LIST
use the del keyword del l2
l 10,eleven
DOES THE LIST CONTAIN x ?
use in or not in l 10,11,12,13,14
13 in l True 13 not in l False
Prior to version 2.5, Python was limited to
sequences with 2 billion elements. Python 2.5
can handle up to 263 elements.
17
Slicing
varlowerupperstep
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.
Mathematically the range is lower,upper). The
step value specifies the stride between elements.
SLICING LISTS
OMITTING INDICES
indices 0 1 2 3 4 l
10,11,12,13,14 10,11,12,13,14
l13 11, 12 negative indices work
also l1-2 11, 12 l-43 11, 12
omitted boundaries are assumed to be the
beginning (or end) of the list. grab first
three elements l3 10, 11, 12 grab last
two elements l-2 13, 14 every other
element l2 10, 12, 14
18
A few methods for list objects
some_list.append( x )
some_list.pop( index )
Add the element x to the end of the list,
some_list.
Return the element at the specified index. Also,
remove it from the list.
some_list.count( x )
some_list.remove( x )
Delete the first occurrence of x from the list.
Count the number of times x occurs in the list.
some_list.reverse( )
some_list.extend( sequence )
Reverse the order of elements in the list.
Concatenate sequence onto this list.
some_list.sort( cmp )
some_list.index( x )
Return the index of the first occurrence of x in
the list.
By default, sort the elements in ascending order.
If a compare function is given, use it to sort
the list.
some_list.insert( index, x )
Insert x before the specified index
19
List methods in action
l 10,21,23,11,24 add an element to the
list l.append(11) print
l 10,21,23,11,24,11 how many 11s are
there? l.count(11) 2 extend with another
list l.extend(5,4) print
l 10,21,23,11,24,11,5,4 where does 11 first
occur? l.index(11) 3 insert 100 at index
2? l.insert(2, 100) print
l 10,21,100,23,11,24,11,5,4
pop the item at index4 l.pop(3) 23
remove the first 11 l.remove(11) print
l 10,21,100,24,11,5,4 sort the list
l.sort() print l 4,5,10,11,21,24,100
reverse the list l.reverse() print
l 100,24,21,11,10,5,4
20
Assorted other list functions
SORTED
ZIP
l.sort() is an inplace sort. sorted(l)
returns a new list of the items in l
sorted. l 10,21,23,11,24
sorted(l)10, 11, 21, 23, 24
zip combines elements of multiple lists
together as tuples. x 1,2,3 y
a,b,c z zip(x, y) z (1,a),
(2,b), (3, d) zip (with a little trick)
is also its own inverse zip(z) (1, 2, 3),
('a', 'b', 'c')
REVERSED
reversed(l) returns an iterator that will
return elements of the list in reverse
order. A copy is not made unless explicitly
asked for. l 10,21,23,11,24 for i in
reversed(l) ... print i,24 11 23 21 10
21
Mutable vs. Immutable
MUTABLE OBJECTS
IMMUTABLE OBJECTS
Mutable objects, such as lists, can be
changed in-place. insert new values into
list l 10,11,12,13,14 l13
5,6 print l 10, 5, 6, 13, 14
Immutable objects, such as strings, cannot be
changed in-place. try inserting values
into a string s abcde s13
xy Traceback (innermost last) File
"",line 1,in ? TypeError
object doesn't support slice
assignment heres how to do it s s1
xy s3 print s 'axyde'
The cStringIO module treats strings like a file
buffer and allows insertions. Its useful when
working with large strings or when speed is
paramount.
22
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 recordfirst Jmes
recordlast Maxwell recordborn
1831 print record 'first' 'Jmes', 'born'
1831, 'last' 'Maxwell' create another
dictionary with initial entries new_record
first James, middleClerk 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
23
A few dictionary methods
some_dict.clear( )
some_dict.keys( )
Remove all key/value pairs from the dictionary,
some_dict.
Return a list of all the keys in the dictionary.
some_dict.copy( )
some_dict.values( )
Create a copy of the dictionary
Return a list of all the values in the dictionary.
some_dict.has_key( x )
some_dict.items( )
Test whether the dictionary contains the key x.
Return a list of all the key/value pairs in the
dictionary.
24
Dictionary methods in action
d cows 1,dogs5, ... cats
3 create a copy. dd d.copy() print
dd 'dogs'5,'cats'3,'cows' 1 test for
chickens. d.has_key(chickens) False get
a list of all keys d.keys() cats,dogs,co
ws
get a list of all values d.values() 3, 5,
1 return the key/value pairs
d.items() ('cats', 3), ('dogs', 5), ('cows',
1) clear the dictionary d.clear()
print d
25
Assignment of simple object
Assignment creates object references.
x
0
x 0
y x cause x and y to point at the same
value y x
y
0
x
re-assigning y to a new value decouples the
two y foo print x 0
foo
y
26
Assignment of Container object
Assignment creates object references.
x
x 0, 1, 2
y x cause x and y to point at the same
list y x
y
x
changes to y also change x y1 6
print x 0, 6, 2
y
x
re-assigning y to a new list decouples the
two lists y 3, 4
y
27
Multiple assignments
creating a tuple without () d 1,2,3
d (1, 2, 3) multiple assignments a,b,c
1,2,3 print b 2
multiple assignments from a tuple a,b,c
d print b 2 also works for lists
a,b,c 1,2,3 print b 2
28
If statements
if/elif/else provide conditional execution of
code blocks.
IF EXAMPLE
IF STATEMENT FORMAT
a simple if statement x 10 if x
0 ... print 1 ... elif x 0 ... print
0 ... else ... print 1 ... 1
if elif
else
29
Test Values
  • True means any non-zero number or non-empty
    object
  • False means not true zero, empty object, or None

EMPTY OBJECTS
It often pays to be explicit. If you are testing
for an empty list, the test for if len(x)
0 ... This is more explanatory to future
readers of your code. It also can avoid bugs
where xNone my be passed in and unexpectedly go
down this path.
empty objects evaluate false x if
x ... print 1 ... else ... print 0 ...
0
30
For loops
For loops iterate over a sequence of objects.
for in
TYPICAL SCENARIO
LOOPING OVER A LIST
for i in range(5) ... print i, ... return 0 1 2 3 4
ldogs,cats,bears accum
for item in l ... accum accum item ...
accum accum ...
print accum dogs cats bears
LOOPING OVER A STRING
for i in abcde ... print i, ... return a b c d e
31
While loops
While loops iterate until a condition is met.
while
WHILE LOOP
BREAKING OUT OF A LOOP
breaking from an infinite loop. i 0
while 1 ... if i else ... break ... i i 1 ... return 0 1 2
the condition tested is whether lst is
empty. lst range(3) while
lst ... print lst ... lst lst1 ... return 0, 1, 2 1, 2 2
32
List Comprehension
LIST COMPREHENSION
LIST TRANSFORM WITH LOOP
Element by element transform of a list by
applying an expression to each element. l
10,21,23,11,24 results for x in
l ... results.append(x1) results 11,
22, 24, 12, 25
List comprehensions provide a concise syntax
for this sort of element by element
transformation. l 10,21,23,11,24
val1 for val in l11, 22, 24, 12, 25
LIST COMPREHENSION WITH FILTER
FILTER-TRANSFORM WITH LOOP
Transform only elements that meet a
criteria. l 10,21,23,11,24
results for x in l ... if x15 ...
results.append(x1) results 22, 24, 25
l 10,21,23,11,24 v1 for v in l if
v1522, 24, 25
33
Anatomy of a function
Function arguments are listed separated by
commas. They are passed by assignment. More on
this later.
The keyword def indicates the start of a
function.
def add(arg0, arg1) a arg0 arg1 return a
Indentation is used to indicatethe contents of
the function. It is not optional, but a part of
the syntax.
A colon ( ) terminatesthe function definition.
An optional return statement specifies the
value returned from the function. If return is
omitted, the function returns the special value
None.
34
Our new function in action
Well create our function on the fly in the
interpreter. def add(x,y) ... a x
y ... return a test it out with
numbers x 2 y 3 add(x,y) 5
how about strings? x foo y
bar add(x,y) foobar functions can be
assigned to variables func add
func(x,y) foobar
how about numbers and strings?
add(abc',1) Traceback (innermost last) File
"", line 1, in ? File
"", line 2, in addTypeError
cannot add type "int" to string
35
Keyword arguments
MULTIPLE KEYWORDS
DEFAULT VALUES
Multiple keyword arguments are possible.
def box_area(width1.0, ...
height1.0) ... area width height ...
return area As positional arguments
box_area(1,2) 2 As keyword arguments
box_area(width1, height2) 2 Specify only
height box_area(height3) 3
Keyword arguments specify a default value
for an argument making it optional def
increment(x,step1) ... result x 1 ...
return result The defaults step size is
1. increment(10) 11 The step specified by
its position in the argument list.
increment(10, 3) 13 or as a keyword
argument increment(10, step5) 15
36
Modules
EX1.PY
FROM SHELL
ej_at_bull ej python ex1.py 6, 3.1416
ex1.py PI 3.1416 def sum(lst) tot
lst0 for value in lst1 tot
tot value return tot l 0,1,2,3 print
sum(l), PI
FROM INTERPRETER
load and execute the module import ex1 6,
3.1416 get/set a module variable.
ex1.PI 3.1415999999999999 ex1.PI
3.14159 ex1.PI 3.1415899999999999 call a
module variable. t 2,3,4 ex1.sum(t) 9
37
Modules cont.
EDITED EX1.PY
INTERPRETER
ex1.py version 2 PI 3.14159 def sum(lst)
tot 0 for value in lst tot
tot value return tot l 0,1,2,3,4 print
sum(l), PI
load and execute the module import ex1 6,
3.1416 import module again
import ex1 nothing happens!!! use reload to
force a previously imported library to be
reloaded. reload(ex1) 10, 3.14159
38
Modules cont. 2
Modules can be executable scripts or libraries or
both.
EX2.PY
EX2.PY CONTINUED
An example module PI 3.1416 def
sum(lst) Sum the values in a
list. tot 0 for value in
lst tot tot value return tot
def add(x,y) Add two values. a x
y return a def test() l 0,1,2,3
assert( sum(l) 6) print test passed
this code runs only if this module is the main
program if __name__ __main__ test()
39
Reading files
FILE INPUT EXAMPLE
PRINTING THE RESULTS
results f open(c\\rcs.txt,r)
read lines and discard header lines
f.readlines()1 f.close() for l in
lines ... split line into fields ...
fields l.split() ... convert text to
numbers ... freq float(fields0) ... vv
float(fields1) ... hh float(fields2) ...
group append to results ... all
freq,vv,hh ... results.append(all) ... return
for i in results print i 100.0, -20.30,
-31.20 200.0, -22.70, -33.60
EXAMPLE FILE RCS.TXT
freq (MHz) vv (dB) hh (dB) 100 -20.3
-31.2 200 -22.7 -33.6
See demo/reading_files directory for code.
40
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) .
.. for i in results ...
print i ...
EXAMPLE FILE RCS.TXT
freq (MHz) vv (dB) hh (dB) 100 -20.3
-31.2 200 -22.7 -33.6
41
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 l0 !""
EXAMPLE FILE RCS.TXT
freq (MHz) vv (dB) hh (dB) 100 -20.3
-31.2 200 -22.7 -33.6
42
Exception Handling
ERROR ON LOG OF ZERO
import math math.log10(10.0) 1.
math.log10(0.0) Traceback (innermost last)
OverflowError math range error
CATCHING ERROR AND CONTINUING
a 0.0 try ... r
math.log10(a) ... except OverflowError ...
print Warning overflow occurred. Value set to
0.0 ... set value to 0.0 and continue ...
r 0.0 Warning overflow occurred. Value set
to 0.0 print r 0.0
43
Handling multiple Exceptions
EXCEPTION RAISED INDEXING PAST THE END OF A LIST
try ... x 5, 4, 3, 2, 1 ... x5
0 ... except IndexError, msg ... print
index error, msg ... except (OtherError,
UnknownError) ... print A user defined
exception was raised. ... except ...
pass ...else ... print No exception was
raised. index error list assignment index out
of range
44
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
objects string representation ... def
__repr__(self) ... msg "(m2.1f, v2.1f)"
(self.mass,self.velocity) ... return msg
EXAMPLE
a Particle(3.2,4.1) a (m3.2,
v4.1) a.momentum() 13.119999999999999
45
Classes
SIMPLE PARTICLE CLASS
Classes can over-ride many behaviors using
special method names --- including Numeric
behavior.
... def __add__(self, other) ... if not
isinstance(other, Particle) ... return
NotImplemented ... mnew self.mass
other.mass ... vnew (self.momentum()
other.momentum())/mnew ... return
Particle(mnew, vnew)
EXAMPLE (cont.)
b Particle(8.6,10.2) b, a ((m8.6,
v10.2), (m3.2, v4.1)) ab (m11.8,
v8.5) (ab).momentum() 100.83999999999999
46
Iterators
Iterators are anything that can be looped over.
They shield users from implementation details of
looping over an object. Classes that have
__iter__ and next methods can be used as
iterators.
USING ITERATORS
CLASS DEFINTION
class FootballRushIterator def __init__(self,
count5) self.count count def
__iter__(self) Prepare an iterable
object. self._counter 1 return self
def next(self) cnt self._counter
if cnt element res str(cnt) " mississippi"
self._counter 1 return res else
Or signal the end of iteration. raise
StopIteration
rusher FootballRushIterator() for count
in rusher ... print count 1 mississippi 2
mississippi 3 mississippi 4 mississippi 5
mississippi
47
  • Batteries Included
  • A Tour of the Standard Library

48
Standard Library Overview
  • sys Python system interaction
  • os operating system interaction
  • datetime/time time and date types and
    arithmetic
  • ftplib ftp
  • csv comma separated values
  • SimpleHTTPServer
  • XMLRPC

49
The sys Module
sys is used to get and set things about your
Python environment. sys.argv exposes command
line arguments for processing.
sys.argv
sys IN ACTION
import sys Python version info
sys.version_info (2, 5, 2, 'final', 0) Useful
for platform specific code
sys.platfrom win32 More details
sys.getwindowsversion() (5, 1, 2600, 2, 'Service
Pack 2')
returns a list of commands used when starting
the current python environment
sys.argv 'C\\Python25\\Scripts\\ipython-script.p
y' This is one way to parse any command
arguments to the running file
50
The os Module
os is one of the most useful modules because it
provides a cross platform way of interacting with
the OS and the filesystem.
OTHER USEFUL OS METHODS
os.path IN ACTION
import os os.path.curdir '.'
os.path.abspath(os.path.curdir) 'C\\temp\\
os.listdir('c\\temp') 'testdir',
'testingfile.txt' os.system('test.bat')
Directory of c\Windows 04/16/2008 1023 AM
. 04/16/2008 1023 AM
.. 11/17/2006 0936 AM
AppPatch 08/04/2004 0700 AM 1,272
Blue Lace 16.bmp
os.listdir('.') 'file_list.py',
'file_list_solution.py', 'file_list_solution.pyc'
, 'os_example.py', 'test.bat',
'test.html' os.startfile(test.html)
Launches app associated with html files
(Firefox on my system) os.getenv('HOMEPATH')
'\\Documents and Settings\\travis'
51
The datetime module
datetime supplies a rich object for handling and
formatting datetime information.
datetime arithmetic
datetime.date IN ACTION
  • from datetime import date
  • now date.today()
  • now
  • datetime.date(2008, 4, 18)
  • now.weekday()
  • 4 Note Monday 0
  • now.strftime("A, B d, Y")
  • 'Friday, April 18, 2008'

my_bday date(1969, 11,17) print now
my_bday 14032 days, 00000 Man, thats
old print int(float('04d.02d02d'
(now.timetuple()3)) - float('04d.02d02d'
(my_b day.timetuple()3))) 38 It doesnt
seem quite so old in years
52
Some strftime codes
  • a Locale's abbreviated weekday name.
  • A Locale's full weekday name.
  • b Locale's abbreviated month name.
  • B Locale's full month name.
  • c Locale's appropriate date and time
    representation.
  • d Day of the month as a decimal number 01,31.
  • H Hour (24-hour clock) as a decimal number
    00,23.
  • I Hour (12-hour clock) as a decimal number
    01,12.
  • j Day of the year as a decimal number
    001,366.
  • m Month as a decimal number 01,12.
  • M Minute as a decimal number 00,59.
  • p Locale's equivalent of either AM or PM.
  • S Second as a decimal number 00,61.
  • U Week number of the year
  • w Weekday as a decimal number 0(Sunday),6.
  • W Week number of the year
  • x Locale's appropriate date representation.
  • X Locale's appropriate time representation.
  • y Year without century as a decimal number
    00,99.

53
FTP -- Sending binary files
Generate a plot pylab.plot(1,2,3)
Save the plot as plot.png Open an ftp
connection import ftplib server
www.enthought.com user, password
pythonclass, utpythonclass site
ftplib.FTP(server, user, password) To work
with U.T. Austin Firewall. site.set_pasv(False
) Send file to site in binary
format, and clean up site.cwd(public_html)
img open(plot.png, rb)
site.storbinary(STOR plot.png, img)
site.sendcmd(SITE CHMOD 755 plot.png)
img.close() site.quit()
54
FTP Sending text
Generate html page for plot html
Judahs Nice Plot srcplot.png Make a
file-like object from string import
cStringIO html_file cStringIO.StringIO(html)
Open an ftp connection (variables from
previous page) import ftplib site
ftplib.FTP(server, user, password)
site.cwd(public_html) site.storlines(STOR
plot.html, html_file) site.sendcmd(SITE
CHMOD 755 plot.html) img.close()
site.quit()
55
FTP -- Retrieving files
Open an ftp connection (variables from previous
page) import ftplib site
ftplib.FTP(server, user, password)
site.cwd(public_html) Grab directory
contents site.retrlines('LIST') -rw-r--r--
1 578 578 58 Apr 16 2008
plot.html -rw-r--r-- 1 578 578 16711 Apr
16 2008 plot.png '226 Directory send OK.
Grab a file and stick data in a string import
cStringIO data_buffer cStringIO.StringIO()
site.retrlines('RETR plot.html',data_buffer.wri
te) '226 File send OK.' data_buffer.getvalue()
'Nice Plot '
56
Browsers and HTTP
LAUNCH AN EXTERNAL WEB BROWSER
import webbrowser url
http//www.enthought.com/pythonclass/plot.html
webbrowser.open(url) on your platform for viewing
USING urllib2
import urllib2 url http//www.enthought
.com/pythonclass/plot.html webpage
urllib2.urlopen(url) Retrieve the file and
print the data print webpage.read() print
webstring 'Nice Plot for Judah src"plot_for_judah.png"'
57
The csv Module
Comma Separated Values of the format
ID,Last_Name,First_Name,Date_Assigned,Title,Salary
1003,Jones,Michael,2/14/2006,Assistant
V.P.,85000
USING csv.reader
import csv f open(data1.csv, r)
r csv.reader(f) dat for line in
r print line dat.append(line) '
ID', 'Last_Name', 'First_Name', 'Date_Assigned',
'Title', 'Salary' '1003', 'Jones', 'Michael',
'2/14/2006', 'Assistant V.P.', '85000' '1008',
'Hudspeth', 'Gus', '12/25/2003', 'Engineer',
'70000' '1009', 'Raphael', 'Bernard',
'7/4/2005', 'Contract Administrator',
'48000' '1018', 'Goforth', 'Susan', '1/1/2007',
'President', '110000' '1021', 'Forreston',
'April', '3/17/2006', 'Activities Coordinator',
'38000'
58
The csv Module, contd
USING csv.writer
Note Sometimes is just as good the parse out
your data manually. (variable used from previous
slide) import csv f open(data_out.csv
, w) w csv.writer(f, dialectexcel,
linedelimier\n) w.writerows(dat)
59
XMLRPC
SETTING UP A SERVER
import from standard lib import
SimpleXMLRPCServer local import from csv_util
import csv2dict PORT8080 server
SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost"
, PORT)) server.register_function(csv2dict)
Let's light this candle! print "Listening on
port " , PORT server.serve_forever()
60
XMLRPC, contd
CALLING FROM A CLIENT
  • import xmlrpclib
  • client xmlrpclib.ServerProxy("http//localhost8
    080")
  • data client.csv2dict('data1.csv', 0)
  • print data

61
Other Standard Lib modules
  • The following modules wont be covered in detail,
    but are considered very useful
  • glob pattern matching
  • re regular expressions
  • math math operations
  • zlib compressed file creation/reading
  • urllib2 scraping web data

62
IPython
  • An enhanced interactive python shell

63
IPython command prompt
  • Available at http//ipython.scipy.org/
  • Developed by Fernando Perez atUniversity of
    Colorado at Boulder
  • Provides a nice environment for scientific
    computing with Python

64
IPython
STANDARD PYTHON
OUTPUT HISTORY
In 1 a1 In 2 a Out2 1
grab result from prompt2 In 5 _2 Out5 1
AVAILABLE VARIABLES
HISTORY COMMAND
list previous commands. Use magic because
hist is histogram function in pylab In 3
hist 1 a1 2 a
In 6 b 1,2,3 List available
variables. In 7 whos Variable Type
Data/Length ----------------------------- a
int 1 b list 1, 2, 3
INPUT HISTORY
list string from prompt2 In 4 _i2 Out4
'a\n'
65
Shell Commands
change directory (note Unix style forward
slashes!!) In 9 cd c/demo/speed_of_light c\de
mo\speed_of_light list directory contents In
10 ls Volume in drive C has no label. Volume
Serial Number is E0B4-1D2D Directory of
c\demo\speed_of_light 11/11/2004 0351 PM
. 11/11/2004 0351 PM
.. 11/08/2004 1145 PM 1,188
exercise_speed_of_light.txt 11/08/2004 1052 PM
2,682,023 measurement_description.pdf 11/08/20
04 1044 PM 187,087 newcomb_experiment.pdf
11/08/2004 1051 PM 1,402
speed_of_light.dat 11/11/2004 0351 PM
1,017 speed_of_light.py 8 File(s) 2,874,867
bytes 2 Dir(s) 8,324,673,536
bytes free
66
Directory History
change directory In 1 cd c/demo/speed_of_lig
ht c\demo\speed_of_light list the history of
directory changes In 2 dhist Directory history
(kept in _dh) 0 C\Python23\Scripts 1
c\demo\speed_of_light change to a directory
in the history chosen by number. In 3 cd
-0 C\Python23\Scripts
67
Magic commands
tab completion In 11 run speed_of_li speed_of
_light.dat speed_of_light.py execute a python
file In 11 run speed_of_light.py
68
Magic Commands
pdef prints the definition for a command In
45 pdef stats.histogram stats.histogram(a,
numbins10, defaultlimitsNone, printextras1)
psource prints the source code for a command In
43 psource squeeze def squeeze(a)
"Returns a with any ones from the shape of a
removed" a asarray(a) b
asarray(a.shape) val reshape (a, tuple
(compress (not_equal (b, 1), b))) return val
psource cant show the source code for
extension functions that are implemented in C.
69
Magic Commands
? prints information about an object In 46
stats.histogram? Type function Base
Class String Form histogram at 0x02289730 Namespace
Interactive File c\python23\lib\site-pack
ages\scipy\stats\stats.py Definition
stats.histogram(a, numbins10, defaultlimitsNone,
printextras1) Docstring Returns (i) an
array of histogram bin counts, (ii) the smallest
value of the histogram binning, and (iii) the bin
width (the last 2 are not necessarily integers).
Default number of bins is 10. Defaultlimits can
be None (the routine picks bins spanning all the
numbers in the a) or a 2-sequence (lowerlimit,
upperlimit). Returns all of the following array
of bin values, lowerreallimit, binsize,
extrapoints. Returns (array of bin counts,
bin-minimum, min-width, -points-outside-range)
70
Selections from the Standard Library
71
FTP -- Sending binary files
Generate a plot plot((1,2,3))
savefig(simple_plot.png) broken in current
release Open an ftp connection import
ftplib server www.enthought.com
user,password eric, python site
ftplib.FTP(server, user, password) Change to
web directory on server homepage_folder
public_html site.cwd(homepage_folder)
Send file to site in binary format, and clean
up img open(simple_plot.png)
site.storbinary(STOR plot.png, img)
img.close() site.quit()
72
FTP Sending text
Generate html page for plot html
A Nice Plot srcplot.png Make a
file-like object from string import
cStringIO html_file cStringIO.StringIO(html)
Open an ftp connection (variables from
previous page) import ftplib site
ftplib.FTP(server, user, password)
site.cwd(homepage_folder) site.storlines(STOR
plot.html, html_file) img.close()
site.quit()
73
FTP -- Retrieving files
Open an ftp connection (variables from previous
page) import ftplib site
ftplib.FTP(server, user, password)
site.cwd(homepage_folder) Grab directory
contents site.retrlines('LIST') total
12 -rw-r--r-- 1 eric eric 56 Aug 26
1441 plot.html -rw-r--r-- 1 eric eric
5111 Aug 26 1438 plot.png 226 Transfer
complete. Grab a file and stick data in a
string import cStringIO data_buffer
cStringIO.StringIO() site.retrlines('RETR
plot.html',data_buffer.write) '226 Transfer
complete.' data_buffer.getvalue() 'Nice
Plot '
74
Browsers and HTTP
LAUNCH AN EXTERNAL WEB BROWSER
import webbrowser webbrowser.open('http/
/www.enthought.com/eric/plot.html') web browser on your platform for viewing
USING httplib
import httplib site httplib.HTTP(http/
/www.enthought.com) setup a request header
(RFC822 format) site.putrequest(GET,/eric/
plot.html) site.putheader(Accept,text/html
) site.putheader(Accept,text/plain)
site.endheaders() Retrieve the file and print
the data errcode,errmsg,headers
site.getreply() file site.getfile()
file.read() 'Nice Plot src"plot.png"' file.close()
75
Telnet Remote system control
import telnetlib tn telnetlib.Telnet(so
me.host.com') Read text up to the login
prompt and send login name tn.read_until("logi
n ")
tn.write("ej\n") Read text up to the password
prompt and send password tn.read_until("Passwo
rd ") "ej's Password " tn.write(secret_passw
ord \n") Retrieve directory listing and
exit. tn.write("ls\n") tn.write("exit\n")
Read all text from telnet session since
password prompt. results tn.read_all()
print results and other stuff ls foo.c info
bin exit logout
76
Remote Call example -- xmlrpc
XMLRPC FACTORIAL SERVER
import SocketServer, xmlrpcserver def fact(n)
if n fact(n-1) Override the call() handler to
call the requested function class
my_handler(xmlrpcserver.RequestHandler) def
call(self, method, params) print "CALL",
method return apply(eval(method),params)
Start a server listening for request on port
8001 if __name__ '__main__' server
SocketServer.TCPServer(('', 8001), my_handler)
server.serve_forever()
CLIENT CODE -- CALLS REMOTE FACTORIAL FUNCTION
import xmlrpclib svr xmlrpclib.Server("h
ttp//localhost8001") svr.fact(10) 3628800
77
Retrieving remote files
GRAB AND VIEW MATRIX FROM THE MATRIX MARKET SERVER
import gzip import os from tempfile import mktemp
from urllib import urlretrieve from scipy
import io from matplotlib.pylab import imshow,
show url"ftp//math.nist.gov/pub/MatrixMarket2/SP
ARSKIT/fidap/fidap005.mtx.gz" fname  
mktemp(".mtx.gz") print "Downloading Matrix
Market this may take a minute..."
urlretrieve(url, fname) a io.mmread(gzip.open(
fname)) imshow(a.toarray()) show() os.unlink(fnam
e)
Source available in demo/url_retreive/matrix_marke
t.py
78
Pipes
import os Change directories
os.chdir(c\\) Get command output from
pipe. p os.popen('dir') print
p.read() Volume C has no label. Volume Serial is
58C7-F5CD Directory of C 07/10/01 457p
act 08/03/01 338p ATI ...
Use popen2 to get pipes for both reading and
writing. snd,rcv os.popen2('grep dog')
snd.write('1 cat\n 2 dog\n') snd.close()
print rcv.read() 2 dog
Take care when using input and output pipes.
Most OSes buffer IO which can lead to unexpected
behavior. When sending data through a pipe, call
.flush() or .close() to force the write. When
reading data, your at the mercy of the other
process and the OS. rcv.read() can deadlock in
worst case situations.
79
Ipython help
IPYTHONS ? RETURNS HELP FOR OBJECTS
In 22 from numpy import array In 31
array? Type builtin_function_or_method
Base Class String Form array Namespace Interactive Docstring
array(object, dtypeNone, copy1,orderNone,
subok0,ndmin0) Return an array from object
with the specified date-type. Inputs object -
an array, any object exposing the array
interface, any object whose __array__
method returns an array, or any
(nested) sequence. dtype - The desired
data-type for the array. If not given, then
the type will be determined as the minimum
type required to hold the objects in
the sequence. This argument can only
be used to 'upcast' the array. For downcasting,
use the
80
Ipython help
Add a question mark to any object for access to
its help information
In 24 a array(1,2,3,4,5,6) In 25
a? Type array String Form 1 2 3
4 5 6 Namespace Interactive Length
2 Docstring A array object represents a
multidimensional, homogeneous array of basic
values. It has the folowing data members,
m.shape (the size of each dimension in the
array), m.itemsize (the size (in bytes) of each
element of the array), and m.typecode (a
character representing the type of the matrices
elements). Matrices are sequence, mapping and
numeric objects. Sequence indexing is similar to
lists, with single indices returning a reference
that points to the old matrices data, and slices
returning by copy. A array is also allowed to be
indexed by a sequence of items. Each member of
the sequence indexes the corresponding dimension
of the array. Numeric operations operate on
matrices in an element-wise fashion.
81
Another excellent source of help...
http//www.python.org/doc
82
NumPy
83
NumPy
  • Website -- http//numpy.scipy.org/
  • Offers Matlab-ish capabilities within Python
  • NumPy replaces both Numeric and Numarray
  • Developed by Travis Oliphant
  • 27 svn committers to the project
  • Numpy 1.0 released October, 2006
  • 20K downloads/month from Sourceforge.
  • This does not count
  • Linux distributions that include numpy
  • Enthought distributions that include numpy

84
Getting Started
IMPORT NUMPY
Often at the command line, it is handy to import
everything fromnumpy into the command shell.
  • from numpy import
  • __version__
  • 1.0.2.dev3487
  • or
  • from numpy import array, ...

However, if you are writing scripts, it is
easier for others to read and debug in the
future if you use explicit imports.
USING IPYTHON -PYLAB
Ipython has a pylab mode whereit imports all
of numpy, matplotlib, and scipy into the
namespace foryou as a convenience.
C\ ipython pylab In 1 array((1,2,3)) Out1
array(1, 2, 3)
While IPython is used for all the demos,
is used on future slides instead of In 1
because it takes up less room.
85
Array Operations
SIMPLE ARRAY MATH
MATH FUNCTIONS
Create array from 0 to 10 x
arange(11.) multiply entire array by scalar
value a (2pi)/10. a 0.62831853071795862
ax array( 0.,0.628,,6.283) inplace
operations x a x array(
0.,0.628,,6.283) apply functions to
array. y sin(x)
a array(1,2,3,4) b
array(2,3,4,5) a b array(3, 5, 7, 9)
Numpy defines the following constants pi
3.14159265359 e 2.71828182846
86
Plotting Arrays
MATPLOTLIB
CHACO SHELL
plot(x,y)
from enthought.chaco2 \... import
shell shell.plot(x,y)
87
Introducing Numpy Arrays
SIMPLE ARRAY CREATION
ARRAY SHAPE
a array(0,1,2,3) a array(0, 1, 2, 3)
shape returns a tuple listing the length of
the array along each dimension.
a.shape (4,) shape(a) (4,) size reports
the entire number of elements in an
array. a.size 4 size(a) 4
CHECKING THE TYPE
type(a)
ARRAY SIZE
NUMERIC TYPE OF ELEMENTS
a.dtype dtype(int32)
BYTES PER ELEMENT
a.itemsize per element 4
88
Introducing Numpy Arrays
CONVERSION TO LIST
BYTES OF MEMORY USED
returns the number of bytes used by the data
portion of the array. a.nbytes 12
convert a numpy array to a python list.
a.tolist() 0, 1, 2, 3 For 1D arrays, list
also works equivalently, but is slower.
list(a) 0, 1, 2, 3
NUMBER OF DIMENSIONS
a.ndim 1
ARRAY COPY
create a copy of the array b a.copy()
b array(0, 1, 2, 3)
89
Setting Array Elements
BEWARE OF TYPE COERSION
ARRAY INDEXING
a0 0 a0 10 a 10, 1, 2, 3
a.dtype dtype('int32') assigning a float
to into an int32 array will truncate decimal
part. a0 10.6 a 10, 1, 2, 3 fill
has the same behavior a.fill(-4.8) a -4,
-4, -4, -4
FILL
set all values in an array. a.fill(0)
a 0, 0, 0, 0 This also works, but may be
slower. a 1 a 1, 1, 1, 1
90
Multi-Dimensional Arrays
NUMBER OF DIMENSIONS
MULTI-DIMENSIONAL ARRAYS
a.ndims 2
a array( 0, 1, 2, 3,
10,11,12,13) a array( 0, 1, 2, 3,
10,11,12,13)
GET/SET ELEMENTS
a1,3 13 a1,3 -1 a array( 0,
1, 2, 3, 10,11,12,-1)
column
(ROWS,COLUMNS)
row
a.shape (2, 4) shape(a) (2, 4)
ADDRESS FIRST ROW USING SINGLE INDEX
ELEMENT COUNT
a.size 8 size(a) 8
a1 array(10, 11, 12, -1)
91
Array Slicing
SLICING WORKS MUCH LIKE STANDARD PYTHON SLICING
a0,35 array(3, 4)
a4,4 array(44, 45, 54,
55) a,2 array(2,12,22,32,42,52)
STRIDES ARE ALSO POSSIBLE
a22,2 array(20, 22, 24, 40,
42, 44)
92
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,3,4)) create a slice
containing only the last element of a b
a24 b0 10 changing b changed
a! a array( 1, 2, 10, 3, 4)
93
Fancy Indexing
INDEXING BY POSITION
INDEXING WITH BOOLEANS
mask array(0,1,1,0,0,1,0,0, ...
dtypebool) fancy indexing y
amask print y 10,20,50 using
compress y compress(mask, a) print
y 10,20,50
a arange(0,80,10) fancy indexing y
a1, 2, -3 print y 10 20 50 using
take y take(a,1,2,-3) print y 10 20
50
94
Fancy Indexing in 2D
a(0,1,2,3,4),(1,2,3,4,5) array( 1, 12, 23,
34, 45) a3,0, 2, 5 array(30, 32,
35, 40, 42, 45) 50, 52,
55) mask array(1,0,1,0,0,1,
dtypebool) amask,2 array(2,22,52)
Unlike slicing, fancy indexing creates copies
instead of views into original arrays.
95
Indexing with None
None is a special index that inserts a new axis
in the array at the specified location. Each
None increases the arrays dimensionality by 1.
a
1 X 3
3 X 1
3 X 1 X 1
y aNone, shape(y) (1, 3)
y a,None shape(y) (3, 1)
y a,None, None shape(y) (3, 1, 1)
96
Fancy Indexing with indices
INDEXING BY POSITION
create an Nx3 colormap grayscale map -- R
G B cmap array(1.0,1.0,1.0,
0.9,0.9,0.9, ...
0.0,0.0,0.0)
cmap.shape (10,3) img array(0,10,
5,1 ) img.shape (2,2) use
the image as an index into the colormap.
rgb_img cmapimg rgb_img.shape (2,2,3)
97
3D Example
MULTIDIMENSIONAL
Retreive two slices from a 3D cube via
indexing. y a,,2,-2 The take()
function also works. y take(a,2,-2,
axis2)
a
y
98
Where (version 1)
WHERE
Find the indices in array where expression
is True. a array(0, 12, 5, 20)
a10 array(False, True, False, True,
dtypebool) where(a10) array(1, 3)
99
Flattening Arrays
a.flatten()
a.flat
a.flatten() converts a multi-dimensional array
into a 1D array. The new array is a copy of the
original data.
a.flat is an attribute that returns an iterator
object that accesses the data the
multi-dimensional array data as a 1D array. It
references the original memory.
Create a 2D array a array(0,1,
2,3) Flatten out elements to 1D
b a.flatten() b array(0,1,2,3)
Changing b does not change a b0 10
b array(10,1,2,3) a array(0, 1, 2,
3)
a.flat
a.flat array(0,1,2,3) b a.flat
b0 10 a array(10, 1, 2, 3)
changed!
no change
100
(Un)raveling Arrays
a.ravel()
a.ravel()
a.ravel() is the same as a.flatten(), but it
returns a reference (or view) of the array if it
is possible (ie. the memory is contiguous).
Otherwise the new array copies the data.
Create a 2D array a array(0,1,
2,3) Transpose array so memory
layout is no longer contiguous aa
a.transpose() aa array(0, 2, 1,
3) ravel will create a copy of data b
aa.ravel() array(0,2,1,3) changing b doesnt
change a. b0 10 b array(10,1,2,3)
a array(0, 1, 2, 3)
Create a 2D array a array(0,1,
2,3) Flatten out elements to 1D
b a.ravel() b array(0,1,2,3)
Changing b does change a b0 10
b array(10,1,2,3) a array(10, 1,
2, 3)
changed!
101
Reshaping Arrays
SHAPE AND RESHAPE
RESHAPE
a arange(6) a array(0, 1, 2, 3, 4,
5) a.shape (6,) reshape array inplace to
2x3 a.shape (2,3) a array(0, 1, 2,
3, 4, 5)
return a new array with a different shape
a.reshape(3,2) array(0, 1, 2, 3,
4, 5) reshape cannot change the number
of elements in an array. a.reshape(4,2) Valu
eError total size of new array must be
unchanged
102
Transpose
TRANSPOSE
TRANSPOSE RETURNS VIEWS
a array(0,1,2, ...
3,4,5) a.shape (2,3) Transpose swaps the
order of axes. For 2D this swaps rows and
columns a.transpose() array(0, 3,
1, 4, 2, 5) The .T attribute is
equivalent to transpose() a.T array(0, 3,
1, 4, 2, 5)
b a.T changes to b alter a b0,1
30 a array( 0, 1, 2, 30, 4,
5) Transpose does not move values around
in memory. It only changes the order of
strides in the array a.strides (12, 4)
a.T.strides (4, 12)
TRANSPOSE AND STRIDES
103
Squeeze
SQUEEZE
a array(1,2,3, ...
4,5,6) a.shape (2,3) insert an extra
dimension a.shape (2,1,3) a array(0,
1, 2, 3, 4, 5) squeeze removes
any dimension with length1 a.squeeze()
a.shape (2,3)
104
Diagonals
DIAGONAL
DIAGONALS WITH INDEXING
a array(11,21,31, ...
12,22,32, ... 13,23,33)
Extract the diagonal from an array.
a.diagonal() array(11, 22, 33) Use offset to
move off the main diagonal.
a.diagonal(offset1) array(21, 32)
Fancy indexing also works. i
0,1,2 ai,i array(11, 22, 33)
Indexing can also be used to set diagonal
values ai,i 2 i array(0,1)
upper diagonal ai,i1 1 lower
diagonal ai1,i -1 a array( 2, 1,
13, -1, 2, 1, 31, -1, 2)
105
Complex Numbers
COMPLEX ARRAY ATTRIBUTES
CONJUGATION
a array(11j,1,2,3) array(1.1.j,
2.0.j, 3.0.j, 4.0.j)
a.dtype dtype(complex128) real and imaginary
parts a.real array( 1., 2., 3., 4.)
a.imag array( 1., 0., 0., 0.) set
imaginary part to a different set of
values. a.imag (1,2,3,4)
a array(1.1.j, 2.2.j, 3.3.j, 4.4.j)
a.conj() array(0.-1.j, 1.-2.j, 2.-3.j,
3.-4.j)
FLOAT (AND OTHER) ARRAYS
a array(0.,1,2,3) .real and .imag
attributes are available. a.real array(
0., 1., 2., 3.) a.imag array( 0., 0., 0.,
0.) But .imag is read-only. a.imag
(1,2,3,4) TypeError array does not have
imaginary part to set
106
Array Constructor Examples
FLOATING POINT ARRAYS DEFAULT TO DOUBLE PRECISION
UNSIGNED INTEGER BYTE
a array(0,1,2,3, ...
dtypeuint8) a.dtype dtype(uint8)
a.nbytes 4
a array(0,1.,2,3) a.dtype dtype(float
64) a.nbytes 32
notice decimal
REDUCING PRECISION
a array(0,1.,2,3, ...
dtypefloat32) a.dtype dtype(float32)
a.nbytes 16
107
Numpy dtypes
108
Type Casting
ASARRAY
ASTYPE
a array(1.5, -3, ...
dtypefloat64) a.astype(float32) array(
1.5, -3., dtypefloat32) a.astype(uint8) arr
ay( 1, 253,dtypeunit8) astype is
safe. It always returns a copy of the
array. b a.astype(float64) b0
2.0 a array(1.5, -3.)
a array(1.5, -3, ...
dtypefloat32) a array( 1.5, -3.,
dtypefloat32) upcast asarray(a,
dtypefloat64) array( 1.5,-3.) downcast
asarray(a, dtypeuint8) array( 1, 253,
dtypeuint8) asarray is efficient. It does
not make a copy if the type is the same.
b asarray(a, dtypefloat32) b0 2.0
a array(2.0,-3. )
109
Array Calculation Methods
SUM FUNCTION
SUM ARRAY METHOD
a array(1,2,3, 4,5,6,
float) Sum defaults to summing all all
array values. sum(a) 21. supply the
keyword axis to sum along the 0th axis.
sum(a, axis0) array(5., 7., 9.) supply the
keyword axis to sum along the last axis.
sum(a, axis-1) array(6., 15.)
The a.sum() defaults to summing all array
values a.sum() 21. Supply an axis argument
to sum along a specific axis.
a.sum(axis0) array(5., 7., 9.)
PRODUCT
product along columns. a.prod(axis0) array(
4., 10., 18.) functional form. prod(a,
axis0) array( 4., 10., 18.)
110
Min/Max
MIN
MAX
a array(2.,3.,0.,1.)
a.min(axis0) 0. use Numpys amin() instead
of Pythons builtin min() for speed operations
on multi-dimensional arrays. amin(a,
axis0) 0.
a array(2.,1.,0.,3.)
a.max(axis0) 3. functional form amax(a,
axis0) 3.
ARGMIN
ARGMAX
Find index of minimum value.
a.argmin(axis0) 2 functional form
argmin(a, axis0) 2
Find index of maximum value.
a.argmax(axis0) 1 functional form
argmax(a, axis0) 1
111
Statistics Array Methods
MEAN
STANDARD DEV./VARIANCE
a array(1,2,3, 4,5,6,
float) mean value of each column
a.mean(axis0) array( 2.5, 3.5, 4.5)
mean(a, axis0) array( 2.5, 3.5, 4.5)
average(a, axis0) array( 2.5, 3.5, 4.5)
average can also calculate a weighted
average average(a, weights1,2,...
axis0) array( 3., 4., 5.)
Standard Deviation a.std(axis0) array(
1.5, 1.5, 1.5) Variance
a.var(axis0) array(2.25, 2.25, 2.25)
var(a, axis0) array(2.25, 2.25, 2.25)
112
Other Array Methods
CLIP
ROUND
Limit values to a range a
array(1,2,3, 4,5,6,
float) Set values 5 equal to 5. a.clip(3,5) a array(
3., 3., 3., 4., 5., 5.)
Round values in an array. Numpy rounds to
even, so 1.5 and 2.5 both round to 2. a
array(1.35, 2.5, 1.5) a.round() array( 1.,
2., 2.) Round to first decimal place.
a.round(decimals1) array( 1.4, 2.5, 1.5)
POINT TO POINT
Calculate max min for array along
columns a.ptp(axis0) array( 3.0, 3.0,
3.0) max min for entire array.
a.ptp(axisNone) 5.0
113
Summary of (most) array attributes/methods
BASIC ATTRIBUTES
a.dtype Numerical type of array elements.
float32, uint8, etc. a.shape Shape of the
array. (m,n,o,...) a.size Number of elements in
entire array. a.itemsize Number of bytes used
by a single element in the array. a.nbytes
Number of bytes used by entire array (data
only). a.ndim Number of dimensions in the array.
SHAPE OPERATIONS
a.flat An iterator to step through array as if
it is 1D. a.flatten() Returns a 1D copy of a
multi-dimensional array. a.ravel() Same as
flatten(), but returns a view if
possible. a.resize(new_size) Change the
size/shape of an array in-place. a.swapaxes(axis1,
axis2) Swap the order of two axes in an array.
a.transpose(axes) Swap the order of any number
of array axes. a.T Shorthand for
a.transpose() a.squeeze() Remove any length1
dimensions from an array.
114
Summary of (most) array attributes/methods
FILL AND COPY
a.copy() Return a copy of the
array. a.fill(value) Fill array with a scalar
value.
CONVERSION / COERSION
a.tolist() Convert array into nested lists of
values. a.tostring() raw copy of array memory
into a python string. a.astype(dtype) Return
array coerced to given dtype. a.byteswap(False)
Convert byte order (big little endian).
COMPLEX NUMBERS
a.real Return the real part of the
array. a.Imag Return the imaginary part of the
array. a.conjugate() Return the complex
conjugate of the array. a.conj() Return the
complex conjugate of an array.(same as conjugate)
115
Summary of (most) array attributes/methods
SAVING
a.dump(file) Store a binary array data out to
the given file. a.dumps() returns the binary
pickle of the array as a string. a.tofile(fid,
sep"", format"s") Formatted ascii output to
file.
SEARCH / SORT
a.nonzero() Return indices for all non-zero
elements in a. a.sort(axis-1) Inplace sort of
array elements along axis. a.argsort(axis-1)
Return indices for element sort order along axis.
a.searchsorted(b) Return index where elements
from b would go in a.
ELEMENT MATH OPERATIONS
a.clip(low, high) Limit values in array to the
specified range. a.round(decimals0) Round to
the specified number of digits. a.cumsum(axisNone
) Cumulative sum of elements along
axis. a.cumpro
Write a Comment
User Comments (0)
About PowerShow.com