0
0
NumPydata~10 mins

Understanding array memory layout in NumPy - Interactive Quiz & Practice

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

Complete the code to create a 2D NumPy array filled with zeros.

NumPy
import numpy as np
arr = np.[1]((3, 4))
Drag options to blanks, or click blank then click option'
Azeros
Bones
Cempty
Dfull
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.ones instead of np.zeros
Using np.empty which creates uninitialized values
2fill in blank
medium

Complete the code to check if the array is stored in C-contiguous order.

NumPy
is_c_contiguous = arr.[1]
Drag options to blanks, or click blank then click option'
Aflags.contiguous
Bflags.c_order
Cflags.c_contiguous
Dflags.f_contiguous
Attempts:
3 left
💡 Hint
Common Mistakes
Using f_contiguous which checks Fortran order
Using flags.contiguous which does not exist
3fill in blank
hard

Fix the error in the code to reshape the array without copying data.

NumPy
reshaped = arr.[1]((6, 2), order='K')
Drag options to blanks, or click blank then click option'
Areshape
Bflatten
Cresize
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Using resize which changes the array in-place and may copy
Using flatten which always returns a copy
4fill in blank
hard

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

NumPy
arr_f = np.array([[1, 2], [3, 4]], order=[1])
is_fortran = arr_f.flags.[2]
Drag options to blanks, or click blank then click option'
A'F'
B'C'
Cf_contiguous
Dc_contiguous
Attempts:
3 left
💡 Hint
Common Mistakes
Using order='C' instead of 'F'
Checking c_contiguous flag for Fortran layout
5fill in blank
hard

Fill all three blanks to create a C-contiguous array, reshape it, and verify the layout.

NumPy
arr_c = np.array([[5, 6], [7, 8]], order=[1])
reshaped_c = arr_c.[2]((4, 1))
is_c = reshaped_c.flags.[3]
Drag options to blanks, or click blank then click option'
A'F'
Breshape
Cc_contiguous
D'C'
Attempts:
3 left
💡 Hint
Common Mistakes
Using order='F' instead of 'C'
Using resize instead of reshape
Checking f_contiguous flag for C layout