Exercise: Python

Questions for: Arrays

What is the purpose of the NumPy function numpy.flipud(arr)?
A:
Flip array vertically.
B:
Flip array horizontally.
C:
Rotate array 90 degrees clockwise.
D:
Transpose the array.
Answer: A
The numpy.flipud() function flips the array vertically.
How can you calculate the element-wise square root of a NumPy array arr?
A:
np.sqrt(arr)
B:
arr ** 0.5
C:
np.square_root(arr)
D:
np.power(arr, 0.5)
Answer: A
The np.sqrt() function calculates the element-wise square root of the array.
What does the NumPy function numpy.arange(1, 10, 2) generate?
A:
[1, 3, 5, 7, 9]
B:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
C:
[2, 4, 6, 8, 10]
D:
[1, 4, 7]
Answer: A
The numpy.arange() function generates an array with evenly spaced values.
How can you concatenate two NumPy arrays arr1 and arr2 vertically?
A:
np.concatenate((arr1, arr2), axis=0)
B:
np.vstack((arr1, arr2))
C:
np.vertical_concatenate((arr1, arr2))
D:
arr1 + arr2
Answer: B
The np.vstack() function vertically stacks arrays.
Given a NumPy array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), how can you extract the diagonal elements?
A:
arr.diagonal()
B:
arr[1:3, 1:3]
C:
np.diag(arr)
D:
arr[::2, ::2]
Answer: C
The np.diag() function extracts the diagonal elements of the array.
Ad Slot (Above Pagination)
Quiz