Python Programming - Arrays

Exercise : Arrays - General Questions
  • Arrays - General Questions
41.
How do you find the median value of a NumPy array arr?
np.median(arr)
arr.median()
np.mean(arr)
arr.mean()
Answer: Option
Explanation:
The np.median() function calculates the median value of the array.

42.
What does the NumPy function numpy.zeros((3, 4)) do?
Creates a 3x4 array with all elements initialized to zero.
Generates an array with random values.
Reshapes an existing array to have dimensions 3x4.
Computes the square root of each element in the array.
Answer: Option
Explanation:
The numpy.zeros() function creates a 3x4 array with all elements initialized to zero.

43.
How can you find the indices of non-zero elements in a NumPy array arr?
np.where(arr != 0)
np.nonzero(arr)
np.find_nonzero(arr)
arr.index_nonzero()
Answer: Option
Explanation:
The np.nonzero() function returns the indices of non-zero elements in the array.

44.
What does the NumPy function numpy.linspace(1, 10, 5) do?
Creates an array with values ranging from 1 to 10 with a step of 5.
Generates a sequence of 5 evenly spaced values between 1 and 10.
Reshapes an existing array to have dimensions 1x10.
Computes the logarithm of each element in the array.
Answer: Option
Explanation:
The numpy.linspace() function generates a sequence of 5 evenly spaced values between 1 and 10.

45.
Given a NumPy array arr = np.array([2, 4, 6, 8, 10]), how do you extract elements at even indices?
arr[::2]
arr[1::2]
arr[1:][::2]
arr[1::]
Answer: Option
Explanation:
The code arr[::2] extracts elements at even indices from the array.