Exercise: Python

Questions for: Arrays

How can you calculate the mean of a NumPy array arr along the second axis?
A:
arr.mean(axis=1)
B:
np.mean(arr, axis=1)
C:
np.mean(axis=1, arr)
D:
arr.mean(axis=0)
Answer: A
The mean() function with axis=1 calculates the mean along the second axis.
What is the result of the expression np.linspace(0, 1, num=5, endpoint=False)?
A:
[0.0, 0.25, 0.5, 0.75, 1.0]
B:
[0.0, 0.2, 0.4, 0.6, 0.8]
C:
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
D:
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2]
Answer: B
The np.linspace() function generates evenly spaced numbers over a specified range.
How can you reshape a NumPy array arr to have 2 rows and 3 columns?
A:
np.reshape(arr, (2, 3))
B:
arr.resize(2, 3)
C:
arr.reshape(2, 3)
D:
np.resize(arr, (2, 3))
Answer: C
The reshape() function is used to reshape the array.
Given a NumPy array arr = np.array([10, 20, 30, 40]), what does arr[1:3] return?
A:
[10, 20]
B:
[20, 30]
C:
[30, 40]
D:
[20, 30, 40]
Answer: B
The slice arr[1:3] extracts elements from index 1 to (3-1).
How can you flatten a NumPy array arr in column-major order?
A:
arr.flatten()
B:
np.column_stack(arr)
C:
arr.ravel(order='F')
D:
np.column_major(arr)
Answer: C
The ravel() function with order='F' flattens the array in column-major order.
Ad Slot (Above Pagination)
Quiz