Exercise: Python

Questions for: Arrays

How do you perform element-wise addition of a scalar value, 5, to a NumPy array arr?
A:
np.add_scalar(arr, 5)
B:
arr.add(5)
C:
arr + 5
D:
np.scalar_add(arr, 5)
Answer: C
The + operator can be used for element-wise addition of a scalar value to a NumPy array.
What does the NumPy function numpy.eye(N, M=None, k=0) do?
A:
Creates an identity matrix with dimensions N x N.
B:
Generates a random matrix of size N x M.
C:
Reshapes an array to have dimensions N x M.
D:
Computes the eigenvalues of a matrix.
Answer: A
The numpy.eye() function creates an identity matrix with dimensions N x N.
How do you perform element-wise multiplication of two NumPy arrays, arr1 and arr2?
A:
np.multiply(arr1, arr2)
B:
arr1 * arr2
C:
arr1.mul(arr2)
D:
np.product(arr1, arr2)
Answer: B
The * operator can be used for element-wise multiplication of two NumPy arrays.
In NumPy, how do you find the indices of elements that satisfy a given condition in an array?
A:
np.index_of(arr, condition)
B:
np.where(condition, arr)
C:
np.find_indices(arr, condition)
D:
np.where(condition)
Answer: D
The np.where() function in NumPy returns the indices of elements that satisfy a given condition.
What does the numpy.transpose() function do?
A:
Flips the array vertically.
B:
Swaps the axes of the array.
C:
Rotates the array 90 degrees.
D:
Converts the array to a transposed form.
Answer: B
The numpy.transpose() function swaps the axes of the array.
Ad Slot (Above Pagination)
Quiz