0
0
NumPydata~10 mins

Why memory management matters in NumPy - Test Your Understanding

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 of zeros with 5 elements.

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

Complete the code to check the memory size (in bytes) of a NumPy array.

NumPy
import numpy as np
arr = np.arange(10)
size_in_bytes = arr.[1]
Drag options to blanks, or click blank then click option'
Ashape
Bnbytes
Citemsize
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size which returns the number of elements, not bytes.
Using itemsize which returns bytes per element, not total.
3fill in blank
hard

Fix the error in the code to create a copy of a NumPy array to avoid modifying the original.

NumPy
import numpy as np
original = np.array([1, 2, 3])
copied = original.[1]()
Drag options to blanks, or click blank then click option'
Acopy
Bview
Cclone
Dduplicate
Attempts:
3 left
💡 Hint
Common Mistakes
Using view() which creates a shallow copy sharing the same data.
Using non-existent methods like clone() or duplicate().
4fill in blank
hard

Fill both blanks to create a memory-efficient array of integers from 0 to 9 with 8-bit data type.

NumPy
import numpy as np
arr = np.arange(10, dtype=[1])
size = arr.[2]
Drag options to blanks, or click blank then click option'
Anp.int8
Bnp.int64
Cnbytes
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.int64 which uses more memory than needed.
Using size which returns number of elements, not bytes.
5fill in blank
hard

Fill all three blanks to create a dictionary mapping each word to its length only if length is greater than 3.

NumPy
words = ['data', 'science', 'is', 'fun']
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.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.upper() as key which changes the word.
Using incorrect condition like len(word) < 3.