NumPy ndarray:

      When we create an object in numpy with the help of array method, that object is called ndarray.

An ndarray is a collection of integer data that is arranged in rows and columns. 

Syntax to Create a ndarray:

import numpy as np

array_name = np.array("your numarical data here")

How to Create a ndarray:

import numpy as np
array_name = np.array([12, 34, 45, 56, 78, 23])

print(array_name)

print(type(array_name))   # check data type of a veriable

Output:

[12 34 45 56 78 23]

<class 'numpy.ndarray'>

 

Dimensions of a NumPy ndarray:

When we create an ndarray, it always holds the dimensions. So, we have 0-D arrays, 1-D arrays, 2-D arrays, 3-D arrays, ..., up to 100-D arrays, or even n-D arrays.

So, let's start with how to create ndarrays with different dimensions.

0-Dimensional ndarray :

Also called scalar array. A scalar is just a single number.

import numpy as np

my_array = np.array(42)

print(my_array)
print(my_array.ndim)    #ndim is used to check the dimensions of an array

Output:

42
0

 

1-Dimensional ndarray :

Also called Vector array. A 1D array is the same as a list of numbers with a single axis (rows).

import numpy as np

my_array = np.array([12, 34, 45, 56, 67, 78, 100])

print(my_array)
print(my_array.ndim)    #ndim is used to check the dimensions of an array

Output:

[ 12  34  45  56  67  78 100]
1

 

2-Dimensional ndarray :

Also called matrix array. A 2D ndarray is the collection of multiple 1-D ndarrays. A 2D array is like a table with rows and columns.

To create 2-D ndarray we have to pass multiple 1-D ndarray and wrap all of these arrays with an outer bracket.

import numpy as np

my_array = np.array([[56, 67, 78, 100], [23, 34, 45, 7], [23, 45, 56, 67]])

print(my_array)
print(my_array.ndim)

Output:

[[ 56  67  78 100]
 [ 23  34  45   7]
 [ 23  45  56  67]]


2 #these are the Dimensions

Note : Always remember, all the arrays are with the same number of elements.


 

3-Dimensional ndarray :

Also called Tensor array. A 3D ndarray is the collection of multiple 2-D ndarrays. A 3D array is like a stack of matrices (rows, columns, and depth).

To create 3-D ndarray we have to pass multiple 2-D ndarray and wrap all of these arrays with an outer bracket.

import numpy as np

my_array = np.array([[[56, 67, 78], [23, 34, 45]], [[23, 45, 100], [200, 56, 67]]])

print(my_array)
print(my_array.ndim)    

Output:

[[[ 56  67  78]
  [ 23  34  45]]

 [[ 23  45 100]
  [200  56  67]]]


3 #these are the Dimensions

 

Higher-Dimensional Arrays :

Arrays can have even more dimensions, like 4D or 5D, 6D, 100D, ........, nD, typically used in advanced data processing like image processing or deep learning.

Now you have to follow the same process to make arrays.

At last let's create a 5-D ndArray:

 

5-Dimensional ndarray :

To create 5-D ndarray we have to pass multiple 4-D ndarray and wrap all of these arrays with an outer bracket.

import numpy as np

my_array = np.array([[[[[1, 2], [3, 4]]],  [[[5, 6], [7, 8]]]],  [[[[9, 10], [11, 12]]],  [[[13, 14], [15, 16]]]]])

print(my_array)
print(my_array.ndim)   

Output:

[[[[[ 1  2]
    [ 3  4]]]

  [[[ 5  6]
    [ 7  8]]]]

 [[[[ 9 10]
    [11 12]]]

  [[[13 14]
    [15 16]]]]]


5 #these are the Dimensions

 

You also have the option to create higher dimensions. As you see, all the ndarray dimensions are based on how many brackets are introduced at the start of the array. This is also one of the ways to know the dimensions of an ndarray. These brackets tell the dimensions of the array.

Or, you are also able to create an ndarray with the brackets. For example:

If i want to create 5-D ndarray so i direct make this with:

import numpy as np

my_array = np.array([[[[[10]]]]])

print(my_array)
print(my_array.ndim)    #ndim is used to check the dimensions of an array

Output:

[[[[[10]]]]]

5

 

20+ interview questions about NumPy ndarray

Question 1 : What is a NumPy array (ndarray)?

Answer: A NumPy array (ndarray) is a multi-dimensional array object in Python, provided by the NumPy library. It can store elements of the same data type and is more efficient for numerical operations than Python lists.

Example: 

import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)

Output:

[1 2 3 4]

 

