Exercise: Python

Questions for: Arrays

How do you reshape a NumPy array arr into a single-dimensional array?
A:
arr.reshape((1, -1))
B:
arr.flatten()
C:
arr.reshape((-1, 1))
D:
arr.ravel()
Answer: D
The arr.ravel() function flattens the array into a single-dimensional array.
How can you create a NumPy array filled with zeros of shape (4, 4)?
A:
np.zeros((4, 4))
B:
np.ones((4, 4))
C:
np.empty((4, 4))
D:
np.full((4, 4), 0)
Answer: A
The np.zeros() function creates an array filled with zeros of the specified shape.
Given a NumPy array arr = np.array([[1, 2, 3], [4, 5, 6]]), what does arr.shape return?
A:
(2, 3)
B:
(3, 2)
C:
(2,)
D:
(3,)
Answer: A
The arr.shape attribute returns the dimensions of the array.
What is the result of the expression np.linspace(0, 1, 5, endpoint=False)?
A:
[0.0, 0.2, 0.4, 0.6, 0.8]
B:
[0.0, 0.25, 0.5, 0.75, 1.0]
C:
[0.0, 0.1, 0.2, 0.3, 0.4]
D:
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
Answer: A
The expression generates an array with values from 0.0 to 1.0 (exclusive) with a step of 0.2 and excludes the endpoint.
How can you extract the diagonal elements from a NumPy array arr?
A:
np.diagonal(arr)
B:
arr.extract_diagonal()
C:
arr.get_diagonal()
D:
np.diag(arr)
Answer: A
The np.diagonal() function extracts the diagonal elements from the array.
Ad Slot (Above Pagination)
Quiz