As with Python lists, NumPy arrays also have the concept of indexing.
Indexing allows us to access or get any element by using there index number. Every element stored in the array has a unique index number.
Like Python lists, NumPy uses zero-based indexing, meaning the first element is assigned index 0, the second element uses index 1, and so on.
How to use simple indexing in numpy ndarray.
Let's start with 1-D array:
It's very easy to get elements by using index numbers from the 1-D array.
Syntax:
array_name[index_number]
Example:
import numpy as np
x = np.array([122, 345, 5656, 28, 100, 56, 34])
print(x[3])
Output:
28
Indexing with 2-D array:
We simply get elements from the 2-D ndarray by passing array number or index number. Let's see how:
We have two different syntax:
array_name[array_index, index_number]
or
array_name[array_index][index_number]
Example:
import numpy as np
arr = np.array([[123, 34, 34, 45], [234, 8, 1, 96]])
print(arr[1][0])
# print(arr[1, 0]) #same
Output:
234
Indexing with 3-D array:
We can simply access elements from a 3-D ndarray by passing the index numbers for each dimension. The index numbers specify the position of the element in the 3D array, i.e [depth, row, column].
We have two different syntax:
array_name[depth_index, row_index, column_index]
or
array_name[depth_index][row_index][column_index]
Example:
import numpy as np
arr = np.array([[[123, 232, 322], [48, 5, 66]], [[33, 8, 10], [80, 762, 1]]])
print(arr[1, 1, 1])
# print(arr[1][1][1]) #same
Output:
762
and so on for higher dimensions...
17+ interview questions on NumPy simple indexing and attributes
Question 1. What is the basic indexing in NumPy?
Answer: Basic indexing refers to accessing elements in a NumPy array using simple indices similar to Python lists. You can use integers or slices to access array elements.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[0]) # Accessing the first element
Output:
1
Question 2. How do you index a 2D NumPy array?
Answer: In a 2D NumPy array, indexing is done using two indices: one for rows and one for columns.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[1, 2]) # Accessing element at second row and third column
Output:
6
Question 3. What does the ndim attribute represent in a NumPy array?
Answer: The ndim attribute returns the number of dimensions (axes) in a NumPy array.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)
Output:
2
Question 4. How do you access a single row of a 2D NumPy array?
Answer: To access a row in a 2D array, use the row index, followed by a colon (:) to include all columns.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[1]) # Accessing the second row
Output:
[4 5 6]
Question 5. How do you access all elements of a specific column in a 2D array?
Answer: To access all elements in a specific column, use the column index with a colon (:) to include all rows.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[:, 1]) # Accessing the second column
Output:
[4 5 6]
Question 6. What does the shape attribute represent in a NumPy array?
Answer: The shape attribute returns the dimensions of the array, represented as a tuple.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
Output:
(2, 3)
Question 7. How do you use simple indexing to modify an element in a NumPy array?
Answer: You can modify an element by assigning a new value to a specific index in the array.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4])
arr[2] = 10
print(arr)
Output:
[ 1 2 10 4]
Question 8. What does the size attribute in a NumPy array represent?
Answer: The size attribute returns the total number of elements in the array.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)
Output:
6
Question 9. How do you access a subarray using simple indexing?
Answer: You can access a subarray by specifying a range of indices using slicing.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4]) # Accessing a subarray from index 1 to 3
Output:
[20 30 40]
Question 10. How do you access the last element of a NumPy array?
Answer: Use negative indexing to access elements from the end of the array.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[-1]) # Accessing the last element
Output:
50
Question 11. How can you create a NumPy array with a specific data type?
Answer: You can specify the data type when creating a NumPy array using the dtype argument.
Example:
import numpy as np
arr = np.array([1, 2, 3], dtype=float)
print(arr)
Output:
[1. 2. 3.]
Question 12. How do you use simple indexing to get a specific element in a 3D array?
Answer: Use three indices to access elements in a 3D array (one for each dimension).
Example:
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr[1, 0, 1]) # Accessing element in the second 2D array, first row,
Output:
6
Question 13. What does the dtype attribute represent in a NumPy array?
Answer: The dtype attribute represents the data type of the elements in the array.
Example:
import numpy as np
arr = np.array([1, 2, 3], dtype=int)
print(arr.dtype)
Output:
int64
Question 14. How can you index an array of strings in NumPy?
Answer: NumPy arrays can store strings, and you can index them the same way as numerical arrays.
Example:
import numpy as np
arr = np.array(["apple", "banana", "cherry"])
print(arr[1]) # Accessing the second element
Output:
banana
Question 15. How do you check the number of elements in a NumPy array?
Answer: Use the size attribute to find the total number of elements in the array.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.size)
Output:
5
Question 16. What is the difference between shape and size attributes in NumPy?
Answer: shape returns a tuple representing the dimensions of the array.
size returns the total number of elements in the array.
Example:
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr.shape) # Dimensions
print(arr.size) # Total elements
Output:
(3, 2)
6
Question 17. How do you get the total number of elements in a 2D array using indexing?
Answer: You can use the size attribute or access the array's dimensions and multiply them.
Example:
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr.shape[0] * arr.shape[1]) # Total elements (rows * columns)
Output:
6
Question 18. What does the T attribute represent in a NumPy array?
Answer: The T attribute returns the transpose of the array, swapping rows with columns.
Example:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.T) # Transposing the array
Output:
[[1 3]
[2 4]]
Leave a comment
You must be logged in to post a comment.