top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

‘n’ - dimensional arrays in Python and how to find its shape!

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.

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

arr = np.array(53)

print(arr)                 

 

            Output:

       53

      

            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

            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.

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.

 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.

 4.      Creating a 3-D array, checking the dimension and shape of the 3-D array:



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:


 



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!

42 views0 comments

Recent Posts

See All

Beginner Friendly Java String Interview Questions

Hello Everyone! Welcome to the second section of the Java Strings blog. Here are some interesting coding questions that has been solved with different methods and approaches. “Better late than never!”

bottom of page