Numerical Python Tom LeFebvre
Numerical 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
Numerical 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
Importing Numeric >>> from Numeric import * You must import the Numeric module before using Numerical Python import Numeric or >>> from Numeric import * Sept. 24-27, 2002 Numerical Python
Making Numeric Arrays >>> a = array ([0, 1, -2, 6, -5]) >>> b = arrayrange(10) >>> b Sept. 24-27, 2002 Numerical Python
Multi-dimensional Arrays >>> a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> a >>> a.shape Sept. 24-27, 2002 Numerical Python
Creating Special Arrays >>> BunchaZeros = zeros((4, 4)) >>> BunchaZeros >>> BunchaOnes = ones((4, 4)) >>> BunchaOnes Sept. 24-27, 2002 Numerical Python
Manipulating Arrays >>> BunchaThrees = BunchaOnes * 3 >>> f = arrayrange(0, 100, 5) >>> c = (f - 32) * 5 / 9 >>> c >>> c = (f - 32) * 5 / 9.0 >>> c Sept. 24-27, 2002 Numerical Python
Array Slicing Slicing Operator “[:]” array[start : 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 Sept. 24-27, 2002 Numerical Python
Slicing Arrays >>> a = arrayrange(10) >>> a Sept. 24-27, 2002 Numerical Python
Slicing Multi-dimensional Arrays >>> b = reshape(arrayrange(9), (3, 3)) >>> b >>> b[0, 0] >>> b [2, 1] >>> b[-1, -1] Sept. 24-27, 2002 Numerical Python
Slicing Multi-dimensional Arrays >>> b >>> b[0:1, 0] >>> b[0:2, 0:2] >>> b [:] >>> b[::2] >>> b[::-1] >>> b[::-1, ::-1] Sept. 24-27, 2002 Numerical Python
Some Useful Functions >>> a = arrayrange(10) >>> a >>> add.reduce(a) Sept. 24-27, 2002 Numerical Python
Logical Functions - makes 0s or 1s >>> b = reshape(arrayrange(25), (5, 5)) >>> b >>> less (b, 12) >>> greater(b, 7) >>> less_equal(b, 10) Sept. 24-27, 2002 Numerical Python
Array Functions - where() “where” is the Numerical Python “if” statement where(condition, true value, false value) >>> b >>> c = where (less(b, 12), 0, b) >>> c Note: 0 is false, everything else is true Sept. 24-27, 2002 Numerical Python
Array Functions - clip() clip(array, min, max) >>> b >>> d = clip(b, 5, 15) >>> d Sept. 24-27, 2002 Numerical Python