0
0
NumPydata~10 mins

ndarray as the core data structure 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 NumPy array from a Python list.

NumPy
import numpy as np
arr = np.[1]([1, 2, 3, 4])
print(arr)
Drag options to blanks, or click blank then click option'
Aarray
Blist
Cndarray
Dmatrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.list instead of np.array
Using np.matrix which creates a different object
Trying to use np.ndarray directly without arguments
2fill in blank
medium

Complete the code to find the shape of the NumPy array.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
shape = arr.[1]
print(shape)
Drag options to blanks, or click blank then click option'
Asize
Bdim
Clength
Dshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using size which returns total elements, not shape
Using length which is not an ndarray attribute
Using dim which is not a valid attribute
3fill in blank
hard

Fix the error in the code to correctly create a 2D NumPy array.

NumPy
import numpy as np
arr = np.array([1])
print(arr)
Drag options to blanks, or click blank then click option'
A[1, [2, 3, 4]]
B[1, 2, 3, 4]
C[[1, 2], [3, 4]]
D[[1, 2, 3], 4]
Attempts:
3 left
💡 Hint
Common Mistakes
Using uneven nested lists causing ragged arrays
Using a flat list when a 2D array is needed
Mixing data types inside the list
4fill in blank
hard

Fill both blanks to create a 3x3 array of zeros and print its data type.

NumPy
import numpy as np
arr = np.[1]((3, 3), dtype=[2])
print(arr.dtype)
Drag options to blanks, or click blank then click option'
Azeros
Bones
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.ones instead of zeros
Using int dtype when float is expected
Forgetting to specify shape as a tuple
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word to its length if length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = { [1]: [2] for word in words if [3] }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using word > 3 which compares string to number
Using len(word) without comparison in if condition
Swapping key and value positions