10  NumPy

A Python library for scientific computing. It features multidimensional array objects along with an assortment of functions optimized for fast computation. To use this library we first need to import it as shown below. After that we’ll have access to all the functions within the NumPy library. Let’s create a numpy array.

import numpy as np
a1 = np.array([1,2,3,4,5,6])
print(a1)
print(type(a1))
[1 2 3 4 5 6]
<class 'numpy.ndarray'>

Although the array looks like a list but it has very different properties and functions associated with it as compared to the python list. For example, there is no built-in function to change the shape of a list or to transpose a list whereas these operations can be done seemlessly with ndarrays. The shape function returns the dimensions of an ndarray as a tuple and the reshape function changes the shape of an ndarray given a tuple.

print("The shape of a1 array is", a1.shape)
m1 = a1.reshape(2,3)
print(m1)
print("The shape of m1 array is", m1.shape)
The shape of a1 array is (6,)
[[1 2 3]
 [4 5 6]]
The shape of m1 array is (2, 3)

The transpose and ravel functions can be used to transpose and flatten multidimensional arrays. For tranposition, we can also use .T attribute of a numpy array.

print(m1)
m1_transposed = m1.T
print(m1_transposed)
[[1 2 3]
 [4 5 6]]
[[1 4]
 [2 5]
 [3 6]]
print(m1)
m1_flattened = m1.ravel()
print(m1_flattened)
[[1 2 3]
 [4 5 6]]
[1 2 3 4 5 6]

There are various functions available in NumPy to create arrays e.g. zeros, ones, empty, random can be used to create an ndarray having all the values as zero, one, no value, or random values, respectively. These three functions take shape of the array as an argument and data type can be optionally specified. arange is another function that can be used to initialize an array with specific values. This function is similar to the range function with a difference that it return an array while the range function returns a list.

array_of_zeros = np.zeros((3,3))
print(array_of_zeros)
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
array1 = np.arange(0,10)
print(array1)
array2 = np.arange(0,10,2)
print(array2)
[0 1 2 3 4 5 6 7 8 9]
[0 2 4 6 8]

linspace is another useful function to generate specified number of equally spaced values within a given range. It takes three numbers as argument, the first two numbers specify the range (both number included) and the third number specifies the number of values to return.

array3 = np.linspace(0,3,5) 
print(array3)
array4 = np.linspace(1,10,4)
print(array4)
[0.   0.75 1.5  2.25 3.  ]
[ 1.  4.  7. 10.]

10.1 Reading data from file

Numpy has genfromtxt function that comes handy for reading data from text files. The example below show read a csv file using this function.

# %load test1.csv
1, 1, 1
2, 4, 8
3, 9, 27
4, 16, 64
5, 25, 125
inp_data = np.genfromtxt("test1.csv", delimiter=",")
print(inp_data)
print(inp_data.shape)
[[  1.   1.   1.]
 [  2.   4.   8.]
 [  3.   9.  27.]
 [  4.  16.  64.]
 [  5.  25. 125.]]
(5, 3)
sub_matrix = inp_data[1:4]
print(sub_matrix)
[[ 2.  4.  8.]
 [ 3.  9. 27.]
 [ 4. 16. 64.]]

10.2 Writing data to file

Numpy has savetxt function that can be used to save numpy arrays to a text file in e.g. csv format. There is an option to change the delimiter. The example below shows writing a csv file using this function with space as a delimiter. The fmt argument is used to specify the format in which to write the data.

np.savetxt("test2.csv",sub_matrix, delimiter=" ", fmt='%d')
# %load test2.csv
2 4 8
3 9 27
4 16 64

10.3 Slicing multidimensional arrays

Just like we can splice a string or list, slicing of numpy arrays can also be performed. While in case of a sting or a list the slice operator takes the start and end positions, in the case of ndarrays, the slice operator would take as many start and end combinations as the dimensions of the ndarray. Which means that slicing can be performed for each dimension of a multidimensional ndarray. In the example below, we first create a three dimensional array having values from 0 to 27. Note that the zeroth element of this 3D array is a 2D array (with values 0 to 8). Similarly, the first element of this 3D array is another 2D array (with values 9 to 17). Next, using slicing, we’ll print one of the number of from this 3D array.

array_3d = np.arange(0,27).reshape(3,3,3)
print(array_3d)
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
print(array_3d.shape)
print(array_3d[0,2,1])
(3, 3, 3)
7
The figure below shows the parsing of the slice operator for the 3D array.
<img src="ndarray_3D.PNG" width=50%, align="left">
print(array_3d[:,:,0])
[[ 0  3  6]
 [ 9 12 15]
 [18 21 24]]

