0
0
NumPydata~10 mins

Strides and how data is accessed 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 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.