It is how the array elements are stored in the computer's memory, usually in a continuous block either row-wise (C order) or column-wise (Fortran order).
Click to reveal answer
beginner
What does 'C order' mean in NumPy array memory layout?
It means the array is stored row by row, like reading lines in a book. The last axis changes fastest.
Click to reveal answer
beginner
What does 'Fortran order' mean in NumPy array memory layout?
It means the array is stored column by column, like reading down columns in a spreadsheet. The first axis changes fastest.
Click to reveal answer
intermediate
How can you check if a NumPy array is stored in C order or Fortran order?
Use the attributes arr.flags['C_CONTIGUOUS'] and arr.flags['F_CONTIGUOUS']. They return True if the array is stored in C or Fortran order respectively.
Click to reveal answer
intermediate
Why does understanding array memory layout matter?
Because it affects speed of operations and memory usage. Accessing elements in the stored order is faster due to how computers read memory.
Click to reveal answer
In NumPy, which memory layout stores elements row-wise?
AC order
BFortran order
CRandom order
DColumn-major order
✗ Incorrect
C order means row-wise storage, where rows are stored one after another.
Which NumPy attribute tells you if an array is stored in Fortran order?
Aarr.flags['F_CONTIGUOUS']
Barr.flags['C_CONTIGUOUS']
Carr.order
Darr.layout
✗ Incorrect
arr.flags['F_CONTIGUOUS'] returns True if the array is stored in Fortran order.
Why is accessing elements in the stored memory order faster?
ABecause it changes the data type
BBecause the CPU caches memory blocks efficiently
CBecause it uses less memory
DBecause it compresses the data
✗ Incorrect
Accessing elements in stored order uses CPU cache better, speeding up operations.
What does it mean if a NumPy array is neither C_CONTIGUOUS nor F_CONTIGUOUS?
AThe array is empty
BThe array is read-only
CThe array is 1D
DThe array is stored in a non-contiguous or mixed memory layout
✗ Incorrect
If neither flag is True, the array memory is not stored continuously in either C or Fortran order.
Which of these is NOT a common memory layout for NumPy arrays?
AC order
BFortran order
CZ order
DRow-major order
✗ Incorrect
Z order is not a standard memory layout for NumPy arrays.
Explain the difference between C order and Fortran order in NumPy array memory layout.
Think about how you read text lines versus columns in a spreadsheet.
You got /3 concepts.
Why is it important to know the memory layout of a NumPy array when writing efficient code?
Consider how computers read memory and cache data.
You got /3 concepts.
Practice
(1/5)
1. What does it mean when a NumPy array is C-contiguous?
easy
A. The array is stored as a list of Python objects.
B. The array is stored in column-major order, meaning columns are stored one after another in memory.
C. The array is stored in row-major order, meaning rows are stored one after another in memory.
D. The array is stored in a compressed format to save memory.
Solution
Step 1: Understand memory layout types
C-contiguous means the array is stored in row-major order, where rows are stored sequentially in memory.
Step 2: Compare with other layouts
Fortran-contiguous arrays store columns sequentially, not rows.
Final Answer:
The array is stored in row-major order, meaning rows are stored one after another in memory. -> Option C
Quick Check:
C-contiguous = row-major order [OK]
Hint: C-contiguous means rows stored sequentially in memory [OK]
Common Mistakes:
Confusing C-contiguous with Fortran-contiguous
Thinking C-contiguous means compressed storage
Assuming C-contiguous means column-major order
2. Which of the following NumPy commands correctly checks if an array a is stored in Fortran-contiguous order?
easy
A. np.isfortran(a)
B. np.iscontiguous(a)
C. a.is_fortran()
D. a.flags.f_contiguous
Solution
Step 1: Recall NumPy flags for memory layout
NumPy arrays have a flags attribute with f_contiguous to check Fortran order.
Step 2: Identify correct syntax
a.flags.f_contiguous returns True if array a is Fortran-contiguous.
Final Answer:
a.flags.f_contiguous -> Option D
Quick Check:
Check Fortran order with a.flags.f_contiguous [OK]
Hint: Use a.flags.f_contiguous to check Fortran order [OK]
Common Mistakes:
Using non-existent functions like np.isfortran
Confusing C-contiguous and Fortran-contiguous flags
A. Because slicing can create views that are not stored continuously in memory.
B. Because slicing always returns a copy, which is never contiguous.
C. Because reshape changes the memory layout to Fortran order.
D. Because arange creates arrays that are never contiguous.
Solution
Step 1: Understand slicing behavior
Slicing often returns a view, which may not be stored continuously in memory.
Step 2: Check memory layout after slicing
The slice arr[:, 1:3] selects columns 1 and 2, which may not be contiguous in row-major order.
Step 3: Explain why c_contiguous is False
Because the slice skips some elements in memory, the resulting view is not C-contiguous.
Final Answer:
Because slicing can create views that are not stored continuously in memory. -> Option A
Quick Check:
Slicing can break contiguity = False [OK]
Hint: Slicing can create non-contiguous views, so flag may be False [OK]
Common Mistakes:
Thinking slicing always returns a copy
Assuming reshape changes memory layout to Fortran order by default
Believing arange arrays are never contiguous
5. You want to create a large 2D NumPy array with shape (1000, 1000) that is stored in Fortran order for faster column-wise operations. Which code snippet correctly creates such an array?
hard
A. np.zeros((1000, 1000), order='F')
B. np.zeros((1000, 1000), order='C')
C. np.zeros((1000, 1000)).T.copy()
D. np.zeros((1000, 1000), fortran=True)
Solution
Step 1: Recall how to specify memory order in NumPy
Use the order parameter with value 'F' to create Fortran-contiguous arrays.
Step 2: Analyze each option
np.zeros((1000, 1000), order='F') uses order='F', which is correct. np.zeros((1000, 1000), order='C') uses 'C' (row-major). np.zeros((1000, 1000)).T.copy() transposes a C-contiguous array and copies it into a new C-contiguous array by default. np.zeros((1000, 1000), fortran=True) uses an invalid parameter.
Final Answer:
np.zeros((1000, 1000), order='F') -> Option A
Quick Check:
Use order='F' to create Fortran order arrays [OK]
Hint: Use order='F' in array creation for Fortran order [OK]
Common Mistakes:
Using order='C' instead of 'F'
Trying to transpose to get Fortran order
Using invalid parameters like fortran=True
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.