Quiz: What would be the slice operator for array_3d to get the following output
[[[10 11]
  [13 14]]
 [[19 20]
  [22 23]]]

Show answer
print(array_3d[1:3,:2,1:3])

10.4 Combining arrays

The Numpy arrays can be combined using concatenate function. All the arrays that are to be combined must have same number of dimensions. The arrays to be joined as passed as a tuple and an axis can be specified to indicate the direction along which to join the arrays. The arrays can be joined along row-wise or column-wise by specifing axis as 0 or 1, respectively. Note that the dimensions for all the arrays along the concatenation axis must be identical. That is to say that, e.g., when combining two or more arrays along rows - the number of rows in all the arrays should be same.

print(np.arange(1,5))
[1 2 3 4]
arr1 = np.arange(1,5).reshape(2,2)
arr2 = np.array([[5,6]]) #note the square brackets

print(arr1.shape)
print(arr2.shape)
(2, 2)
(1, 2)
arr3 = np.concatenate((arr1,arr2), axis=0) 
print(arr3)
[[1 2]
 [3 4]
 [5 6]]

The above code won’t work with axis=1 because the number of rows in the two arrays is not same. We can transpose the arr2 and then concatenate it with arr1 along axis=1.

arr4 = np.concatenate((arr1, arr2.T), axis=1) #note the .T
print(arr4)
[[1 2 5]
 [3 4 6]]

Similar, output can be generated using the vstack and hstack functions as shown below.

print(np.vstack((arr1,arr2)))
print(np.hstack((arr1,arr2.T)))
[[1 2]
 [3 4]
 [5 6]]
[[1 2 5]
 [3 4 6]]

10.5 Broadcasting

When performing arithmetic operations with arrays of different sizes, numpy has a an efficient way of broadcasting the array with lower dimensions to match the dimensions of larger array. Broadcasting can be thought of as creating replicas of the original array. This vectorized array operation is a lot faster than conventional looping in Python.

arr1 = np.arange(1,5).reshape(2,2)
arr2 = np.array([[5,6]]) #note the square brackets
print(arr1)
print(arr2)
[[1 2]
 [3 4]]
[[5 6]]

When we multiply the two arrays (arr1 * arr2) the arr2 would be broadcasted such as its shape would change from (1,2) to (2,2). Now, with this broadcasted array the multiplication would be performed element-wise. Similarly, when with multiple a scaler with a ndarray, the scaler is broadcasted to match the dimensions of the ndarray followed by element-wise multiplication.

print(arr1*arr2) #arr2 would be broadcasted along rows
[[ 5 12]
 [15 24]]
print(arr1*arr2.T) #arr2 would be broadcasted along columns
[[ 5 10]
 [18 24]]
print(arr1*2)
[[2 4]
 [6 8]]

Quiz: Write a program to calculate cube of first 10 natural numbers.

Show answer
print(np.arange(1,11)**3)

This ability to broadcast open up lot of possibilities when working with martices. A simple example is shown be to print table for first ten natural numbers.

num1 = np.arange(1,11).reshape(1,10)
all_ones = np.ones((10,10), dtype=int)
table_10 = all_ones*num1*num1.T
print(table_10)
[[  1   2   3   4   5   6   7   8   9  10]
 [  2   4   6   8  10  12  14  16  18  20]
 [  3   6   9  12  15  18  21  24  27  30]
 [  4   8  12  16  20  24  28  32  36  40]
 [  5  10  15  20  25  30  35  40  45  50]
 [  6  12  18  24  30  36  42  48  54  60]
 [  7  14  21  28  35  42  49  56  63  70]
 [  8  16  24  32  40  48  56  64  72  80]
 [  9  18  27  36  45  54  63  72  81  90]
 [ 10  20  30  40  50  60  70  80  90 100]]
#Print as seen in math books
for x in num1[0]:
    for y in num1[0]:
        print(f"{x} X {y} = {table_10[x-1,y-1]}")
1 X 1 = 1
1 X 2 = 2
1 X 3 = 3
1 X 4 = 4
1 X 5 = 5
1 X 6 = 6
1 X 7 = 7
1 X 8 = 8
1 X 9 = 9
1 X 10 = 10
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
4 X 1 = 4
4 X 2 = 8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36
4 X 10 = 40
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70
8 X 1 = 8
8 X 2 = 16
8 X 3 = 24
8 X 4 = 32
8 X 5 = 40
8 X 6 = 48
8 X 7 = 56
8 X 8 = 64
8 X 9 = 72
8 X 10 = 80
9 X 1 = 9
9 X 2 = 18
9 X 3 = 27
9 X 4 = 36
9 X 5 = 45
9 X 6 = 54
9 X 7 = 63
9 X 8 = 72
9 X 9 = 81
9 X 10 = 90
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100