Important Notice:

Course Content
NumPy Basic in Python (Chapter-13) M3-R5
About Lesson

3-D arrays

  • An array that has 2-D arrays (matrices) as its elements is called 3-D array.

  • These are often used to represent a 3rd order tensor.

  • Example

  • Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:

  • Program:

  • import numpy as np

    arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

    print(arr)

Access 3-D Arrays

  • To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element.

  • Program: Access the third element of the second array of the first array:

import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])

Output: arr[0, 1, 2] prints the value 6

Data Types in NumPy

  • NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.

  • Below is a list of all data types in NumPy and the characters used to represent them.

  • i – integer

  • b – boolean

  • u – unsigned integer

  • f – float

  • c – complex float

  • m – timedelta

  • M – datetime

  • O – object

  • S – string

  • U – unicode string

  • V – fixed chunk of memory for other type ( void)

error: Content is protected !!