3. Object-oriented - Structure supports polymorphism, operation overloading and
multiple inheritance
Open source
Coherence - not hard to read, write and maintain
Portable
Runs on virtually every major platform
Dynamic typing
Built-in types and tools
3
4. Library utilities
Third party utilities
Numeric, NumPy, SciPy
Strong numeric processing capabilities: matrix operations
Suitable for probability and machine learning code
Automatic memory management
Linkable to components written in other languages
Linking to fast compiled code useful for computation intensive problems
Good for code steering and merging multiple programs in conflicting languages
Python/C integration quite common
4
5. Easy to use
Rapid turnaround - no intermediate compile and link steps as in C or C++
Programs compiled automatically to an intermediate form called byte-code,
which the interpreter then reads
Development speed of an interpreter without the performance loss inherent in
purely interpreted languages
Intuitive structure and syntax
Multi-purpose (Web, GUI, Scripting)
5
7. Python also an interpreter
Interpreter reads other Python programs and commands and executes them
Python programs are compiled automatically before being scanned into the interpreter
The fact that this process is hidden makes Python faster than a pure interpreter.
Type python into command line
$ sign - start of a terminal command line
# sign - a comment
>>> - start of a Python interpreter's command line
7
8. /usr/local/bin/python
#! /usr/bin/env python
interactive use
Python 3.0 (#1, Sep 24 2008, 20:40:45) [GCC 2.95.1 19990816 (release)] on sunos5
Copyright (c) 1995-2008 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-2008 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>>
python –c command [arg] ...
python –i script
read script first, then interactive
8
9. >>> print 'Hello world'
Hello world
# Relevant output is displayed on subsequent lines without the >>> symbol
>>> x = [0,1,2]
>>> x
[0,1,2]
>>> 2+3
5
Ctrl-D exits the interpreter
$
9
10. Python scripts written in text files with the suffix .py
Scripts can be read into the interpreter in several ways
$ python script.py
▪ # Executes the script and returns to the terminal
$ python -i script.py
▪ # Flag –i keeps interpreter open after script is executed
$ python #starts command line interpreter
>>> execfile('script.py')
▪ #execfile command reads script and executes them immediately as though it had been typed into the interpreter directly
$ python
>>> import script # DO NOT add the .py suffix. Script is a module here
▪ #Import command runs the script, displays any un-stored outputs and creates a lower level (or context) within the program
10
11. Assume script.py with following lines
print 'Hello world'
x = [0,1,2]
$ python script.py
Hello world
$ # Script is executed and interpreter is immediately closed. x is lost.
$ python -i script.py
Hello world
>>> x
[0,1,2]
x is stored and can be called later and interpreter is left open
11
12. >>> execfile('script.py')
Hello world
>>> x
▪ [0,1,2]
>>> # Identical to calling the script from terminal with command python -i script.py
$ python
>>> import script
Hello world
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
When script.py loaded in this way, x is not defined on the top level
12
13. To make use of x, let Python know which module it came from- provide context
>>> script.x
[0,1,2]
>>>
If script.py contains multiple stored quantities, to promote x (and only x) to the top level context
$ python
>>> from script import x
▪ Hello world
>>> x
▪ [0,1,2]
>>>
To promote all quantities in script.py to the top level context, type from script import * into the
interpreter. Of course, if that's what you want, you might as well type python -i script.py into the
terminal
13
14. Whitespace is meaningful in Python: especially indentation and placement of
newlines
Use a newline to end a line of code
Not a semicolon like in C++ or Java
Use when must go to next line prematurely
No braces { } to mark blocks of code in Python…
Use consistent indentation instead. The first line with a new indentation is considered
outside of the block.
Often a colon appears at the start of a new block
14
15. Comments starting with # – rest of line is ignored
Can include a “documentation string” as the first line of any new function or class
Development environment, debugger and other tools use it
def my_function(x, y):
“““This is the docstring.This
function does xyz”””
#The code would go here...
15
16. Dynamic Typing - Python determines the data types automatically
StrongTyping - Python’s not casual about types, it enforces them after it figures them out
Can not just append an integer to a string
First assignment to a variable creates it
Variable types need not be declared
Python figures out the variable types on its own
First convert the integer to a string itself
x = “the answer is ” # Decides x is string
y = 23 # Decides y is integer
print x + y # Python will complain about this
16
17. You can print a string to the screen using “print.”
Use % string operator in combination with print command to format output text
>>> print “%s xyz %d” % (“abc”, 34)
abc xyz 34
“Print” automatically adds a newline to the end of the string
A list of strings are concatenated with a space between them
>>> print “abc”
Abc
>>> print “abc”, “def”
abc def
17
18. Integers - 0, 1, 1234, -56
Dividing an integer by integer returns only the integer part - 7/2 yields 3
Arbitrarily long integers - must end in l or L - 999999999999999999999L
Floating point numbers - 0., 1.0, 1e10, 3.14e-2, 6.99E4
Division works normally - 7./2. = 3.5
18
19. Operations having floats and integers yields floats
6.4 – 2 = 4.4
Octal constants start with leading 0 - 0177, -01234
Hex constants start with leading 0x or 0X - 0x9ff, 0X7AE
Complex numbers must end in j or J
3+4j, 3.0+4.0j, 2J
Typing imaginary part first will return the complex number in the order
Re+ImJ
19
20. Arithmetic operations: a+b, a-b, a*b, a/b
Exponentiation: a**b
Other elementary functions but included in packages like NumPy and
SciPy
Comparison operators - a < b, a > b, a <= b, a >= b
Logical operators are words (and, or, not) not symbols (&&, ||, !)
20
21. Identity tests - a == b, a != b
Bitwise or: a | b
Bitwise exclusive or: a ^ b
Bitwise and: a & b
Shift a left or right by b bits: a << b, a >> b
21
22. Enclosed in single or double quotation marks
Unmatched ones can occur within the string “matt’s”
Use triple double-quotes for multi-line strings or strings than contain
both ‘ and “ inside of them - “““a‘b“c”””
Double quotation marks allow extending strings over multiple lines
without backslashes
'abc', “ABC”
22
23. Concatenation using + sign
>>> 'abc'+'def‘
'abcdef‘
Word = 'Help' 'a‘
Strings repeated using *
>>> 'abc'*3
'abcabcabc‘
Built-in methods for formatting
>>> “hello”.upper()
‘HELLO’
23
24. Indexing starts at 0 to end at len-1
>>> s = 'string‘
>>> s[1]
't’
s[i:j] fetches elements i (inclusive) through j (not inclusive)
>>> s[1:4]
'tri’
24
25. s[:j] fetches all elements up to, but not including j
>>> s[:3]
'str'
s[i:] fetches all elements from i onward (inclusive)
>>> s[2:]
'ring'
25
26. s[i:j:k] extracts every kth element starting with index i (inclusive) and
ending with index j (not inclusive)
>>> s[0:5:2]
'srn‘
Negative indexes - s[-1] means extract first element from end (s[len(s)-1])
>>> s[-1]
'g‘
>>> s[-2]
'n’
26
27. Contained in square brackets []
Members - numbers, strings, nested sub-lists or nothing
L1 = [0,1,2,3], L2 = ['zero', 'one'], L3 = [0,1,[2,3],'three',['four,one']], L4 = []
Indexing works same as strings
Mutable: individual elements can be reassigned in place
Members can grow and shrink in place
>>> L1 = [0,1,2,3]
>>> L1[0] = 4
>>> L1[0]
4
27
30. append(x)
extend(L) - append all items in list (likeTcl lappend)
insert(i,x)
remove(x)
pop([i]), pop() - create stack (FIFO), or queue (LIFO) pop(0)
index(x) - return the index for value x
count(x) - how many times x appears in list
sort() - sort items in place
reverse() - reverse list
30
31. filter(function, sequence)
def f(x): return x%2 != 0 and x%3 0
filter(f, range(2,25))
map(function, sequence)
call function for each item
return list of return values
reduce(function, sequence)
return a single value
call binary function on the first two items
then on the result and next item
iterate
31
32. Create lists without map(), filter(), lambda
= expression followed by for clause + zero or more for or of clauses
>>> vec = [2,4,6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [{x: x**2} for x in vec}
[{2: 4}, {4: 16}, {6: 36}]
32
33. Cross products
>>> vec1 = [2,4,6]
>>> vec2 = [4,3,-9]
>>> [x*y for x in vec1 for y in vec2]
[8,6,-18, 16,12,-36, 24,18,-54]
>>> [x+y for x in vec1 and y in vec2]
[6,5,-7,8,7,-5,10,9,-3]
>>> [vec1[i]*vec2[i] for i in
range(len(vec1))]
[8,12,-54]
can also use if
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [3*x for x in vec if x < 2]
[]
33
34. Remove by index
Remove slices from list (rather than by assigning an empty list)
>>> a = [-1,1,66.6,333,333,1234.5]
>>> del a[0]
>>> a
[1,66.6,333,333,1234.5]
>>> del a[2:4]
>>> a
[1,66.6,1234.5]
34
35. Contained in parentheses ()
Members - numbers, strings, nested
sub-tuples or nothing
t1 = (0,1,2,3), t2 = ('zero', 'one'),t3 =
(0,1,(2,3), 'three', ('four,one')), t4 = ()
IF not nesting tuples, can omit
parentheses - t1 = 0,1,2,3 is same as t1
= (0,1,2,3)
Indexing works string
Immutable: individual elements
cannot be reassigned in place
Concatenation
>>> t1 = (0,1,2,3); t2 = (4,5,6)
>>> t1+t2
(0,1,2,3,4,5,6)
Repetition
>>> t1*2
(0,1,2,3,0,1,2,3)
Length: len(t1) (also works for lists
and strings)
35
36. LikeTcl or awk associative arrays
Indexed by keys
Keys are any immutable type - tuples
But not lists (mutable!)
Uses 'key: value' notation
>>> tel = {'hgs' : 7042, 'lennox': 7018}
>>> tel['cs'] = 7000
>>> tel
36
37. No particular order
Delete elements with del
>>> del tel['foo']
Keys() method unsorted list of keys
>>> tel.keys()
['cs', 'lennox', 'hgs']
Use has_key() to check for existence
>>> tel.has_key('foo')
0
37
38. Speed – Array operations are much faster than on each list member
Loading in array capabilities
>>> from numpy import *
Create
>>> vec = array([1,2,3])
Create 3x3 matrix
>>> mat = array([[1,2,3],[4,5,6],[7,8,9]])
Initialize a dummy array
zeros((m,n), 'typecode')
Creates an m x n array of zeros
Can be integers, floats, double precision floats depending on type code
38
39. Similarities
Mutable: can have elements reassigned in place
indexed and sliced identically
len command works identically
Sort and reverse attributes
Differences
For arrays the + and * signs do not refer to concatenation or repetition
>>> ar1 = array([2,4,6])
>>> ar1+2 # Adding a constant to an array adds it to each member
▪ [4,6,8,]
>>> ar1*2
▪ [4,8,12,]
39
40. Adding two arrays - adding two vectors
>>> ar1 = array([2,4,6]); ar2 = array([1,2,3])
>>> ar1+ar2
▪ [3,6,9,]
Multiply two arrays – multiplies term by
term
>>> ar1*ar2
▪ [2,8,18,]
Division
>>> ar1/ar2
▪ [2,2,2,]
A function acting on an array acts on each
term in the array
>>> ar2**2
▪ [1,4,9,]
>>> ar3 = (pi/4)*arange(3) # like range, but
an array
>>> sin(ar3)
▪ [ 0. , 0.70710678, 1. ,]
40
41. Mutable types - dictionaries, lists, arrays - can have individual items reassigned in place
Immutable types - numbers, strings, tuples - cannot
>>> L = [0,2,3]
>>> L[0] = 1
>>> L
▪ [1,2,3]
>>> s = 'string'
>>> s[3] = 'o'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
41
42. Handle name assignments differently
If you assign a name to an immutable item, then set a second name equal to the first, changing the
value of the first name will not change that of the second
However, for mutable items, changing the value of the first name will change that of the second
>>> a = 2
>>> b = a # a and b are both numbers, and are thus immutable
>>> a = 3
>>> b
2
42
43. Even though we set b equal to a, changing the value of a
does not change the value of b.
However, for mutable types, this property does not hold.
>>> La = [0,1,2]
>>> Lb = La # La and Lb are both lists, and are thus mutable
>>> La = [1,2,3]
>>> Lb
▪ [1,2,3]
43
44. Setting Lb equal to La means that changing the value of La changes that
of Lb
To circumvent this property, we would make use of the function
copy.copy()
>>> La = [0,1,2]
>>> Lb = copy.copy(La)
Now changing the value of La will not change the value of Lb
44
45. Can check for sequence membership with is and is not
>>> if (4 in vec):
... print '4 is'
Chained comparisons - a less than b AND b equals c:
a < b == c
And, or are short-circuit operators
evaluated from left to right
stop evaluation as soon as outcome clear
45
46. Can assign comparison to variable
>>> s1,s2,s3='', 'foo', 'bar'
>>> non_null = s1 or s2 or s3
>>> non_null
▪ foo
UnlikeC, no assignment within expression
46
48. >>> if condition
... action
...
Subsequent indented lines are assumed to be part of the if statement
The same is true for most other types of python statements
A statement typed into an interpreter ends once an empty line is entered and a
statement in a script ends once an un-indented line appears
The same is true for defining functions
48
49. Can be combined with else if (elif) and else statements
if condition1: # if condition1 is true, execute action1
▪ action1
elif condition2: # if condition1 is not true, but condition2 is, execute
▪ action2 # action2
else: # if neither condition1 nor condition2 is true, execute
▪ action3 # action3
49
50. Combined using and & or
if condition1 and condition2
Action1
if condition1 or condition2:
action2
Condition operations - <, <=, >, >=, ==, !=, in
>>> x = 2; y = 3; L = [0,1,2]
>>> if (1<x<=3 and 4>y>=2) or (1==1 or 0!=1) or 1 in L:
... print 'Hello world‘
...
Hello world
50
51. >>> while condition
... action
...
>>>
>>> x = 1
>>> while x < 4:
... print x**2
... x = x+1
...
1 # only the squares of 1, 2, and 3 are printed, because
4 # once x = 4, the condition is false
9
51
52. for item i in set s:
action on item i
>>> for i in range(1,7):
... print i, i**2, i**3, i**4
...
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625
6 36 216 1296
52
53. >>> L = [0,1,2,3] # or, equivalently, range(4)
>>> for i in range(len(L)):
... L[i] = L[i]**2
...
>>> L
[0,1,4,9]
>>>
#CompactVersion
>>> L = arange(4)
>>> L = L**2
>>> L
[0,1,4,9,]
53
54. >>> L = [0,1,2,3] # or, equivalently, range(4)
>>> for i in range(len(L)):
... j = i/2.
... if j – int(j) == 0.0:
... L[i] = L[i]+1
... else: L[i] = -i**2
...
>>> L
[1,-1,3,-9]
>>>
54
55. def func(args):
return values
A function call must end with
parentheses
Functions may be simple one-to-
one mappings
>>> def f1(x):
... return x*(x-1)
...
>>> f1(3)
6
They may contain multiple input
and/or output variables
>>> def f2(x,y):
... return x+y,x-y
...
>>> f2(3,2)
(5,1)
55
56. Do not need to contain arguments at
all
>>> def f3():
... print 'Hello world'
...
>>> f3()
Hello world
Can set arguments to default values in
function definitions
>>> def f4(x,a=1):
... return a*x**2
...
>>>
Function is called with only one
argument, the default value of 1 is
assumed for the second argument
>>> f4(2)
4
However can change the second
argument from its default value
>>> f4(2,a=2) # f4(2,2) would also
work
8
56
58. Anything calculated inside a function but
not specified as an output quantity (either
with return or global) will be deleted once
the function stops running
>>> def f5(x,y):
... a = x+y
... b = x-y
... return a**2,b**2
...
>>> f5(3,2)
(25,1)
If we try to call a or b, we get an error
message
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'a' is not defined
58
59. Anonymous functions
may not work in older versions
def make_incrementor(n):
return lambda x: x + n
f = make_incrementor(42)
f(0)
f(1)
59
60. >>> help(execfile) # don't include parentheses when using the function name as
an argument
WARP has a similar library function called doc()
>>> from warp import *
>>> doc(execfile)
The main difference between help() and doc() is that doc() prints the relevant
documentation onto the interpreter screen.
60
61. Scoping hierarchy, in decreasing order of breadth
Built-in (Python)
Predefined names (len, open, execfile, etc.) and types
Global (module)
Names assigned at the top level of a module, or directly in the interpreter
Names declared global in a function
Local (function)
Names assigned inside a function definition or loop
61
62. >>> a = 2 # a is assigned in the interpreter, so it's global
>>> def f(x): # x is in the function's argument list, so it's local
... y = x+a # y is only assigned inside the function, so it's local
... return y # using the sa
...
>>>
62
63. If a module file is read into the interpreter via execfile, any quantities defined in
the top level of the module file will be promoted to the top level of the program
script.py:
print 'Hello world'
x = [0,1,2]
>>> execfile('script.py')
Hello world
>>> x
[0,1,2]
63
64. If we had imported script.py instead, the list x would not be defined on the top
level
To call x, we would need to explicitly tell Python its scope, or context.
>>> import script
Hello world
>>> script.x
[0,1,2]
If we had tried to call x without a context flag, an error message would have
appeared
64
65. Modules may well contain sub-modules
For a file named module.py which, in its definition, imports a sub-module named submodule, which in
turn contains some quantity named x.
>>> import module
If we load the module this way, we would type the following to call x:
>>> module.submodule.x
We can also import the sub-module without importing other quantities defined in module.py:
>>> from module import submodule
In this case, we would type the following to call x:
>>> submodule.x
We would also call x this way if we had read in module.py with execfile()
65
66. Can use the same names in different scopes
>>> a = 2
>>> def f5(x,y)
... a = x+y # this a has no knowledge of the global a, and vice-versa
... b = x-y
... return a**2,b**2
...
>>> a
2
66
67. The local a is deleted as soon as the function stops running
>>> x = 5
>>> import script # same script as before
Hello world
>>> x
5
>>> script.x # script.x and x are defined in different scopes, and
[0,1,2] # are thus different
67
68. Changing a global name used in a function definition changes the function
>>> a = 2
>>> def f(x):
... return x+a # this function is, effectively, f(x) = x+2
...
>>> f(4)
6
68
69. >>> a = 1
>>> f(4) # since we set a=1, f(x) = x+1 now
5
Unlike some other languages, Python function arguments are not modified by default:
>>> x = 4
>>> f(x)
5
>>> x
4
69
70. Collection of functions and variables, typically in scripts
Definitions can be imported
File name is module name + .py
Create module fibo.py
def fib(n): # write Fib. series up to n
...
def fib2(n): # return Fib. series up to n
70
71. Import module
import fibo
Use modules via "name space"
>>> fibo.fib(1000)
>>> fibo.__name__
▪ 'fibo'
can give it a local name
>>> fib = fibo.fib
>>> fib(500)
71
72. Function definition + executable statements
Executed only when module is imported
Modules have private symbol tables
Avoids name clash for global variables
Accessible as module.globalname
Can import into name space
>>> from fibo import fib, fib2
>>> fib(500)
Can import all names defined by module
>>> from fibo import *
72
73. Current directory
List of directories specified in PYTHONPATH environment variable
Uses installation-default if not defined - .:/usr/local/lib/python
Uses sys.path
>>> import sys
>>> sys.path
['', 'C:PROGRA~1Python2.2', 'C:Program FilesPython2.2DLLs', 'C:Program
FilesPython2.2lib', 'C:Program FilesPython2.2liblib-tk', 'C:Program
FilesPython2.2', 'C:Program FilesPython2.2libsite-packages']
73
74. Include byte-compiled version of module if there exists fibo.pyc in same directory
as fibo.py
Only if creation time of fibo.pyc matches fibo.py
Automatically write compiled file, if possible
Platform independent
Doesn't run any faster, but loads faster
Can have only .pyc file hide source
74
77. Mixture of C++ and Modula-3
Multiple base classes
Derived class can override any methods of its base class(es)
Method can call the method of a base class with the same name
Objects have private data
C++ terms
all class members are public
all member functions are virtual
no constructors or destructors (not needed)
77
78. Classes (and data types) are objects
Built-in types cannot be used as base classes by user
Arithmetic operators, subscripting can be redefined for class instances
(like C++, unlike Java)
78
80. Mapping from name to object
built-in names (abs())
global names in module
local names in function invocation
Attributes = any following a dot
z.real, z.imag
Attributes read-only or writable
module attributes are writeable
80
81. Scope = textual region of Python program where a namespace is directly
accessible (without dot)
innermost scope (first) = local names
middle scope = current module's global names
outermost scope (last) = built-in names
Assignments always affect innermost scope
don't copy, just create name bindings to objects
Global indicates name is in global scope
81
82. obj.name references (plus module!):
class MyClass:
"A simple example class"
i = 123
def f(self):
return 'hello world'
>>> MyClass.i
123
MyClass.f is method object
82
83. Class instantiation
>>> x = MyClass()
>>> x.f()
'hello world'
Creates new instance of class
note x = MyClass vs. x = MyClass()
___init__() special method for initialization of object
def __init__(self,realpart,imagpart):
self.r = realpart
self.i = imagpart
83
84. Attribute references
Data attributes (C++/Java data members)
created dynamically
x.counter = 1
while x.counter < 10:
x.counter = x.counter * 2
print x.counter
del x.counter
84
85. Called immediately
x.f()
can be referenced
xf = x.f
while 1:
print xf()
object is passed as first argument of function 'self'
x.f() is equivalent to MyClass.f(x)
85
86. Data attributes override method attributes with the same name
No real hiding not usable to implement pure abstract data types
Clients (users) of an object can add data attributes
First argument of method usually called self
'self' has no special meaning (cf. Java)
86
91. No real support, but textual replacement (name mangling)
__var is replaced by _classname_var
Prevents only accidental modification, not true protection
91
92. Packages
Numeric – good for numerical algebra, trigonometry, etc.CAUTION: no longer supported
NumPy – similar to Numeric, but handles arrays slightly differently and has a few other built-in commands and
functions
SciPy – useful for numerical integration,ODE solutions, interpolations, etc.: based on NumPy
Websites
http://docs.python.org - online version of built-in Python function documentation
http://laurent.pointal.org/python/pqrc - Python Quick Reference Card
http://rgruet.free.fr - long version of Python Quick Reference Card
http://mail.python.org - extensive Python forum
https://docs.python.org/2/tutorial/
92
94. Open-source add-on modules to Python
Common mathematical and numerical routines in pre-compiled, fast functions
Functionality similar to commercial software like MatLab
NumPy (Numeric Python) package - provides basic routines to manipulate large
arrays and matrices of numeric data
SciPy (Scientific Python) package – extends functionality of NumPy with a
substantial collection of useful algorithms, like minimization, Fourier
transformation, regression, and other applied mathematical techniques
94
95. Linear algebra – matrix inversion, decompositions...
Fast Fourier transforms
Masked arrays
Random number generation
Rich set of numerical data types
Fast
Loops mostly unnecessary
Operate on entire array
95
96. ndarray
A fast built-in N-dimensional homogeneous array object containing elements of same type
Elements can be C-structure or simple data-types
Fast algorithms on machine data-types (int, float...)
Universal functions
Element-by-element function objects
>>> a. = numpy.array([20,30,40,50])
>>> b = 10*numpy.sin(a)
>>>b
▪ Array([9.12945351, -9.88031624, 7.4511316, -2.62374854])
96
97. Can be any arbitrary structure specified using data-
type
Every dimension is accessed by stepping (striding) a
fixed number of bytes through memory
If memory is contiguous, then the strides are pre-
computed indexing-formula
Indexing starts at 0
Dimensions are called axes
97
99. Create
>>> a = array([0,1,2,3])
array([0, 1, 2, 3])
Check type
>>> type(a)
<type 'array'>
Numeric type of element
>>> a.dtype
dtype(‘int32’)
Bytes per element
>>> a.itemsize
4
Shape - returns aTuple listing the
length of array along each dimension
>>> a.shape
(4,)
>>> shape(a)
(4,)
99
100. Size – reports entire number of
elements in an array
>>> a.size
4
>>> size(a)
4
GET / SET elements
>>> a[1,3]
13
>>> a[1,3] = -1
array([[ 0, 1, 2, 3], [10,11,12,-1]])
Address first row using index
>>> a[1]
array([10, 11, 12, -1])
100
101. Memory usage - returns number of
bytes
>>> a.nbytes
12
Dimensions
>>> a.ndim
1
Create a copy of the array
>>> b = a.copy()
array([0, 1, 2, 3])
Convert numpy array to a python list
>>> a.tolist()
[0, 1, 2, 3]
101
102. For 1D arrays, list works similarly but
slow
>>> list(a)
[0, 1, 2, 3]
Indexing
>>> a[0]
0
>>> a[1] = 10
>>> a
[10, 1, 2, 3]
Test if values are present in an array
>>> a = np.array([[1, 2, 3], [4, 5, 6]],
float)
>>> 2 in a
True
102
103. Fill - set all values in an array
>>> a.fill(0)
[0, 0, 0, 0]
This also works, but slow
>>> a[:] = 1
[1, 1, 1, 1]
dtype property tells type of values
stored by the array
>>> a.dtype
dtype('int32')
Assigning a float to int32 array
truncates decimal part
>>> a[0] = 10.6
[10, 1, 2, 3]
103
104. Fill behaves same
>>> a.fill(-4.8)
[-4, -4, -4, -4]
Arrays can be reshaped using tuples that specify new dimensions
Turn a ten-element one-dimensional array into a two-dimensional one whose
first axis has five elements and second axis has two
104
107. Memory model allows simple indexing (integers and slices) to be a view of the same data
>>> b = a[:,::2]
>>> b[0,1] = 100
>>> print a
[[ 1. 2. 100.]]
[ 4. 5. 6.]]
>>> c = a[:,::2].copy()
>>> c[1,0] = 500
>>> print a
[[ 1. 2. 100.]]
[ 4. 5. 6.]]
107
108. References to memory in array
Changing values in a slice also changes array
>>> a = array((0,1,2,3,4))
Create a slice containing only the last element of a
>>> b = a[2:4]
>>> b[0] = 10
>>> a #changing b changes a
array([ 1, 2, 10, 3, 4])
108
109. INDEXING BY POSITION
>>> a = arrange(0,80,10)
fancy indexing
>>> y = a[[1, 2, -3]]
>>> print y
[10 20 50]
using take
>>> y = take(a,[1,2,-3])
>>> print y
[10 20 50]
INDEXINGWITH BOOLEANS
>>> mask = array([0,1,1,0,0,1,0,0],dtype = bool)
fancy indexing
>>> y = a[mask]
>>> print y
[10,20,50]
using compress
>>> y = compress(mask, a)
>>> print y
[10,20,50]
109
111. 21 built-in static data-type objects
Provides details of how to interpret the memory for an item
An instance of a single dtype class
Every object has a type attribute which provides the Python object returned
when an element is selected from the array
111
112. New dynamic data-type objects created to handle
Alteration of the byte-order
Change in the element size for string, unicode and void built-ins
Addition of fields
Change of the type object (C-structure arrays)
Creation is flexible
New user-defined built-in data-types can be added
but must be done in C and involves filling a function-pointer table
112
113. An item can include fields of different data types
A field is described by a data-type object and a byte offset
allows nested records
Array construction command interprets tuple elements as field entries
>>> dt = N.dtype(“i4,f8,a5”)
>>> print dt.fields
{'f1': (dtype('<i4'), 0), 'f2': (dtype('<f8'), 4), 'f3': (dtype('|S5'), 12)}
>>> a = N.array([(1,2.0,”Hello”), (2,3.0,”World”)], dtype=dt)
>>> print a['f3']
[HelloWorld]
113
114. >>> a = array([[1,2,3], [4,5,6]], float)
>>> sum(a) #summing all array values
21
Keyword axis used to sum along the 0th axis
>>> sum(a, axis=0)
array([5., 7., 9.])
Keyword axis to sum along the last axis
>>> sum(a, axis=-1)
array([6., 15.])
>>> a.sum() # Sum array method
21
114
115. Axis argument to sum along a specific axis
>>> a.sum(axis=0)
array([5., 7., 9.])
Product along columns
>>> a.prod(axis=0)
array([ 4., 10., 18.])
Functional form
>>> prod(a, axis=0)
array([ 4., 10., 18.])
115
116. Min
>>> a = array([2.,3.,0.,1.])
>>> a.min(axis=0)
0
Use amin() instead of Python’s built-in min() for speed operations on multi-dimensional arrays
>>> amin(a, axis=0)
0
ArgMin - Find index of minimum value
>>> a.argmin(axis=0)
2
functional form
>>> argmin(a, axis=0)
2
116
117. Max
>>> a = array([2.,1.,0.,3.])
>>> a.max(axis=0)
3
functional form
>>> amax(a, axis=0)
3
ArgMax - Find index of maximum value
>>> a.argmax(axis=0)
1
functional form
>>> argmax(a, axis=0)
1
117
118. >>> a = array([[1,2,3], [4,5,6]], float)
Mean value of each column
>>> a.mean(axis=0)
array([ 2.5, 3.5, 4.5])
>>> mean(a, axis=0)
array([ 2.5, 3.5, 4.5])
>>> average(a, axis=0)
array([ 2.5, 3.5, 4.5])
Weighted average
>>> average(a, weights=[1,2],
... axis=0)
array([ 3., 4., 5.])
118
120. Clip - limit values to a range
>>> a = array([[1,2,3], [4,5,6]], float)
set values < 3 equal to 3 and values > 5 equal to 5
>>> a.clip(3,5)
>>> a
array([[ 3., 3., 3.], [ 4., 5., 5.]])
Round values in an array. Rounds to even, so 1.5 and 2.5 round to 2
>>> a = array([1.35, 2.5, 1.5])
>>> a.round()
array([ 1., 2., 2.])
120
121. Round to first decimal place
>>> a.round(decimals=1)
array([ 1.4, 2.5, 1.5])
Point to point calculate max – min for array along columns
>>> a.ptp(axis=0)
array([ 3.0, 3.0, 3.0])
Max – min for entire array
>>> a.ptp(axis=None)
5.0
121
122. a.dtype – Numerical type of array elements - float32, uint8
a.shape – shape of the array. (m,n,o,...)
a.size – elements in array
a.itemsize – bytes used by a single element in the array
a.nbytes – bytes used by entire array (data only)
a.ndim – dimensions in the array
122
123. a.flat – 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 order of two axes in an array
a.transpose(*axes) – swap order of any number of array axes
a.T – a.transpose()
a.squeeze() – remove any length=1 dimensions from an array
123
124. a.copy() – copy of the array
a.fill(value) – fill array with a scalar value
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
a.real – return real part of the array
a.imag – return imaginary part of the array
a.conjugate() – return complex conjugate of the array
a.conj()– return the complex conjugate of an array
124
125. a.dump(file) – store binary array data to file
a.dumps() – returns the binary pickle of the array as a string
a.tofile(fid, sep="", format="%s") - formatted ascii output to file
a.nonzero() – return indices for all non-zero elements in a
a.sort(axis=-1) – in-place 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
a.clip(low, high) – limit values in array to the specified range
a.round(decimals=0) – round to the specified number of digits
a.cumsum(axis=None) – cumulative sum of elements along axis
a.cumprod(axis=None) – cumulative product of elements along axis
125
126. These methods reduce size of the array by 1 dimension by carrying out an
operation along the specified axis
If axis is none, operation is carried out across the entire array
a.sum(axis=None) – sum up values along axis
a.prod(axis=None) – product of all values along axis
a.min(axis=None)– minimum value along axis.
a.max(axis=None) – maximum value along axis
a.argmin(axis=None) – index of the minimum value along axis
126
127. a.argmax(axis=None) – index of the maximum value along axis
a.ptp(axis=None) – a.max(axis) – a.min(axis)
a.mean(axis=None) – mean average value along axis
a.std(axis=None) – standard deviation along axis
a.var(axis=None) – variance along axis
a.any(axis=None) –True if any value along axis is non-zero
a.all(axis=None) –True if all values along axis are non-zero
127
128. Simple array math
>>> a = array([1,2,3,4])
>>> b = array([2,3,4,5])
>>> a + b
array([3, 5, 7, 9])
NumPy defined constants
pi = 3.14159265359
e = 2.71828182846
128
129. >>> x = arange(11.) #Create array
from 0 to 10
Multiply entire array by scalar value
>>> a = (2*pi)/10
>>> a
0.62831853071795862
>>> a*x
array([ 0.,0.628,…,6.283])
In-place operations
>>> x *= a
>>> x
array([ 0.,0.628,…,6.283])
Apply functions to array
>>> y = sin(x)
129
130. Objects that rapidly evaluate a function element-by-element over an array
Core piece is a 1-d loop written in C that performs the operation over the largest
dimension of the array
For 1-d arrays - much faster than list comprehension
>>> type(N.exp)
<type 'numpy.ufunc'>
>>> x = array([1,2,3,4,5])
>>> print N.exp(x)
[ 2.71828183 7.3890561 20.08553692 54.59815003 148.4131591 ]
>>> print [math.exp(val) for val in x]
[2.7182818284590451, 7.3890560989306504,20.085536923187668,
54.598150033144236,148.4131591025766]
130
131. Mathematic, comparative, logical, and bitwise operators with two
arguments (binary operators) have special methods that operate on
arrays
op.reduce(a,axis=0)
op.accumulate(a,axis=0)
op.outer(a,b)
op.reduceat(a,indices)
131
132. a + b add(a, b)
a - b subtract(a, b)
a % b remainder(a, b)
a * b multiply(a, b)
a / b divide(a, b)
a ** b power(a, b)
Multiply by a scalar
>>> a = array((1,2))
>>> a*3
▪ array([3., 6.])
Element by element addition
>>> a = array([1,2])
>>> b = array([3,4])
>>> a + b
▪ array([4, 6])
Addition using operator function
>>> add(a, b)
▪ array([4, 6])
In place operation - overwrite contents of a -
saves array creation overhead
>>> add(a, b, a) # a += b
▪ array([4, 6])
>>> a
▪ array([4, 6])
132
136. Describes how NumPy treats arrays with different shapes during arithmetic
operations
Smaller array is broadcast across the larger array so that they have compatible
shapes
All arrays are promoted to the same number of dimensions
136
+
30 30 30
20 20 20
10 10 10
0 0 0 0 1 2
=
3
4x3 4
mismatch!
137. Two dimensions are compatible when
they are equal, or
one of them is 1
If conditions not met exception thrown
ValueError: frames are not aligned
Size of the resulting array is the maximum size along each dimension of the input
arrays
137
139. >>> x = [1,2,3,4];
>>> y = [[10],[20],[30]]
>>> print N.add(x,y)
[[11 12 13 14]
[21 22 23 24]
[31 32 33 34]]
x has shape (4,). Ufunc sees it as having shape
(1,4). y has shape (3,1).The ufunc result has
shape (3,4)
>>> x = array(x)
>>> y = array(y)
>>> print x+y
▪ [[11 12 13 14]
▪ [21 22 23 24]
▪ [31 32 33 34]]
>>> a = array((0,10,20,30))
>>> b = array((0,1,2))
>>> y = a[:, None] + b
139
+
30
20
10
0 0 1 2
=
30 31 32
20 21 22
10 11 12
0 1 2
140. Optimization
Data fitting
Interpolation
Spatial analysis
Clustering
Signal and image processing
Sparse matrices
Statistics
Integration
Determining a function’s maxima or
minima
Eigenvectors for large sparse matrices
Testing if two distributions are the
same...
140
141. Fftpack - Fast Fourier transform
Integrate - integration routines
Interpolate - Routines and classes for interpolation objects that can be used with
discrete numeric data
Linalg - linear algebra routines - inverse, determinant, solving a linear system of
equations, computing norms and pseudo/generalized inverses, eigenvalue /
eigenvector decomposition, singular value decomposition, LU decomposition,
Cholesky decomposition, QR decomposition, Schur decomposition
141
142. Optimize - Linear regression - finding a function’s minimum and maximum
values, determining the root of a function , finding where two functions
intersect...
Signal - convolution, correlation, finite Fourier transforms, B-spline smoothing,
filtering
Sparse - sparse matrices
Stats - functions for statistical distributions
IO data - input and output
Special - definitions of many usual math functions
Weave - C/C++ integration
142
143. 2D graphics library for Python that produces publication quality figures
Graphics in R are much easier to construct, and require less code
Graphics in matplotlib may look nicer; require much more code
143
144. Much larger community (SciPy + Python).
Better, and cleaner graphics
Sits on atop a programming language rather than being one
Dictionary data type is very useful (Python)
Memory mapped files and extensive sparse matrix packages
144
145. Documentation, Examples and cookbook http://www.numpy.org
Documentation, Examples and cookbook http://www.scipy.org3
http://stackoverflow.com/
http://matplotlib.org/
http://www.scipy.org/install.html
145