Exercise: Python

Questions for: Arrays

What does the NumPy function numpy.identity(3) generate?
A:
3x3 array with ones on the main diagonal.
B:
3x3 array with ones above the main diagonal.
C:
3x3 array with ones below the main diagonal.
D:
3x3 array with zeros on the main diagonal.
Answer: A
The numpy.identity() function generates an identity matrix with ones on the main diagonal.
What does the NumPy function numpy.vstack((arr1, arr2)) do?
A:
Stacks arrays vertically.
B:
Stacks arrays horizontally.
C:
Vertically concatenates arrays.
D:
Horizontally concatenates arrays.
Answer: A
The numpy.vstack() function vertically stacks arrays.
Given a NumPy array arr = np.array([[1, 2, 3], [4, 5, 6]]), how can you extract the second column?
A:
arr[:, 1]
B:
arr[1, :]
C:
arr[1, 1]
D:
arr[:, 2]
Answer: A
The colon : denotes all rows, and 1 specifies the second column.
Given a NumPy array arr = np.array([[1, 2], [3, 4]]), what does arr.sum(axis=1) calculate?
A:
Sum of all elements in the array.
B:
Sum of elements along the first axis.
C:
Sum of elements along the second axis.
D:
Sum of elements in each row.
Answer: D
The sum() function with axis=1 calculates the sum of elements in each row.
How can you reverse the order of elements along the first axis of a NumPy array arr?
A:
np.flipud(arr)
B:
np.reverse(arr, axis=0)
C:
np.flip(arr, axis=0)
D:
arr[::-1]
Answer: C
The np.flip() function can be used to reverse the order of elements along a specified axis.
Ad Slot (Above Pagination)
Quiz