Bird
Raised Fist0
NumPydata~10 mins

Strides and how data is accessed in NumPy - Interactive Code Practice

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

Complete the code to create a 2D NumPy array with shape (3, 4).

NumPy
import numpy as np
arr = np.array([1])
print(arr.shape)
Drag options to blanks, or click blank then click option'
A[[1, 2, 3], [4, 5, 6]]
B[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
C[[1, 2], [3, 4], [5, 6]]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a flat list instead of a list of lists.
Creating inner lists with wrong length.
2fill in blank
medium

Complete the code to get the strides of the NumPy array 'arr'.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
strides = arr.[1]
print(strides)
Drag options to blanks, or click blank then click option'
Astrides
Bdtype
Cshape
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using shape instead of strides.
Trying to call a method instead of accessing an attribute.
3fill in blank
hard

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

NumPy
import numpy as np
arr = np.arange(6)
reshaped = arr.[1]((2, 3), order='C')
print(reshaped)
Drag options to blanks, or click blank then click option'
Areshape
Bresize
Cflatten
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Using resize which modifies the array in-place.
Using flatten or ravel which change the array to 1D.
4fill in blank
hard

Fill both blanks to create a transposed view of the array and print its strides.

NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
transposed = arr.[1]
print(transposed.[2])
Drag options to blanks, or click blank then click option'
AT
Bshape
Cstrides
Dtranspose
Attempts:
3 left
💡 Hint
Common Mistakes
Using transpose() as a method call instead of property T.
Printing shape instead of strides.
5fill in blank
hard

Fill all three blanks to create a dictionary of word lengths for words longer than 3 characters.

NumPy
words = ['data', 'science', 'is', 'fun']
lengths = { [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length as key and word as value.
Using '<' instead of '>' in the condition.

Practice

(1/5)
1. What do strides in a NumPy array represent?
easy
A. The number of bytes to jump to move to the next element in each dimension
B. The total number of elements in the array
C. The shape of the array
D. The data type of the array elements

Solution

  1. Step 1: Understand the definition of strides

    Strides indicate how many bytes NumPy moves in memory to access the next element along each dimension.
  2. Step 2: Differentiate from other array attributes

    Shape is the size per dimension, dtype is the data type, and total elements is the count, none describe memory jumps.
  3. Final Answer:

    The number of bytes to jump to move to the next element in each dimension -> Option A
  4. Quick Check:

    Strides = bytes jump per dimension [OK]
Hint: Strides = byte steps to next element per dimension [OK]
Common Mistakes:
  • Confusing strides with shape
  • Thinking strides represent element count
  • Mixing strides with data type size
2. Which of the following is the correct way to check the strides of a NumPy array arr?
easy
A. arr.strides()
B. arr.strides
C. arr.get_strides()
D. strides(arr)

Solution

  1. Step 1: Recall how strides are accessed in NumPy

    Strides is an attribute, not a method, so it is accessed without parentheses.
  2. Step 2: Evaluate each option

    arr.strides() and C use parentheses incorrectly; strides(arr) is not a valid function; arr.strides correctly accesses the attribute.
  3. Final Answer:

    arr.strides -> Option B
  4. Quick Check:

    Strides is an attribute, no parentheses [OK]
Hint: Use attribute, not method: arr.strides [OK]
Common Mistakes:
  • Using parentheses like a method
  • Trying non-existent functions
  • Confusing strides with shape attribute
3. Given the code:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
slice_arr = arr[:, 1:3]

What will slice_arr.strides most likely be if arr.strides is (24, 8)?
medium
A. (24, 16)
B. (24, 8) but reversed
C. (24, 8)
D. (24, 8) but with first stride changed to 8

Solution

  1. Step 1: Understand original strides meaning

    Original strides (24, 8) mean 24 bytes to move to next row, 8 bytes to next column.
  2. Step 2: Analyze slicing effect on strides

    Slicing [:, 1:3] uses step=1 for columns (equivalent to 1:3:1), so both row stride (24) and column stride (8) remain unchanged.
  3. Final Answer:

    (24, 8) -> Option C
  4. Quick Check:

    Step=1 slicing keeps strides unchanged [OK]
Hint: Step=1 slicing keeps strides unchanged [OK]
Common Mistakes:
  • Thinking non-zero start changes strides
  • Believing column slicing doubles stride
  • Confusing with step>1 strided slicing
4. Consider this code snippet:
import numpy as np
arr = np.arange(12).reshape(3,4)
sub_arr = arr[::2, ::-1]
print(sub_arr.strides)

It raises an error. What is the likely cause?
medium
A. Negative step in slicing causes invalid strides
B. Using step size 2 in slicing is not allowed
C. Slicing with negative step reverses strides but is valid
D. No error; code runs fine

Solution

  1. Step 1: Understand slicing with steps

    The code uses [::2, ::-1]: step=2 for rows (valid), step=-1 for columns (valid, reverses dimension).
  2. Step 2: Check if code runs

    NumPy supports all integer steps including negative; strides adjust (row *=2, col *=-1), no error occurs.
  3. Final Answer:

    No error; code runs fine -> Option D
  4. Quick Check:

    Negative step reverses strides, no error [OK]
Hint: Negative slice steps reverse strides, no error [OK]
Common Mistakes:
  • Assuming negative steps cause errors
  • Thinking step size 2 is invalid
  • Confusing stride reversal with error
5. You have a large 2D NumPy array data with shape (1000, 1000) and strides (8000, 8). You want to create a view that selects every 10th row and every 5th column without copying data. What will be the strides of this view?
hard
A. (80000, 40)
B. (8000, 8)
C. (8000, 40)
D. (80000, 8)

Solution

  1. Step 1: Understand original strides

    Original strides (8000, 8) mean 8000 bytes to move one row, 8 bytes to move one column.
  2. Step 2: Calculate new strides after slicing

    Selecting every 10th row multiplies row stride by 10 -> 8000*10=80000; every 5th column multiplies column stride by 5 -> 8*5=40.
  3. Final Answer:

    (80000, 40) -> Option A
  4. Quick Check:

    Stride scales by step size in slicing [OK]
Hint: Multiply strides by slice step size [OK]
Common Mistakes:
  • Forgetting to multiply strides by step size
  • Mixing row and column strides
  • Assuming strides stay unchanged after slicing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard