Python Programming - Arrays

Exercise : Arrays - General Questions
  • Arrays - General Questions
56.
How can you check if all elements in a NumPy array arr are non-zero?
np.nonzero(arr)
np.all(arr != 0)
arr.all_nonzero()
arr.is_nonzero()
Answer: Option
Explanation:
The code np.all(arr != 0) checks if all elements in the array are non-zero.

57.
What does the NumPy function numpy.diag([1, 2, 3]) do?
Creates a 3x3 array with values 1, 2, and 3 on the main diagonal.
Generates a diagonal matrix with values 1, 2, and 3.
Reshapes an existing array to have dimensions 1x3.
Computes the determinant of a 3x3 matrix.
Answer: Option
Explanation:
The numpy.diag() function creates a diagonal matrix with the specified values on the main diagonal.

58.
Given a NumPy array arr = np.array([5, 2, 8, 1, 7]), what does np.argmax(arr) return?
1
2
4
0
Answer: Option
Explanation:
The np.argmax() function returns the index of the maximum value in the array.

59.
What is the purpose of the NumPy function numpy.concatenate((arr1, arr2), axis=0)?
Joins the arrays along the columns.
Joins the arrays along the rows.
Creates a new array with the maximum values from both arrays.
Computes the dot product of the arrays.
Answer: Option
Explanation:
The numpy.concatenate() function joins the arrays along the specified axis, in this case, along the rows.

60.
How can you extract the diagonal elements from a NumPy array arr?
np.diagonal(arr)
arr.extract_diagonal()
arr.get_diagonal()
np.diag(arr)
Answer: Option
Explanation:
The np.diagonal() function extracts the diagonal elements from the array.