Title: Numerical Python
1Numerical Python
2Numerical Python
- Since Python is an interpreted language, we need
better performance particularly when crunching
numbers. - Advantages High-level language allows very fast
development - Disadvantages Not always intuitive
3Numerical Python
- Numerical Python operates on multiarrays
- One line of Numerical Python processes one or
more whole arrays, not just one number. - Currently NumPy is implemented as an extension
to Python. But soon it will be an intrinsic part
of Python
4Importing Numeric
- You must import the Numeric module before using
Numerical Python - import Numeric
- or
-
gtgtgt from Numeric import
5Making Numeric Arrays
gtgtgt a array (0, 1, -2, 6, -5) gtgtgt a
gtgtgt b arrayrange(10) gtgtgt b
6Multi-dimensional Arrays
- gtgtgt a array(1, 2, 3, 4, 5, 6, 7, 8,
9) - gtgtgt a
gtgtgt a.shape
7Creating Special Arrays
gtgtgt BunchaZeros zeros((4, 4)) gtgtgt BunchaZeros
gtgtgt BunchaOnes ones((4, 4)) gtgtgt BunchaOnes
8Manipulating Arrays
gtgtgt BunchaThrees BunchaOnes 3 gtgtgt BunchaThrees
gtgtgt f arrayrange(0, 100, 5) gtgtgt c (f - 32)
5 / 9 gtgtgt c
gtgtgt c (f - 32) 5 / 9.0 gtgtgt c
9Array Slicing
arraystart stop increment
array - a Numeric Python array start - begin here
including this index stop - end here but do not
include this index increment - skip this many
10Slicing Arrays
gtgtgt a arrayrange(10) gtgtgt a gtgtgt a2 gtgtgt
a04 gtgtgt a-1 gtgtgt a-1 gtgtgt a2 -999 gtgtgt a
11Slicing Multi-dimensional Arrays
gtgtgt b reshape(arrayrange(9), (3, 3)) gtgtgt b
gtgtgt b0, 0 gtgtgt b 2, 1 gtgtgt b-1, -1
12Slicing Multi-dimensional Arrays
gtgtgt b gtgtgt b01, 0
gtgtgt b02, 02 gtgtgt b
gtgtgt b2 gtgtgt b-1 gtgtgt b-1, -1
13Some Useful Functions
gtgtgt a arrayrange(10) gtgtgt a gtgtgt add.reduce(a)
14Logical Functions - makes 0s or 1s
gtgtgt b reshape(arrayrange(25), (5, 5)) gtgtgt b
gtgtgt less (b, 12)
gtgtgt greater(b, 7) gtgtgt less_equal(b, 10)
15Array Functions - where()
where is the Numerical Python if statement
where(condition, true value, false value)
gtgtgt b gtgtgt c where (less(b, 12), 0, b) gtgtgt c
Note 0 is false, everything else is true
16Array Functions - clip()
clip(array, min, max)
gtgtgt b gtgtgt d clip(b, 5, 15) gtgtgt d