0
0
NumPydata~20 mins

Memory-mapped files with np.memmap in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memmap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.memmap array modification
What is the output of the following code snippet?
NumPy
import numpy as np
filename = 'data.dat'
# Create a memmap file with 5 integers
arr = np.memmap(filename, dtype='int32', mode='w+', shape=(5,))
arr[:] = np.arange(5)
arr.flush()
# Reopen in read mode
arr2 = np.memmap(filename, dtype='int32', mode='r', shape=(5,))
print(arr2[2])
A2
B0
C3
DRaises a ValueError
Attempts:
2 left
💡 Hint
Remember that np.memmap writes data to disk and reopening in read mode reads the saved data.
data_output
intermediate
2:00remaining
Shape of np.memmap array after reshaping
Given this code, what is the shape of the memmap array after reshaping?
NumPy
import numpy as np
filename = 'data2.dat'
arr = np.memmap(filename, dtype='float64', mode='w+', shape=(12,))
arr[:] = np.arange(12)
arr.flush()
arr2 = np.memmap(filename, dtype='float64', mode='r+', shape=(3,4))
print(arr2.shape)
A(4, 3)
B(12,)
C(3, 4)
DRaises a TypeError
Attempts:
2 left
💡 Hint
The shape parameter defines how the data is viewed, not the original file size.
🔧 Debug
advanced
2:00remaining
Identify the error in np.memmap usage
What error does this code raise?
NumPy
import numpy as np
filename = 'data3.dat'
arr = np.memmap(filename, dtype='int16', mode='w+', shape=(10,))
arr[:] = np.arange(10)
arr.flush()
# Reopen with wrong dtype
arr2 = np.memmap(filename, dtype='int32', mode='r', shape=(10,))
print(arr2[0])
AValueError: cannot reshape array of size 20 into shape (10,)
BTypeError: dtype mismatch
CNo error, prints 0
DValueError: buffer size mismatch
Attempts:
2 left
💡 Hint
The file was saved with int16 (2 bytes per element), but reopened as int32 (4 bytes per element).
🚀 Application
advanced
3:00remaining
Efficiently modifying large data with np.memmap
You have a large binary file with 1 million float64 numbers. You want to add 10 to every element without loading the entire file into memory. Which code snippet correctly does this?
A
arr = np.memmap('large.dat', dtype='float64', mode='r')
arr += 10
arr.flush()
B
arr = np.memmap('large.dat', dtype='float64', mode='r+')
for i in range(len(arr)):
    arr[i] += 10
arr.flush()
C
arr = np.memmap('large.dat', dtype='float64', mode='r+')
arr += 10
arr.flush()
D
arr = np.memmap('large.dat', dtype='float64', mode='w+')
arr[:] = 10
arr.flush()
Attempts:
2 left
💡 Hint
Mode 'r+' allows read and write. Vectorized operations may load all data into memory.
🧠 Conceptual
expert
2:00remaining
Understanding np.memmap file persistence
Which statement about np.memmap file persistence is TRUE?
AData changes in a memmap array are saved to disk only after calling flush()
BData changes in a memmap array are immediately saved to disk without flush()
CMemmap arrays do not save data to disk; they only cache in memory
DCalling flush() deletes the memmap file from disk
Attempts:
2 left
💡 Hint
Think about when data is guaranteed to be written to disk.