3. Multidimensional Array in Numpy

Dimensions in an Array

In this session we are going to cover dimensions in arrays. A dimension in an array is how many levels of indexing you need to reach an element.

import numpy as np

What is a 1-dimensional array?

A simple list — only one index is needed to access an element.

a = [10, 20, 30]
arr = np.array(a)
arr[1]

Output:

np.int64(20)

Only one index is needed to reach a value. This is a single dimensional array. You can check number of dimensions of any array by ndim.

arr.ndim

Output:

1

What is a 2-dimensional array?

A nested list (lists in a list) — two indices are needed to access an element.

b = [[10, 20, 30], [40, 50, 60]]
arr = np.array(b)
arr

Output:

array([[10, 20, 30],
       [40, 50, 60]])
arr[0][0]

Output:

np.int64(10)
arr[0][1]

Output:

np.int64(20)

How to access a single row in a 2-dimensional array

The first index indicates the row and the second index indicates the column.

arr[0]

Output:

array([10, 20, 30])

How to access a single column

To access a column, you must give the row also (two indices).

arr[0, 0]

Output:

np.int64(10)
arr[0, 2]

Output:

np.int64(30)

Note: The first value is the row and the second value is the column.

How to perform slicing in a row

b = [[10, 20, 30],
     [40, 50, 60],
     [70, 80, 90],
     [30, 60, 50],
     [60, 70, 90]]
arr = np.array(b)
arr

Output:

array([[10, 20, 30],
       [40, 50, 60],
       [70, 80, 90],
       [30, 60, 50],
       [60, 70, 90]])

Get starting 3 rows (indices 0, 1, 2)

You can slice rows from your array.

arr[0:3]

Output:

array([[10, 20, 30],
       [40, 50, 60],
       [70, 80, 90]])

Get last 3 rows (indices 2, 3, 4)

arr[2:5]

Output:

array([[70, 80, 90],
       [30, 60, 50],
       [60, 70, 90]])

Or:

arr[2:]

Output:

array([[70, 80, 90],
       [30, 60, 50],
       [60, 70, 90]])

Get starting 2 rows and starting 2 columns

arr[0:2, 0:2]

Output:

array([[10, 20],
       [40, 50]])

Get starting 2 rows and last 2 columns

arr[0:2, 1:3]

Output:

array([[20, 30],
       [50, 60]])

Or:

arr[0:2, 1:]

Output:

array([[20, 30],
       [50, 60]])

Basic functions in multidimensional arrays

b = [[10, 20, 30],
     [40, 50, 60],
     [70, 80, 90],
     [30, 60, 50],
     [60, 70, 90],
     [90, 70, 20],
     [20, 50, 70]]
arr = np.array(b)
arr

Output:

array([[10, 20, 30],
       [40, 50, 60],
       [70, 80, 90],
       [30, 60, 50],
       [60, 70, 90],
       [90, 70, 20],
       [20, 50, 70]])

sum

It will add all the values in an array.

np.sum(arr)

Output:

np.int64(1130)

sum(axis=0) for columns

Here axis = 0 means operate along columns (i.e., sum per column).

np.sum(arr, axis=0)

Output:

array([320, 400, 410])

sum(axis=1) for rows

np.sum(arr, axis=1)

Output:

array([ 60, 150, 240, 140, 220, 180, 140])

max(axis=0) for columns

np.max(arr, axis=0)

Output:

array([90, 80, 90])

max(axis=1) for rows

np.max(arr, axis=1)

Output:

array([30, 60, 90, 60, 90, 90, 70])

min(axis=0) for columns

np.min(arr, axis=0)

Output:

array([10, 20, 20])

mean(axis=1) for rows

np.mean(arr, axis=1)

Output:

array([20.        , 50.        , 80.        , 46.66666667,
       73.33333333, 60.        , 46.66666667])

mean(axis=0) for columns

np.mean(arr, axis=0)

Output:

array([45.71428571, 57.14285714, 58.57142857])

reshape

The reshape function is used to reshape your array into desired numbers of rows and columns.

Reshape into 3 rows and 7 columns

new_arr = np.reshape(arr, (3, 7))
new_arr

Output:

array([[10, 20, 30, 40, 50, 60, 70],
       [80, 90, 30, 60, 50, 60, 70],
       [90, 90, 70, 20, 20, 50, 70]])

Reshape into 1 row and 21 columns

new_arr = np.reshape(arr, (1, 21))
new_arr

Output:

array([[10, 20, 30, 40, 50, 60, 70, 80, 90, 30, 60, 50, 60, 70, 90, 90, 70, 20, 20, 50, 70]])

Reshape into 21 rows and 1 column

new_arr = np.reshape(new_arr, (21, 1))
new_arr

Output:

array([[10],
       [20],
       [30],
       [40],
       [50],
       [60],
       [70],
       [80],
       [90],
       [30],
       [60],
       [50],
       [60],
       [70],
       [90],
       [90],
       [70],
       [20],
       [20],
       [50],
       [70]])

vtsack

vstack in NumPy is used to add values vertically. Suppose you need to add the average of each column in your array, you can use vstack.

column_mean = np.mean(arr, axis=0)
np.vstack((arr, column_mean))

Output:

array([[10.        , 20.        , 30.        ],
       [40.        , 50.        , 60.        ],
       [70.        , 80.        , 90.        ],
       [30.        , 60.        , 50.        ],
       [60.        , 70.        , 90.        ],
       [90.        , 70.        , 20.        ],
       [20.        , 50.        , 70.        ],
       [45.71428571, 57.14285714, 58.57142857]])

htsack

hstack in NumPy is used to add values horizontally. Suppose you need to add the average of each row in your array, you can use hstack.

row_mean = np.mean(arr, axis=1)
row_mean = np.reshape(row_mean, (7, 1))
new_arr = np.hstack((arr, row_mean))
new_arr

Output:

array([[10.        , 20.        , 30.        , 20.        ],
       [40.        , 50.        , 60.        , 50.        ],
       [70.        , 80.        , 90.        , 80.        ],
       [30.        , 60.        , 50.        , 46.66666667],
       [60.        , 70.        , 90.        , 73.33333333],
       [90.        , 70.        , 20.        , 60.        ],
       [20.        , 50.        , 70.        , 46.66666667]])

Assignments

  1. Create a Multidimensional array of 10 rows and 4 columns.

  2. Find the mean of each column and add the mean values in array

  3. Find the total of each column and add the total values in array

  4. find the mean of each row and add mean values in an array

  5. Reshape the array in all possible formats.

  6. Get the first two rows and last three columns from the array

  7. Get the last two rows and first three columns from the array

  8. Get the first column from the array

Last updated