Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a memory-mapped file in the context of numpy's np.memmap?
A memory-mapped file is a way to access small parts of large files on disk as if they were in memory, without loading the entire file. np.memmap lets numpy treat disk files like arrays, saving memory and speeding up access.
Click to reveal answer
beginner
How does np.memmap help when working with large datasets?
np.memmap allows you to work with large datasets by loading only parts of the data into memory on demand, instead of loading the whole dataset at once. This reduces memory use and can improve performance.
Click to reveal answer
intermediate
What are the key parameters when creating a np.memmap object?
Key parameters include: filename (path to the file), dtype (data type of the array), mode ('r', 'r+', 'w+', etc. for read/write), offset (where to start reading), and shape (array dimensions).
Click to reveal answer
beginner
True or False: Changes made to a np.memmap array are immediately saved to the disk file.
True. When you modify a np.memmap array, the changes are written directly to the disk file, making it useful for large data that doesn't fit in memory.
Click to reveal answer
intermediate
Why might you choose np.memmap over loading a numpy array with np.load?
np.memmap is better for very large files because it doesn't load the entire file into memory. np.load loads the whole array into RAM, which can cause memory errors with big data.
Click to reveal answer
What does np.memmap primarily help with?
ACreating plots from numpy arrays
BCompressing numpy arrays
CSorting numpy arrays faster
DAccessing large files without loading all data into memory
✗ Incorrect
np.memmap allows working with large files by mapping them to memory, avoiding loading the entire file.
Which mode in np.memmap allows read and write access to the file?
Ar+
Bw+
Cc
Dr
✗ Incorrect
Mode 'r+' opens the file for reading and writing without truncating it.
If you want to start reading a np.memmap file from byte 1000, which parameter do you use?
Ashape
Boffset
Cdtype
Dmode
✗ Incorrect
The offset parameter sets the starting byte position in the file.
True or False: np.memmap loads the entire file into RAM immediately.
AFalse
BDepends on dtype
COnly for small files
DTrue
✗ Incorrect
np.memmap loads data on demand, not the entire file at once.
What happens when you modify data in a np.memmap array?
AChanges are saved only in memory
BChanges are lost after program ends
CChanges are saved to disk immediately
Dnp.memmap arrays are read-only
✗ Incorrect
Modifications to np.memmap arrays write directly to the disk file.
Explain how np.memmap helps manage large datasets that don't fit into memory.
Think about how you can open a huge book and read only one page at a time.
You got /4 concepts.
Describe the main parameters needed to create a np.memmap object and their roles.
Consider what you need to tell numpy about the file and how you want to use it.
You got /5 concepts.
Practice
(1/5)
1. What is the main benefit of using np.memmap in data science?
easy
A. It allows working with large arrays stored on disk without loading all data into memory.
B. It automatically speeds up all calculations by using GPU acceleration.
C. It compresses data files to save disk space.
D. It converts arrays into Python lists for easier manipulation.
Solution
Step 1: Understand what np.memmap does
np.memmap creates an array-like object that accesses data stored on disk instead of loading it fully into memory.
Step 2: Identify the main advantage
This allows handling very large datasets without using large amounts of RAM, which is the main benefit.
Final Answer:
It allows working with large arrays stored on disk without loading all data into memory. -> Option A
Quick Check:
Memory-mapped files save RAM by accessing disk data [OK]
Hint: Remember: memmap works with disk data like memory arrays [OK]
Common Mistakes:
Thinking memmap compresses data
Assuming memmap loads all data into RAM
Confusing memmap with GPU acceleration
2. Which of the following is the correct way to create a new memory-mapped file with np.memmap of shape (100, 100) and dtype float32?
easy
A. np.memmap('data.dat', dtype='float64', mode='w+', shape=(100, 100))
B. np.memmap('data.dat', dtype='float32', mode='r', shape=(100, 100))
C. np.memmap('data.dat', dtype='float32', mode='rw', shape=(100, 100))
D. np.memmap('data.dat', dtype='float32', mode='w+', shape=(100, 100))
Solution
Step 1: Check the mode for creating a new file
Mode 'w+' creates a new file or overwrites existing one for reading and writing.
Step 2: Verify dtype and shape parameters
The dtype should be 'float32' and shape (100, 100) as given.
Final Answer:
np.memmap('data.dat', dtype='float32', mode='w+', shape=(100, 100)) -> Option D
Quick Check:
Use mode='w+' to create new memmap files [OK]
Hint: Use mode='w+' to create or overwrite memmap files [OK]
Common Mistakes:
Using mode='r' when creating a new file
Using incorrect dtype like float64 instead of float32
np.arange(9).reshape(3,3) creates a 3x3 array:
[[0,1,2],[3,4,5],[6,7,8]]
Step 2: Identify the value at position [1,2]
Row 1, column 2 is the third element in second row, which is 5.
Final Answer:
5 -> Option B
Quick Check:
Index [1,2] in arange(9).reshape(3,3) = 5 [OK]
Hint: Remember zero-based indexing for rows and columns [OK]
Common Mistakes:
Confusing row and column indices
Forgetting zero-based indexing
Assuming flush() changes data values
4. Identify the error in this code snippet that tries to open a memmap file:
import numpy as np
filename = 'data.dat'
# Attempt to open memmap file
fp = np.memmap(filename, dtype='float64', mode='r+', shape=(10,10))
print(fp[0,0])
medium
A. File 'data.dat' does not exist, so mode 'r+' causes an error.
B. dtype 'float64' is not supported by np.memmap.
C. Shape parameter must be omitted when opening existing memmap files.
D. Mode 'r+' is read-only and cannot write to file.
Solution
Step 1: Understand mode 'r+'
Mode 'r+' opens an existing file for reading and writing. If file does not exist, it raises an error.
Step 2: Check file existence
If 'data.dat' does not exist, this code will raise a FileNotFoundError.
Final Answer:
File 'data.dat' does not exist, so mode 'r+' causes an error. -> Option A
Quick Check:
Mode 'r+' requires existing file [OK]
Hint: Use mode='w+' to create files, 'r+' needs existing file [OK]
Common Mistakes:
Assuming 'r+' creates new files
Thinking dtype 'float64' is invalid
Believing shape must be omitted always
5. You have a very large dataset stored in a binary file 'large_data.dat' with shape (10000, 10000) and dtype float64. You want to compute the mean of the first column without loading the entire file into memory. Which approach using np.memmap is best?
hard
A. Open the file with mode='w+' and overwrite data before computing mean.
B. Load the entire file into a numpy array and then compute the mean of the first column.
C. Open the file with mode='r' and read only the first column slice to compute the mean.
D. Use np.memmap with mode='c' and compute mean on the whole array.
Solution
Step 1: Understand memory constraints
The dataset is very large (10000x10000), so loading all data into memory is inefficient.
Step 2: Use memmap to read only needed data
Opening with mode='r' allows read-only access. Slicing the first column reads only that part from disk, saving memory.
Step 3: Avoid unnecessary writes or full reads
Mode 'w+' overwrites data, which is not desired. Mode 'c' is copy-on-write and still loads data. Loading full array wastes memory.
Final Answer:
Open the file with mode='r' and read only the first column slice to compute the mean. -> Option C
Quick Check:
Read-only memmap + slice = efficient mean calculation [OK]
Hint: Read only needed slices with mode='r' to save memory [OK]