A memory-mapped array is a way to access large data stored on disk as if it were in memory, without loading the entire data into RAM. It allows working with data too big to fit in memory by loading only needed parts.
Click to reveal answer
beginner
How do you create a memory-mapped array in NumPy?
Use numpy.memmap(filename, dtype, mode, shape) to create a memory-mapped array. It links the array to a file on disk, specifying data type, access mode, and shape.
Click to reveal answer
beginner
Why use memory-mapped arrays instead of loading the whole file into memory?
Memory-mapped arrays let you work with files larger than your RAM by loading only parts you need. This saves memory and speeds up processing for big data.
Click to reveal answer
intermediate
What does the mode parameter in numpy.memmap control?
The mode controls how the file is opened: 'r' for read-only, 'r+' for read-write, 'w+' to create or overwrite, and 'c' for copy-on-write.
Click to reveal answer
intermediate
Can you modify data in a memory-mapped array?
Yes, if you open the memory-mapped array in a write mode like 'r+' or 'w+', you can change data and it will be saved back to the file on disk.
Click to reveal answer
What is the main advantage of using a memory-mapped array?
AIt speeds up CPU calculations by using GPU
BIt allows working with data larger than RAM by loading parts on demand
CIt automatically cleans data errors
DIt compresses data to save disk space
✗ Incorrect
Memory-mapped arrays let you access large files without loading all data into RAM, improving memory use.
Which NumPy function creates a memory-mapped array?
Anumpy.memmap()
Bnumpy.loadtxt()
Cnumpy.array()
Dnumpy.fromfile()
✗ Incorrect
numpy.memmap() creates a memory-mapped array linked to a file.
If you want to modify a memory-mapped array and save changes, which mode should you use?
A'r+'
B'c'
C'r'
D'r--'
✗ Incorrect
Mode 'r+' opens the file for reading and writing, allowing changes to be saved.
What happens if you open a memory-mapped array with mode 'c'?
AOverwrite the file
BRead-only access
CCopy-on-write, changes not saved to disk
DAppend data to the file
✗ Incorrect
Mode 'c' creates a copy-on-write mapping; changes are not saved back to the file.
Which of these is NOT a benefit of memory-mapped arrays?
AHandling files larger than RAM
BSaving memory by loading data on demand
CFaster access to small parts of large files
DAutomatic data cleaning
✗ Incorrect
Memory-mapped arrays do not clean data; they only manage how data is loaded and accessed.
Explain how memory-mapped arrays help when working with very large datasets.
Think about how you might read a huge book by opening only the pages you need.
You got /3 concepts.
Describe the steps to create and use a memory-mapped array in NumPy.
Remember the function and what parameters it needs.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of using numpy.memmap for large data files?
easy
A. It allows working with data without loading the entire file into memory.
B. It compresses the data to save disk space.
C. It automatically cleans up unused files.
D. It speeds up CPU calculations by using GPU.
Solution
Step 1: Understand memory usage with large files
Loading large files fully into RAM can cause memory errors or slowdowns.
Step 2: Role of numpy.memmap
Memory-mapped arrays let you access parts of the file on disk as if they were in memory, avoiding full loading.
Final Answer:
It allows working with data without loading the entire file into memory. -> Option A
Quick Check:
Memory-mapping = Partial loading [OK]
Hint: Memory-mapping means partial file loading, not full load [OK]
Common Mistakes:
Thinking memmap compresses data
Assuming memmap speeds up CPU/GPU
Believing memmap auto-deletes files
2. Which of the following is the correct way to create a memory-mapped array from a file named data.dat with shape (100, 100) and dtype float64?
easy
A. np.memmap('data.dat', shape=(100, 100), dtype='int32', mode='w+')
B. np.memmap('data.dat', dtype='float64', mode='r+', shape=(100, 100))
C. np.memmap('data.dat', dtype='float64', shape=(100, 100))
D. np.memmap('data.dat', mode='r', shape=(100, 100), dtype='float32')
Solution
Step 1: Check required parameters for memmap
Must specify filename, dtype, mode, and shape for memmap creation.
Step 2: Validate each option
np.memmap('data.dat', dtype='float64', mode='r+', shape=(100, 100)) correctly uses dtype='float64', mode='r+', and shape=(100, 100). Others have wrong dtype or missing mode.
Final Answer:
np.memmap('data.dat', dtype='float64', mode='r+', shape=(100, 100)) -> Option B
5. You have a large dataset stored in a binary file large_data.dat with shape (10000, 10000) and dtype float32. You want to increment all values by 1 without loading the entire file into memory. Which approach using numpy.memmap is best?
hard
A. Create a memmap with mode 'r+', loop over rows, increment each row, flush after each.
B. Load entire file with np.fromfile, add 1, then save back.
C. Open memmap with mode 'w+', increment only first 100 elements, flush once.
D. Use memmap with mode 'r', add 1 to the array, then flush.
Solution
Step 1: Avoid loading entire file
Loading full 10000x10000 array uses too much RAM, so avoid np.fromfile full load.
Step 2: Use memmap with mode 'r+' and process in chunks
Memmap with 'r+' allows read/write access. Looping over rows increments data without full load. Flushing saves changes.
Step 3: Evaluate other options
Load entire file with np.fromfile, add 1, then save back. loads full file (bad for memory). Open memmap with mode 'w+', increment only first 100 elements, flush once. only increments part of data. Use memmap with mode 'r', add 1 to the array, then flush. uses 'r' mode (read-only), so cannot modify.
Final Answer:
Create a memmap with mode 'r+', loop over rows, increment each row, flush after each. -> Option A
Quick Check:
Memmap + chunk update + flush = Create a memmap with mode 'r+', loop over rows, increment each row, flush after each. [OK]
Hint: Use memmap with 'r+' and update in chunks to save memory [OK]
Common Mistakes:
Loading entire file into memory
Using read-only mode for writing
Modifying only part of data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.