Question 2 : How do you create a NumPy array?

Answer: You can create a NumPy array using np.array() by passing a list or other array-like object.

Example: 

import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr)

Output:

[10 20 30 40]

 

Question 3 : What does the term "ndarray" stand for?

Answer: "ndarray" stands for "N-dimensional array," which means it can represent arrays with any number of dimensions (1D, 2D, 3D, etc.).

 

Question 4 : What is the syntax for creating a NumPy array?

Answer: The syntax is np.array([list_of_elements]).

Example: 

import numpy as np
arr = np.array([1, 2, 3])
print(arr)

Output:

[1 2 3]

 

Question 5 : How can you check the type of a NumPy array?

Answer: You can use the type() function to check the type of a NumPy array.

Example: 

import numpy as np
arr = np.array([1, 2, 3])
print(type(arr))

Output:

<class 'numpy.ndarray'>

 

Question 6 : How do you find the dimensions of a NumPy array?

Answer: You can use the ndim attribute to find the number of dimensions.

Example: 

import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.ndim)

Output:

2

 

Question 7 : What is a 0-dimensional ndarray?

Answer: A 0-dimensional ndarray is a scalar array that contains just a single value.

Example: 

import numpy as np
arr = np.array(42)
print(arr)
print(arr.ndim)

Output:

42
0

 

Question 8 : What is a 1-dimensional ndarray (Vector)?

Answer: A 1D ndarray is an array with a single axis, like a list of numbers.

Example: 

import numpy as np
arr = np.array([12, 34, 45])
print(arr)
print(arr.ndim)

Output:

[12 34 45]
1

 

Question 9 : What is a 2-dimensional ndarray (Matrix)?

Answer: A 2D ndarray is like a table with rows and columns, created by passing a list of lists to np.array().

Example: 

import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr)
print(arr.ndim)

Output:

[[1 2]
 [3 4]]
2

 

Question 10 : What is a 3-dimensional ndarray (Tensor)?

Answer: A 3D ndarray is like a stack of matrices and can be created by passing multiple 2D arrays.

Example: 

import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr)
print(arr.ndim)

Output:

[[[1 2]
  [3 4]]
 [[5 6]
  [7 8]]]
3

 

Question 11 : What is a 5-dimensional ndarray?

Answer: A 5D ndarray is an array with 5 dimensions, and you can create it by passing 4D arrays to np.array().

Example: 

import numpy as np
arr = np.array([[[[[1, 2], [3, 4]]], [[[5, 6], [7, 8]]]]])
print(arr)
print(arr.ndim)

Output:

[[[[[1 2]
   [3 4]]]

  [[[5 6]
   [7 8]]]]]
5

 

Question 12 : What does ndim represent?

Answer: ndim represents the number of dimensions of the ndarray (array).

Example: 

import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.ndim)

Output:

2

 

Question 13 : What is the shape of an ndarray?

Answer: The shape of an ndarray represents the number of elements along each axis. It is returned as a tuple.

Example: 

import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)

Output:

(2, 2)

 

Question 14 : How to create a 2D array from a list of lists?

Answer: You can create a 2D array by passing a list of lists to np.array().

Example: 

import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr)

Output:

[[1 2]
 [3 4]
 [5 6]]

 

Question 15 : Can we create a multidimensional array directly by passing a tuple of tuples?

Answer: Yes, we can create a multidimensional array by passing tuples of tuples.

Example: 

import numpy as np
arr = np.array(((1, 2), (3, 4), (5, 6)))
print(arr)

Output:

[[1 2]
 [3 4]
 [5 6]]

 

Question 16 : How can you check the size of an ndarray (total number of elements)?

Answer: You can use the size attribute to check the total number of elements in an ndarray.

Example: 

import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.size)

Output:

4

 

Question 17 : What happens if you pass an empty list to np.array()?

Answer: It creates an empty ndarray.

Example: 

import numpy as np
arr = np.array([])
print(arr)
print(arr.ndim)

Output:

[]
1

 

Question 18 : How to create a 1D ndarray with a specific range of numbers?

Answer: You can use np.arange() to create a 1D array with a specified range.

Example: 

import numpy as np
arr = np.arange(5, 15, 2)
print(arr)

Output:

[ 5  7  9 11 13]

 

Question 19 : Can NumPy arrays have more than 2 dimensions?

Answer: Yes, NumPy arrays can have any number of dimensions, and higher-dimensional arrays are often used in advanced applications like image processing or deep learning.

 

Leave a comment

You must be logged in to post a comment.

0 Comments