iPython Notebook for this tutorial is available here.

NumPy is a package for scientific computing in Python which provides basic functionalities to manipulate arrays and matrices.

Import Modules

import numpy as np

Arrays

arr1 = np.array([10, 20, 30, 40])
print "Array: %s" %arr1
print "Datatype: %s" %type(arr1)
Array: [10 20 30 40]
Datatype: <type 'numpy.ndarray'>
# comparison to lists in Python
list1 = [10, 20, 30, 40]
print "List: %s" %list1
print "Datatype: %s" %type(list1)
List: [10, 20, 30, 40]
Datatype: <type 'list'>
# Accessing Array Elements
print "First element of arr1: %d" %arr1[0]
print "Size of arr1: %s" %arr1.shape
print arr1[:2]
First element of arr1: 10
Size of arr1: 4
[10 20]
arr = np.arange(10)
print arr
[0 1 2 3 4 5 6 7 8 9]

Multidimensional Array

arr = np.array([[1, 2, 3], [4, 5, 6]], int)
print arr
[[1 2 3]
 [4 5 6]]
arr = np.random.random_integers(20, size = (3,3))
print arr
print "First element of arr: %d" %arr[0,0]
print "Size of arr: %s" %(arr.shape,)
[[ 3 18 17]
 [18  4 20]
 [ 2 20 16]]
First element of arr: 3
Size of arr: (3, 3)
arr_zeros = np.zeros((3,3))  
print arr_zeros
[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
arr_ones = np.ones((3,3))  
print arr_ones
[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]
arr_identity = np.eye(3, dtype = int)  
print arr_identity
[[1 0 0]
 [0 1 0]
 [0 0 1]]

Array Indexing

arr = np.random.random_integers(20, size = (3,3))
arr
arr[0,0]
6
arr[1,2]
14
print arr[0,0],arr[1,2]
6 14
print [arr[0,0],arr[1,2]] # outputs a list
[6, 14]
print arr[[0,1],[0,2]] # same output as previous command
[ 6 14]
print np.diag(arr)
[ 6 11  2]
# Boolean array indexing
b = (arr>10)
print arr
print b
print arr[b]
print arr[arr>10]
[[ 6  6 12]
 [13 11 14]
 [17  4  2]]
[[False False  True]
 [ True  True  True]
 [ True False False]]
[12 13 11 14 17]
[12 13 11 14 17]

Array Slicing

# rank 1 and rank 2 views of middle row
arr = np.random.random_integers(20, size = (3,3))
row1 = arr[1, :]    
row2 = arr[1:2, :] 
print row1, row1.shape  
print row2, row2.shape
[ 4 16  8] (3,)
[[ 4 16  8]] (1, 3)
arr_slice = arr[0:2,1:3] # assigned by reference
print arr
print arr_slice
arr_slice[0,0] = 0 # modifying arr_slice will modify arr
print arr_slice
print arr
[[12  1 10]
 [ 4 16  8]
 [20  5 20]]
[[ 1 10]
 [16  8]]
[[ 0 10]
 [16  8]]
[[12  0 10]
 [ 4 16  8]
 [20  5 20]]
# copy array to a new variable
arr = np.random.random_integers(20, size = (3,3))
print arr
arr_new = arr.copy() # new memory is allocated
arr_new[0,0] = 0 # modifying arr_new will not modify arr
print arr_new
print arr
[[ 6 10  4]
 [ 9 10  1]
 [15  2  3]]
[[ 0 10  4]
 [ 9 10  1]
 [15  2  3]]
[[ 6 10  4]
 [ 9 10  1]
 [15  2  3]]

Basic Mathematics

a = np.array([[1, 2, 3],[3, 4, 6]])
print a
b = np.array([[6, 7, 8],[4, 5, 3]])
print b
[[1 2 3]
 [3 4 6]]
[[6 7 8]
 [4 5 3]]
# element-wise multiplication 
print a*b
print np.multiply(a,b)
[[ 6 14 24]
 [12 20 18]]
[[ 6 14 24]
 [12 20 18]]
# element-wise division
print a/b
print np.divide(a,b)
[[0 0 0]
 [0 0 2]]
[[0 0 0]
 [0 0 2]]
# element-wise power operation
print np.power(a,2)
[[ 1  4  9]
 [ 9 16 36]]
# transpose
print a.transpose()
print a.T
[[1 3]
 [2 4]
 [3 6]]
[[1 3]
 [2 4]
 [3 6]]
# matrix multiplication
print np.dot(a, b.transpose())
[[44 23]
 [94 50]]
# concatanate columns of a and b 
print np.concatenate((a,b),1)
print np.hstack((a,b)) 
print np.c_[a,b]
[[1 2 3 6 7 8]
 [3 4 6 4 5 3]]
[[1 2 3 6 7 8]
 [3 4 6 4 5 3]]
[[1 2 3 6 7 8]
 [3 4 6 4 5 3]]
# concatanate rows of a and b 
print np.concatenate((a,b))
print np.vstack((a,b)) 
print np.r_[a,b]
[[1 2 3]
 [3 4 6]
 [6 7 8]
 [4 5 3]]
[[1 2 3]
 [3 4 6]
 [6 7 8]
 [4 5 3]]
[[1 2 3]
 [3 4 6]
 [6 7 8]
 [4 5 3]]
# find maximum elements
print a
print np.max(a) # maximum element in a
print np.max(a,0) # maximum element in each column
print np.max(a,1) # maximum element in each row
[[1 2 3]
 [3 4 6]]
6
[3 4 6]
[3 6]
# sum  
print a
print np.sum(a) # sum of all elements in a
print np.sum(a,0) # sum of elements in each column
print np.sum(a,1) # sum of elements in each row
[[1 2 3]
 [3 4 6]]
19
[4 6 9]
[ 6 13]
# repeat an array several times
print np.tile(a,(3,1))
print np.tile(a,(1,3))
print np.tile(a,(2,3))
[[1 2 3]
 [3 4 6]
 [1 2 3]
 [3 4 6]
 [1 2 3]
 [3 4 6]]
[[1 2 3 1 2 3 1 2 3]
 [3 4 6 3 4 6 3 4 6]]
[[1 2 3 1 2 3 1 2 3]
 [3 4 6 3 4 6 3 4 6]
 [1 2 3 1 2 3 1 2 3]
 [3 4 6 3 4 6 3 4 6]]
# reshape
print a
print a.shape
a1 = np.reshape(a,[1,6])
print a1
print a1.shape
[[1 2 3]
 [3 4 6]]
(2, 3)
[[1 2 3 3 4 6]]
(1, 6)
# inverse
c = np.array([[2,2,-1],[4,0,2],[0,6,-3]])
print c
print np.linalg.inv(c)
[[ 2  2 -1]
 [ 4  0  2]
 [ 0  6 -3]]
[[ 0.5         0.         -0.16666667]
 [-0.5         0.25        0.33333333]
 [-1.          0.5         0.33333333]]
# rank
print np.rank(c)
2
# svd
U, S, Vh = np.linalg.svd(c)
print S
[ 7.13218561  4.75726728  0.70734462]