Exercise: Python

Questions for: Arrays

What does the NumPy function numpy.zeros((2, 3)) return?
A:
2x3 array with all elements initialized to zero.
B:
3x2 array with all elements initialized to zero.
C:
2x2 identity matrix.
D:
3x3 array with all elements initialized to one.
Answer: A
The numpy.zeros() function creates an array filled with zeros of the specified shape.
What does the expression np.eye(4, k=1) generate?
A:
4x4 identity matrix with ones below the main diagonal.
B:
4x4 identity matrix with ones above the main diagonal.
C:
4x4 identity matrix with ones on the main diagonal.
D:
4x4 identity matrix with zeros below the main diagonal.
Answer: B
The np.eye() function generates an identity matrix with ones on the main diagonal, and the k parameter shifts the diagonal.
How can you concatenate two NumPy arrays arr1 and arr2 along the second axis?
A:
np.concatenate((arr1, arr2), axis=1)
B:
np.concat(arr1, arr2, axis=1)
C:
np.merge(arr1, arr2, axis=1)
D:
np.append(arr1, arr2, axis=1)
Answer: A
The np.concatenate() function can be used to concatenate arrays along a specified axis.
What does the NumPy function numpy.linspace(1, 10, 5) return?
A:
[1, 3, 5, 7, 10]
B:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
C:
[1.0, 3.25, 5.5, 7.75, 10.0]
D:
[1, 2.25, 3.5, 4.75, 6]
Answer: C
The numpy.linspace() function generates evenly spaced numbers over a specified range.
Given a NumPy array arr = np.array([[1, 2], [3, 4], [5, 6]]), what does arr.flatten(order='F') do?
A:
Flattens the array in C-style order (row-wise).
B:
Flattens the array in Fortran-style order (column-wise).
C:
Creates a new array by transposing the original array.
D:
Reshapes the array to have a single row.
Answer: B
The flatten() function with order='F' flattens the array in Fortran-style order.
Ad Slot (Above Pagination)
Quiz