An n-dimensional array is a fixed size multi-dimensional container. The items in the container are of the same type and size. The shape of the array is a tuple of n- integers that specify the sizes of each dimension. The shape of the array defines the number of dimensions and items in it. Data – type object defines the type of each item in that array. Using indexing or slicing in python, we can access the contents of an array. Additionally, with the help of methods and attributes of the array, we can access them too.
NumPy libraries help us with using arrays in Python with the help of array object called “ndarray”. Using array() function, we can create ndarray objects.
Using np.array() function in NumPy, we can pass in a nested list of values as an argument and create n-dimensional array. The outer list represents the rows of the array and the inner represent the columns of an array.
We can create various dimensional arrays using “ndarray”. Let’s see them all with an example.
1. A. Creating a 0-D array:
Syntax:
# creating 0-D array
import numpy as np
arr = np.array(53)
print(arr)
Output:
53
In this example above, we are importing numpy as np which is the python library used for arrays. We are creating a 0-dimensional array using np.array function. Then we print the created array.
B. Checking the dimension of array:
Using “[variablename].ndim” , find the dimension of an array.
Syntax:
# creating 0-D array
arr = np.array(53)
print(arr)
#checking dimensions
print(arr.ndim)
Output:
53
0
In the above example, arr.ndim is used to find the dimension of an array which is "0".
C. Checking the shape of an array:
Using “[variablename].shape”, find the shape of an array.
Syntax:
# creating 0-D array
arr = np.array(53)
print(arr)
#checking dimensions
print(arr.ndim)
#shape of an array
print ('shape of array:', arr. shape)
Output:
53
0
shape of array: () ---- since it’s a 0-dimensional array, there is no number displayed in it.
In the above example, arr.shape is used to find the shape of an array which is "0" because we dont have rows and columns.
2. Creating a 1-D array, checking the dimension and shape of the 1-D array:
1 | 2 | 3 | 4 | 5 |
Syntax:
# creating 1-D array
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)
#checking dimensions
print(arr1.ndim)
#shape of an array
print('shape of array :', arr1.shape)
Output:
[1 2 3 4 5]
1
shape of array: (5,) --- This shows the array is having 1 dimension and
the first dimension has 5 elements.
In this example, aar1.ndim prints 1 representing the array only has one dimension that is one row. Shape of array prints 5 reprenting number of elements in the row.
3. Creating a 2-D array, checking the dimension and shape of the 2-D array:
1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 4 |
Syntax:
# creating 2-D array
arr2 = np.array([[1, 2, 3, 4, 5],[6,7,8,9,4]])
print(arr2)
#checking dimensions
print(arr2.ndim)
#shape of an array
print('shape of array :', arr2.shape)
Output:
[[1 2 3 4 5]
[6 7 8 9 4]]
2
shape of array : (2, 5) –
This shows that 2 values inside a bracket represent it has 2 dimensions.
First dimension (row) has 2 values,
and the second dimension(column) has 5 values.
In this example, ndim represents 2-dimensional array is created and shape represents 2 rows with 5 elements in each row.
4. Creating a 3-D array, checking the dimension and shape of the 3-D array:
Now, lets go more deep. Lets create a 3 dimensional array with 2 arrays. each array has 2 rows with 5 elements in each row.
Syntax:
# creating 3-D array
arr3 = np.array([[[1, 2, 3, 4, 5],[6,7,8,9,4]],[[1, 2, 3, 4, 5],[6,7,8,9,4]]])
print(arr3)
#checking dimensions
print(arr3.ndim)
#Shape of an array
print('shape of array :', arr3.shape)
Output:
[[[1 2 3 4 5]
[6 7 8 9 4]]
[[1 2 3 4 5]
[6 7 8 9 4]]]
3
shape of array : (2, 2, 5)
This shows that 3 values inside a bracket represent it has 3 dimensions.
First dimension (i=number of sheets) has 2 sheets
second dimension (j=row) has 2 values,
and the third dimension(k=column) has 5 values.
5. Creating a 4-D array, checking the dimension and shape of the 4-D array:
Creating a 4- dimensional array is still interesting. Array of arrays is the key here. when you combine 2 of 3 dimensional arrays it becomes 4 dimensional array. In the below example, we have 2 arrays combined and so r=2.
Syntax:
# creating 4-D array
arr4 = np.array([[[[1, 2, 3, 4, 5],[6,7,8,9,4]],[[1, 2, 3, 4, 5],[6,7,8,9,4]]],[[[1, 2, 3, 4, 5],[6,7,8,9,4]],[[1, 2, 3, 4, 5],[6,7,8,9,4]]]])
print(arr4)
#checking dimensions
print(arr4.ndim)
#Shape of an array
print('shape of array :', arr4.shape)
Output:
[[[[1 2 3 4 5]
[6 7 8 9 4]]
[[1 2 3 4 5]
[6 7 8 9 4]]]
[[[1 2 3 4 5]
[6 7 8 9 4]]
[[1 2 3 4 5]
[6 7 8 9 4]]]]
4
shape of array : (2, 2, 2, 5)
This shows that 4 values inside a bracket represent it has 4 dimensions.
First dimension(r=number of arrays)has 2 arrays
Second dimension (i=number of sheets) has 2 sheets in each array
Third dimension (j=row) has 2 values,
and the Fourth dimension(k=column) has 5 values.
This is how we find the shape of an array in Python. Try doing different shapes and find its values.
Happy Analyzing!