0
0
NumPydata~10 mins

Memory layout (C-order vs Fortran-order) in NumPy - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a NumPy array with C-order memory layout.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]], order=[1])
print(arr.flags['C_CONTIGUOUS'])
Drag options to blanks, or click blank then click option'
AF
BA
CK
DC
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'F' instead of 'C' for C-order layout.
Not specifying the order parameter, which defaults to 'C' but should be explicit here.
2fill in blank
medium

Complete the code to create a NumPy array with Fortran-order memory layout.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]], order=[1])
print(arr.flags['F_CONTIGUOUS'])
Drag options to blanks, or click blank then click option'
AF
BC
CK
DA
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'C' instead of 'F' for Fortran-order layout.
Confusing the memory layout flags when checking the array.
3fill in blank
hard

Fix the error in the code to check if an array is stored in C-order.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]], order='C')
print(arr.flags[[1]])
Drag options to blanks, or click blank then click option'
A'CONTIGUOUS_C'
B'F_CONTIGUOUS'
C'C_CONTIGUOUS'
D'CONTIGUOUS_F'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'F_CONTIGUOUS' to check C-order.
Using incorrect flag names like 'CONTIGUOUS_C'.
4fill in blank
hard

Fill both blanks to create a Fortran-order array and check its memory layout.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]], order=[1])
print(arr.flags[[2]])
Drag options to blanks, or click blank then click option'
AF
BC
C'F_CONTIGUOUS'
D'C_CONTIGUOUS'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'C' and 'F' for order parameter.
Checking the wrong flag for the memory layout.
5fill in blank
hard

Fill all three blanks to create a C-order array, reshape it, and check if it remains C-contiguous.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4], order=[1])
arr_reshaped = arr.reshape((2, 2), order=[2])
print(arr_reshaped.flags[[3]])
Drag options to blanks, or click blank then click option'
AF
BC
C'C_CONTIGUOUS'
D'F_CONTIGUOUS'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different orders for creation and reshape causing non-contiguous layout.
Checking the wrong memory layout flag.