An array is a list or collection of homogenous elements, i.e., same type of items. An N-dimensional array is a collection of such arrays, and in simplest terms can be described as an array of arrays.
A two dimensional array, also called a matrix (plural: matrices), is very common and most of us would be familiar with it. An array of matrices can be visualized as a 3 dimensional array. An array can be defined using the '.array' method of the numpy module. A range of functions such as dtype, shape, size, etc., are available to find out about various attributes of the array.
For more details, please refer to the documentation https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html of the ndarray method.
Looking at an example would help us understand in better detail.
import numpy as np
h Datatype of the array is: <U1 Shape of the array is: (3, 2, 3)
tdarray = np.array([[['1','2','3'],['a','b','c']],[['4','5','6'],['d','e','f']],[['7','8','9'],['g','h','i']]])
target = tdarray[2][1][1]
print(target,"\nDatatype of the array is: ",tdarray.dtype,"\nShape of the array is: ",tdarray.shape)
ref_tmp_var = False
try:
tdarray_ = np.array([[['1','2','3'],['a','b','c']],[['4','5','6'],['d','e','f']],[['7','8','9'],['g','h','i']]])
if target == 'h' and tdarray.dtype == tdarray_.dtype and tdarray.shape == tdarray_.shape:
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions.')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
True
It is not possible to physically represent a n-dimensional array (where n>3). However, it is simple to code them. Let us try.
# Manual creation - Bad example
Shape of the array is: (4, 3, 2, 2) 10
fdarray = np.array([[[[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,30],[31,32]],[[33,34],[35,36]]],[[[37,38],[39,40]],[[41,42],[43,44]],[[45,46],[47,48]]]])
target2 = fdarray[0][2][0][1]
print("Shape of the array is: ",fdarray.shape,"\n",target2)
ref_tmp_var = False
try:
fdarray_ = np.array([[[[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,30],[31,32]],[[33,34],[35,36]]],[[[37,38],[39,40]],[[41,42],[43,44]],[[45,46],[47,48]]]])
if fdarray.shape == fdarray_.shape and target2 == int('10'):
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions.')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
True