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!

57 views2 comments

Recent Posts

See All

2 Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Are you in search of a reliable assignment helper in Australia? Look no further! Our service stands out as the ultimate solution for students grappling with academic tasks. We understand the complexities of academia and offer tailored assistance to ease your burden. Whether it's a challenging essay, a research-intensive paper, or a perplexing project, our team of experienced professionals is here to support you every step of the way. Committed to excellence and timely delivery, we ensure that your assignments not only meet but exceed expectations. Our dedicated support team is available round the clock to address any queries or concerns you may have. Trust us to deliver exceptional quality and help you achieve academic success. Don't let assignments overwhelm…

Like

Guest
May 06
Rated 4 out of 5 stars.

Assignment Help Services in Chicago serve as valuable resources for students seeking academic support and guidance. By leveraging the expertise of professionals and tutors, students can overcome challenges, excel in their studies, and achieve their academic goals in the vibrant academic landscape of Chicago.

Like
bottom of page