0
0
NumPydata~10 mins

Contiguous arrays and stride tricks in NumPy - Interactive Code Practice

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

Complete the code to create a contiguous NumPy array from a list.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5], [1]='C')
Drag options to blanks, or click blank then click option'
Aorder
Bcopy
Cdtype
Dndmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'copy' instead of 'order' does not control contiguity.
Confusing 'dtype' with memory layout parameter.
2fill in blank
medium

Complete the code to check if a NumPy array is contiguous in memory.

NumPy
import numpy as np
arr = np.arange(10)
is_contiguous = arr.[1]
Drag options to blanks, or click blank then click option'
Acontiguous
Bis_contiguous()
Cis_c_contiguous
Dflags['C_CONTIGUOUS']
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method that does not exist.
Using incorrect attribute names.
3fill in blank
hard

Fix the error in the code to create a sliding window view using stride tricks.

NumPy
import numpy as np
from numpy.lib.stride_tricks import [1]
arr = np.arange(5)
windows = [1](arr, window_shape=3)
Drag options to blanks, or click blank then click option'
Asliding_window_view
Bas_strided
Cbroadcast_to
Dreshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as_strided' directly can cause errors if not used carefully.
Trying to use 'reshape' or 'broadcast_to' which do not create sliding windows.
4fill in blank
hard

Fill both blanks to create a 2D sliding window view of size 2x2 on a 3x3 array.

NumPy
import numpy as np
from numpy.lib.stride_tricks import [1]
arr = np.arange(9).reshape(3, 3)
windows = [1](arr, window_shape=[2])
Drag options to blanks, or click blank then click option'
Asliding_window_view
Bas_strided
C(2, 2)
D(3, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong function name.
Using the wrong window shape size.
5fill in blank
hard

Fill all three blanks to create a contiguous array, then create a sliding window view of size 3.

NumPy
import numpy as np
from numpy.lib.stride_tricks import [1]
arr = np.arange(10)[[2]]
contiguous_arr = np.ascontiguousarray(arr)
windows = [1](contiguous_arr, window_shape=[3])
Drag options to blanks, or click blank then click option'
Asliding_window_view
Bslice(0, 10)
C3
Dslice(1, 11)
Attempts:
3 left
💡 Hint
Common Mistakes
Using an incorrect slice range.
Forgetting to make the array contiguous before sliding window.