Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q13 of 15
SciPy - Integration with Scientific Ecosystem
What will be the output of this code snippet?
import numpy as np
from scipy import io
arr = np.array([1, 2, 3])
io.savemat('test.mat', {'array': arr})
data = io.loadmat('test.mat')
print(data['array'])
A[[1 2 3]]
BError: KeyError
C[[1] [2] [3]]
D[1 2 3]
Step-by-Step Solution
Solution:
  1. Step 1: Understand how savemat stores arrays

    When saving a 1D numpy array, savemat stores it as a 2D array with shape (1, n) by default.
  2. Step 2: Check the loaded data shape

    Loading back with loadmat returns a 2D array with shape (1, 3), so printing shows [[1 2 3]].
  3. Final Answer:

    [[1 2 3]] -> Option A
  4. Quick Check:

    1D array saved as 2D row = [[1 2 3]] [OK]
Quick Trick: Loaded arrays from .mat are often 2D, not 1D [OK]
Common Mistakes:
  • Expecting 1D array output
  • Confusing row vs column shape
  • Assuming KeyError due to wrong key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes