Python Multi-dimensional Arrays

The 2D arrays, 3D arrays etc. are called multi-dimensional arrays. A 2D array contains more than 1 row and 1 column and it can be treated as a combination of several 1D arrays. A 2D array is also considered as a matrix. For example, a 2D array with 'm' rows and 'n' columns is called m x n matrix.

Creating multi-dimensional arrays:

We can create a multi-dimensional arrays using four ways, those are as follows.

  1. Using array() function
  2. Using ones() and zeroes() functions
  3. Using eye() function
  4. Using reshape() function

multi-dimensional array using array() function:

Numpy's array() function can be used to create a multidimensional array. Usually, we pass lists of elements to this function. If we pass one list of elements to this function, then it will create a 1D array. If we pass two lists of elements, then this function creates a 2D array.

a = array([1,2,3,4])

a = array([[1,2,3,4], [5,6,7,8]])

Displaying of 2D array elements

[[1 2 3 4]

[5 6 7 8]]

Even though the elements are displayed in 2 rows and 4 columns, the internal memory allocated to all these elements would be in the form of a single rowcontaining 8 blocks (2 x 4 = 8). The elements are stored in the contiguous memory locations as shown below.

Python Multi-dimensional Arrays

multi-dimensional array using ones() and zeros() functions:

The ones() function is useful to create a 2D array with several rows and columns where all the elements will be taken as 1. The format of this function is:

ones((r, c), dtype)

Here, 'r' represents the number of rows and 'c' represents the number of columns. 'dtype' represents the datatype of the elements in the array. For example,

a = ones((3, 4), float)

will create a 2D array with 3 rows and 4 columns and the datatype is taken as float. we can see the array as:

[[1. 1. 1. 1.]

[1. 1. 1. 1.]

[1. 1. 1. 1.]]

Just like the ones() function, we can also use the zeros() function to create a 2D array with elements filled with zeros.

b = zeros((3,4), int)

Then a 2D array with 2 rows and 4 columns will be created where all elements will be 0s, as shown below:

[[0 0 0 0]

[0 0 0 0]

[0 0 0 0]]

multi-dimensional array using eye() function:

The eye() function creates a 2D array and fills the elements in the diagonal with 1s . The general format of using this function is:

eye(n, dtype=datatype)

This will create an array with 'n' rows and 'n' columns. The default datatype is 'float'. For example, eye(3) will create a 3x3 array and fills the diagonal elements with 1s as shown below:

b =eye(3)

[[1. 0. 0.]

[0. 1. 0.]

[0. 0. 1.]]

CopiedCopy Code

from numpy import * 
b =eye(4)
print(b)

multi-dimensional array using reshape() function:

The reshape() function has been already discussed in the previous section. We will have an elaborate discussion about this function now. This function is useful to convert a 1D array into a multidimensional (2D or 3D) array. The syntax of writing this function is:

reshape(arrayname, (n, r, c))

Here, 'arrayname' represents the name of the array whose elements to be converted. 'n' indicates the number of arrays in the resultant array. 'r', 'c' indicates the number of rows and columns , respectively. For example, we take a 1D array 'a' with 6 elements as:

a = array([1, 2, 3, 4, 5, 6])

To convert 'a' into a 2D array using the reshape() function, we can write:

b = reshape(a, (2, 3))

We are converting the elements of the array 'a' into a 2D array with 2 rows and 3 columns, and the resultant array is 'b'. So, the 2D array 'b' looks like this:

[[1 2 3]

[4 5 6]]

Observe the starting two pairs of square brackets which indicate that it is a 2D array. Suppose, we write:

b = reshape(a, (3, 2))

This will convert 'a' into a 2D array with 3 rows and 2 columns that looks like this:

[[1 2]

[3 4]

[5 6]]

It is possible to use the reshape() function to convert a 1D array into a 3D array . Let's take a 1D array 'a' with 12 elements as:

a = arange(12)

[0 1 2 3 4 5 6 7 8 9 10 11]

Now, to convert this 1D array into a 3D array, we can use the reshape() function as:

b = reshape(a, (2, 3, 2))

b = reshape(a, (2, 3, 2))

Here, 'a' represents the arrayname that is being converted. In the reshape() function, after 'a', observe the figures (2, 3, 2). They represent that we want 2 arrays each with 3 rows and 2 columns . So, the resultant 3D array 'b' looks like this:

CopiedCopy Code
[ [ [ 0  1]
    [ 2  3]
    [ 4  5] ]
 [ [ 6  7]
   [ 8  9]
   [10 11] ] ]

So, 'b' is a 3D array of size 2x3x2. Suppose, we write:

b = reshape(a, (3, 2, 2))

CopiedCopy Code
[  [ [ 0  1]
      [ 2  3]  ]
[  [ 4  5]
      [ 6  7]  ]
[  [ 8  9]
    [10 11] ]  ]