Multidimensional numpy arrays¶

Creating arrays with more than one dimension¶

So far we have encountered numpy arrays that have one dimension - their elements are arranged as a list and they can be accessed using a single index:

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])

a[0] # get the 0-th element of the array


1

In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.

b = np.reshape(
a,     # the array to be reshaped
(2,3)  # dimensions of the new array
)

print(a)  # the original 1-dimensional array


[1 2 3 4 5 6]

print(b)  # the reshaped array


[[1 2 3] [4 5 6]]

b is a 2-dimensional array, with 2 rows and 3 columns. We can access its elements by specifying row and column indexes:

b[0,2] # get the element in  0-th row and 2-nd column


3

b[0,2] = 100
print(b)


[[ 1 2 100] [ 4 5 6]]

The numpy functions zeros(), ones(), and empty() can be also used to create arrays with more than one dimension:

c = np.ones((3,4))  # creates an array 3 rows and 4 columns
print(c)


[[ 1. 1. 1. 1.] [ 1. 1. 1. 1.] [ 1. 1. 1. 1.]]

Mathematical operations on multidimensional arrays¶

Mathematical operations on multidimensional arrays work similarly as for 1-dimensional arrays.

a = np.arange(4)
b = np.reshape(a, (2,2))
print(b)


[[0 1] [2 3]]

c = 10*b  # multiplication by a number
print(c)


[[ 0 10] [20 30]]

d = np.ones((2,2))

e = b+d  # addition of two arrays of the same dimensions
print(e)


[[ 1. 2.] [ 3. 4.]]

f = b*e  # multiplication of two arrays of the same dimensions
print(f)


[[ 0. 2.] [ 6. 12.]]

Notice that array multiplication multiplies corresponding elements of arrays. In order to perform matrix multiplication of 2-dimensional arrays we can use the numpy dot() function:

g = np.dot(b, e) # matrix multiplication of b and e
print(g)


[[ 3. 4.] [ 11. 16.]]

Mathematical functions defined by numpy can be applied to multidimensional arrays:

h = np.cos(g)  # compute cosine of all elements of the array g
print(h)


[[-0.9899925 -0.65364362] [ 0.0044257 -0.95765948]]

Slicing multidimensional arrays¶

In order to create a slice of a multidimensional array we need to specify which part of each dimension we want to select.

a = np.reshape(np.arange(30), (5,6)) # create a 5x6 array
print(a)


[[ 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 27 28 29]]

b = a[1:4, 0:2] #select elements in rows 1-3 and columns 0-1
print(b)


[[ 6 7] [12 13] [18 19]]

c = a[:3, 2:4] #select elements in rows 0-2 and columns 2-3
print(c)


[[ 2 3] [ 8 9] [14 15]]

d = a[:, 0] # select all elements in the 0-th column
print(d)


[ 0 6 12 18 24]

Note. a[i] is the same as a[i,:] i.e. it selects the i-th row of the array:

print(a[1])


[ 6 7 8 9 10 11]

Similarly as for 1-dimensional arrays slicing produces a view of the original array, and changing a slice changes the original array:

b = a[:3, :3]
print(b)


[[ 0 1 2] [ 6 7 8] [12 13 14]]

b[0,0] = 1000
print(b)


[[1000 1 2] [ 6 7 8] [ 12 13 14]]

print(a)


[[1000 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 27 28 29]]

We can use this to change many entries of an array at once

a[:4, :4] = 0 # set all entries of the slize to 0
print(a)


[[ 0 0 0 0 4 5] [ 0 0 0 0 10 11] [ 0 0 0 0 16 17] [ 0 0 0 0 22 23] [24 25 26 27 28 29]]