numerical python tom lefebvre. sept. 24-27, 2002numerical python2 since python is an interpreted...
TRANSCRIPT
Numerical PythonNumerical Python
Tom LeFebvre
Sept. 24-27, 2002 Numerical Python 2
Numerical PythonNumerical 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
Sept. 24-27, 2002 Numerical Python 3
Numerical PythonNumerical 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
Sept. 24-27, 2002 Numerical Python 4
Importing NumericImporting Numeric
You must import the Numeric module before using Numerical Python
import Numeric or
>>> from Numeric import *>>> from Numeric import *
Sept. 24-27, 2002 Numerical Python 5
Making Numeric ArraysMaking Numeric Arrays
>>> a = array ([0, 1, -2, 6, -5])>>> a = array ([0, 1, -2, 6, -5])>>> a>>> a
>>> b = arrayrange(10)>>> b = arrayrange(10)>>> b>>> b
Sept. 24-27, 2002 Numerical Python 6
Multi-dimensional ArraysMulti-dimensional Arrays
>>> a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])>>> a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> a>>> a
>>> a.shape>>> a.shape
Sept. 24-27, 2002 Numerical Python 7
Creating Special ArraysCreating Special Arrays
>>> BunchaOnes = ones((4, 4))>>> BunchaOnes = ones((4, 4))>>> BunchaOnes>>> BunchaOnes
>>> BunchaZeros = zeros((4, 4))>>> BunchaZeros = zeros((4, 4))>>> BunchaZeros>>> BunchaZeros
Sept. 24-27, 2002 Numerical Python 8
Manipulating ArraysManipulating Arrays
>>> BunchaThrees = BunchaOnes * 3>>> BunchaThrees = BunchaOnes * 3>>> BunchaThrees>>> BunchaThrees
>>> f = arrayrange(0, 100, 5)>>> f = arrayrange(0, 100, 5)>>> c = (f - 32) * 5 / 9>>> c = (f - 32) * 5 / 9>>> c>>> c
>>> c = (f - 32) * 5 / 9.0>>> c = (f - 32) * 5 / 9.0>>> c>>> c
Sept. 24-27, 2002 Numerical Python 9
Array SlicingArray Slicing
•Slicing Operator “[:]”Slicing Operator “[:]”
array[start : stop : increment]array[start : stop : increment]
array - a Numeric Python arrayarray - a Numeric Python arraystart - begin here including this indexstart - begin here including this indexstop - end here but do not include this indexstop - end here but do not include this indexincrement - skip this manyincrement - skip this many
Sept. 24-27, 2002 Numerical Python 10
Slicing ArraysSlicing Arrays
>>> a = arrayrange(10)>>> a = arrayrange(10)>>> a>>> a>>> a[2]>>> a[2]>>> a[0:4]>>> a[0:4]>>> a[-1]>>> a[-1]>>> a[:-1]>>> a[:-1]>>> a[2] = -999>>> a[2] = -999>>> a>>> a
Sept. 24-27, 2002 Numerical Python 11
Slicing Multi-dimensional ArraysSlicing Multi-dimensional Arrays
>>> b = reshape(arrayrange(9), (3, 3))>>> b = reshape(arrayrange(9), (3, 3))>>> b>>> b
>>> b[0, 0]>>> b[0, 0]
>>> b [2, 1]>>> b [2, 1]
>>> b[-1, -1]>>> b[-1, -1]
Sept. 24-27, 2002 Numerical Python 12
Slicing Multi-dimensional ArraysSlicing Multi-dimensional Arrays
>>> b>>> b>>> b[0:1, 0]>>> b[0:1, 0]
>>> b[0:2, 0:2]>>> b[0:2, 0:2]
>>> b [:]>>> b [:]
>>> b[::2]>>> b[::2]
>>> b[::-1]>>> b[::-1]
>>> b[::-1, ::-1]>>> b[::-1, ::-1]
Sept. 24-27, 2002 Numerical Python 13
Some Useful FunctionsSome Useful Functions
>>> a = arrayrange(10)>>> a = arrayrange(10)>>> a>>> a>>> add.reduce(a)>>> add.reduce(a)
Sept. 24-27, 2002 Numerical Python 14
Logical Functions - makes 0Logical Functions - makes 0ss or 1 or 1ss
>>> b = reshape(arrayrange(25), (5, 5))>>> b = reshape(arrayrange(25), (5, 5))>>> b>>> b
>>> less (b, 12)>>> less (b, 12)
>>> greater(b, 7)>>> greater(b, 7)>>> less_equal(b, 10)>>> less_equal(b, 10)
Sept. 24-27, 2002 Numerical Python 15
Array Functions - where()Array Functions - where()
where(condition, true value, false value)where(condition, true value, false value)
>>> b>>> b>>> c = where (less(b, 12), 0, b)>>> c = where (less(b, 12), 0, b)>>> c>>> c
Note: 0 is false, everything else is true
“where” is the Numerical Python “if” statement
Sept. 24-27, 2002 Numerical Python 16
Array Functions - clip()Array Functions - clip()
clip(array, min, max)clip(array, min, max)
>>> b>>> b>>> d = clip(b, 5, 15) >>> d = clip(b, 5, 15) >>> d>>> d