Bird
0
0

What will be printed by this code?

medium📝 Predict Output Q4 of 15
SciPy - Integration with Scientific Ecosystem
What will be printed by this code?
import numpy as np
from scipy import io
arr = np.array([[4, 5], [6, 7]])
io.savemat('sample.mat', {'matrix': arr})
data = io.loadmat('sample.mat')
print(data['matrix'])
A[4 5 6 7]
B[[4 5] [6 7]]
C{'matrix': array([[4, 5], [6, 7]])}
DError: KeyError
Step-by-Step Solution
Solution:
  1. Step 1: Save the numpy array

    The array arr is saved under the key 'matrix' in 'sample.mat' using savemat.
  2. Step 2: Load the .mat file

    Using loadmat, the file is loaded into a dictionary where 'matrix' maps to the original numpy array.
  3. Step 3: Print the loaded array

    Printing data['matrix'] outputs the original 2D numpy array as a formatted array.
  4. Final Answer:

    [[4 5] [6 7]] -> Option B
  5. Quick Check:

    Loaded arrays print as numpy arrays [OK]
Quick Trick: loadmat returns dict; access arrays by keys [OK]
Common Mistakes:
  • Expecting a flattened array
  • Printing the whole dict instead of the array
  • Using wrong key causing KeyError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes