Int246 L1
Int246 L1
2
Arrays – Numerical Python (Numpy)
The fundamental library needed for scientific computing with Python is called NumPy.
This Open Source library contains:
• a powerful N-dimensional array object
• advanced array slicing methods (to select array elements)
• convenient array reshaping methods
NumPy can be extended with C-code for functions where performance is highly time
critical. In addition, tools are provided for integrating existing Fortran code. NumPy is a
hybrid of the older NumArray and Numeric packages, and is meant to replace them both.
Numpy – Creating arrays
• From lists
– numpy.array
# as vectors from lists
>>> a = numpy.array([1,3,5,7,9])
>>> b = numpy.array([3,5,6,7,9])
>>> c = a + b
>>> print c
[4, 8, 11, 14, 18]
>>> type(c)
(<type 'numpy.ndarray'>)
>>> c.shape
(5,)
Numpy – Creating matrices
>>> l = [[1, 2, 3], [3, 6, 9], [2, 4, 6]] # create a list
>>> a = numpy.array(l) # convert a list to an array
>>>print(a)
[[1 2 3]
[3 6 9]
[2 4 6]]
>>> a.shape
(3, 3)
>>> print(a.dtype) # get type of an array
int64
# or directly as matrix
>>> M = array([[1, 2], [3, 4]])
>>> M.shape
(2,2)
>>> M.dtype
dtype('int64')
Numpy – Matrices use
>>> print(a)
[[1 2 3]
[3 6 9]
[2 4 6]]
>>> print(a[0]) # this is just like a list of lists
[1 2 3]
>>> print(a[1, 2]) # arrays can be given comma separated indices
9
>>> print(a[1, 1:3]) # and slices
[6 9]
>>> print(a[:,1])
[2 6 4]
>>> a[1, 2] = 7
>>> print(a)
[[1 2 3]
[3 6 7]
[2 4 6]]
>>> a[:, 0] = [0, 9, 8]
>>> print(a)
[[0 2 3]
[9 6 7]
[8 4 6]]
Numpy – Creating arrays
• Generation functions
>>> x = arange(0, 10, 1) # arguments: start, stop, step
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b = numpy.zeros(5)
>>> print(b)
[ 0. 0. 0. 0. 0.]
>>> b.dtype
dtype(‘float64’)
>>> n = 1000
>>> my_int_array = numpy.zeros(n, dtype=numpy.int)
>>> my_int_array.dtype
dtype(‘int32’)
>>> c = numpy.ones((3,3))
>>> c
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
Numpy – array creation and use
>>> d = numpy.arange(5) # just like range()
>>> print(d)
[0 1 2 3 4]
>>> M = numpy.random.rand(3,3)
>>> M
array([[ 0.84188778, 0.70928643, 0.87321035],
[ 0.81885553, 0.92208501, 0.873464 ],
[ 0.27111984, 0.82213106, 0.55987325]])
>>>
>>> numpy.save('saved-matrix.npy', M)
>>> numpy.load('saved-matrix.npy')
array([[ 0.84188778, 0.70928643, 0.87321035],
[ 0.81885553, 0.92208501, 0.873464 ],
[ 0.27111984, 0.82213106, 0.55987325]])
Numpy - ndarray
[ [ 1.5, 0.2, -3.7] , An array of rank 2 i.e. It has 2 axes, the first
[ 0.1, 1.7, 2.9] ] length 3, the second of length 3 (a matrix
with 2 rows and 3 columns
Numpy – ndarray attributes
• ndarray.ndim
– the number of axes (dimensions) of the array i.e. the rank.
• ndarray.shape
– the dimensions of the array. This is a tuple of integers indicating the size of the array in each
dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the
shape tuple is therefore the rank, or number of dimensions, ndim.
• ndarray.size
– the total number of elements of the array, equal to the product of the elements of shape.
• ndarray.dtype
– an object describing the type of the elements in the array. One can create or specify dtype's
using standard Python types. NumPy provides many, for example bool_, character, int_,
int8, int16, int32, int64, float_, float8, float16, float32, float64, complex_, complex64, object_.
• ndarray.itemsize
– the size in bytes of each element of the array. E.g. for elements of type float64, itemsize is 8
(=64/8), while complex32 has itemsize 4 (=32/8) (equivalent to ndarray.dtype.itemsize).
• ndarray.data
– the buffer containing the actual elements of the array. Normally, we won't need to use this
attribute because we will access the elements in an array using indexing facilities.
Numpy – array creation and use
Two ndarrays are mutable and may be views to the same memory:
>>> print(x)
[ 4.5 2.3 6.7 1.2 1.8 5.5]
>>> s = x.argsort()
>>> s
array([3, 4, 1, 0, 5, 2])
>>> x[s]
array([ 1.2, 1.8, 2.3, 4.5, 5.5, 6.7])
>>> y[s]
array([ 6.2, 7.8, 2.3, 1.5, 8.5, 4.7])
Numpy – array functions
>>> arr.sum()
45
>>> numpy.sum(arr)
45
>>> a.transpose()
array([[ 1., 3.],
[ 2., 4.]])
>>> inv(a)
array([[-2. , 1. ],
[ 1.5, -0.5]])
>>> u
array([[ 1., 0.],
[ 0., 1.]])
The correlation coefficient for multiple variables observed at multiple instances can be
found for arrays of the form [[x1, x2, …], [y1, y2, …], [z1, z2, …], …] where x, y, z are
different observables and the numbers indicate the observation times:
>>> a = np.array([[1, 2, 1, 3], [5, 3, 1, 8]], float)
>>> c = np.corrcoef(a)
>>> c
array([[ 1. , 0.72870505],
[ 0.72870505, 1. ]])
Here the return array c[i,j] gives the correlation coefficient for the ith and jth
observables. Similarly, the covariance for data can be found::
>>> np.cov(a)
array([[ 0.91666667, 2.08333333],
[ 2.08333333, 8.91666667]])
Using arrays wisely