Python Programming - Arrays

Exercise : Arrays - General Questions
  • Arrays - General Questions
81.
What does the NumPy function numpy.zeros((2, 3)) return?
2x3 array with all elements initialized to zero.
3x2 array with all elements initialized to zero.
2x2 identity matrix.
3x3 array with all elements initialized to one.
Answer: Option
Explanation:
The numpy.zeros() function creates an array filled with zeros of the specified shape.

82.
How can you reverse the order of elements along the first axis of a NumPy array arr?
np.flipud(arr)
np.reverse(arr, axis=0)
np.flip(arr, axis=0)
arr[::-1]
Answer: Option
Explanation:
The np.flip() function can be used to reverse the order of elements along a specified axis.

83.
Given a NumPy array arr = np.array([[1, 2], [3, 4]]), what does arr.sum(axis=1) calculate?
Sum of all elements in the array.
Sum of elements along the first axis.
Sum of elements along the second axis.
Sum of elements in each row.
Answer: Option
Explanation:
The sum() function with axis=1 calculates the sum of elements in each row.

84.
Given a NumPy array arr = np.array([[1, 2, 3], [4, 5, 6]]), how can you extract the second column?
arr[:, 1]
arr[1, :]
arr[1, 1]
arr[:, 2]
Answer: Option
Explanation:
The colon : denotes all rows, and 1 specifies the second column.

85.
What does the NumPy function numpy.vstack((arr1, arr2)) do?
Stacks arrays vertically.
Stacks arrays horizontally.
Vertically concatenates arrays.
Horizontally concatenates arrays.
Answer: Option
Explanation:
The numpy.vstack() function vertically stacks arrays.