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

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

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!

161 views24 comments

24 Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Rated 5 out of 5 stars.

Nice sharing. The blog post on NumpyNinja explains the concept of n-dimensional arrays in Python using NumPy. It covers how to create and manipulate these arrays and provides a detailed guide on finding the shape of an array. The article is designed to help users understand multi-dimensional data structures and their practical applications. Need help crafting a compelling descriptive essay? Visit our website MyAssignmentHelp.com for descriptive essay help. Our team of skilled writers specializes in creating vivid, engaging essays that capture the essence of your topic. Whether you need help with brainstorming ideas, structuring your essay, or polishing your final draft, we provide personalized support to meet your specific requirements. Our focus on detail and creativity ensures that your descriptive…


Edited
Like

dd

Like

Rated 5 out of 5 stars.

The TronLink Wallet Extension is a browser extension that allows users to manage their TRON (TRX) and TRC-based tokens directly from their web browser. It serves as a bridge between the TRON blockchain and decentralized applications (dApps), enabling users to interact with dApps securely and the world of blockchain and cryptocurrency, having a reliable and secure wallet is crucial. Whether you're a seasoned investor or just getting started, the Tronlink Wallet Extension is a tool that can make managing your assets easier and safer. In this article, we’ll dive deep into what the Tronlink Wallet Extension is, its features, how to install and use it, and why it might be the right choice for you.The Tronlink Wallet Extension is a…

Like

Guest
Sep 08
Rated 5 out of 5 stars.

We can arrange escorts for Lahore with a variety of our most beautiful and experienced women who swiftly change into a person you’d like to talk with and would like you to have a great time by talking about their hopes and dreams. For a day that will be memorable, the ladies in our group will text you to make sure you’re at ease throughout your day. They’re educated and have worked with girls who escort Lahore over the last three years.

먹튀검증

Like

Rated 5 out of 5 stars.

I am jovial you take pride in what you write. It makes you stand way out from many other writers that can not push high-quality content like you

스포츠 중계

Like
bottom of page