0
0
NumPydata~20 mins

Why memory management matters in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Mastery in Data Science
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding numpy array memory usage

What is the output of the following code showing the memory size of a numpy array?

NumPy
import numpy as np
arr = np.arange(1000)
print(arr.nbytes)
A1000
B4000
C8000
D16000
Attempts:
2 left
💡 Hint

Each element in the array is an integer of 8 bytes (int64) by default.

data_output
intermediate
2:00remaining
Effect of data type on memory usage

Which option shows the correct memory size in bytes for a numpy array of 1000 elements with dtype float64?

NumPy
import numpy as np
arr = np.arange(1000, dtype=np.float64)
print(arr.nbytes)
A32000
B16000
C4000
D8000
Attempts:
2 left
💡 Hint

float64 uses 8 bytes per element.

🧠 Conceptual
advanced
2:00remaining
Why memory management is important in data science

Why is managing memory important when working with large datasets in numpy?

ATo reduce the chance of running out of memory and slowing down computations
BBecause numpy automatically frees memory so manual management is not needed
CTo make the code run slower and use more CPU
DBecause memory management only matters for small datasets
Attempts:
2 left
💡 Hint

Think about what happens if your computer runs out of memory.

🔧 Debug
advanced
2:00remaining
Identifying memory inefficiency in numpy code

Which option shows the code that wastes the most memory when creating large arrays?

NumPy
import numpy as np

# Option A
arr_a = np.arange(1000000, dtype=np.int32)

# Option B
arr_b = np.arange(1000000, dtype=np.int64)

# Option C
arr_c = np.arange(1000000, dtype=np.float64)

# Option D
arr_d = np.arange(1000000, dtype=np.int16)
Aarr_a uses 4MB, arr_b uses 8MB, arr_c uses 8MB, arr_d uses 2MB; all use the same memory
Barr_a uses 4MB, arr_b uses 8MB, arr_c uses 8MB, arr_d uses 2MB; arr_b and arr_c waste the most memory
Carr_a uses 4MB, arr_b uses 8MB, arr_c uses 8MB, arr_d uses 2MB; arr_a wastes the most memory
Darr_a uses 4MB, arr_b uses 8MB, arr_c uses 8MB, arr_d uses 2MB; arr_d wastes the most memory
Attempts:
2 left
💡 Hint

Check the size in bytes of each data type.

🚀 Application
expert
3:00remaining
Optimizing memory usage for large numpy arrays

You have a numpy array of 1 million integers ranging from 0 to 255. Which dtype should you choose to minimize memory usage without losing data?

Anp.uint8
Bnp.int32
Cnp.float32
Dnp.int64
Attempts:
2 left
💡 Hint

Consider the range of values and the size of each data type.