0
0
NumPydata~10 mins

Memory-mapped files with np.memmap in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a memory-mapped file named 'data.dat' with shape (100,) and dtype float32.

NumPy
import numpy as np
mmap_array = np.memmap('data.dat', dtype=[1], mode='w+', shape=(100,))
Drag options to blanks, or click blank then click option'
A'int64'
B'float32'
C'bool'
D'complex128'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer dtype instead of float32.
Forgetting to specify the dtype parameter.
2fill in blank
medium

Complete the code to open an existing memory-mapped file 'data.dat' in read-only mode.

NumPy
mmap_read = np.memmap('data.dat', dtype='float32', mode=[1], shape=(100,))
Drag options to blanks, or click blank then click option'
A'r'
B'w+'
C'c'
D'w'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w+' which allows writing and truncates the file.
Using 'c' which allows copy-on-write but not pure read-only.
3fill in blank
hard

Fix the error in the code to correctly create a memory-mapped file with shape (50, 2).

NumPy
mmap_array = np.memmap('data2.dat', dtype='float64', mode='w+', shape=[1])
Drag options to blanks, or click blank then click option'
A50, 2
B[50, 2]
C(50, 2)
D50
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple for shape.
Passing multiple arguments instead of a single tuple.
4fill in blank
hard

Fill both blanks to create a memory-mapped file 'data3.dat' with dtype int32 and shape (10, 10).

NumPy
mmap_file = np.memmap('data3.dat', dtype=[1], mode='w+', shape=[2])
Drag options to blanks, or click blank then click option'
A'int32'
B'float64'
C(10, 10)
D[10, 10]
Attempts:
3 left
💡 Hint
Common Mistakes
Using float64 instead of int32 for dtype.
Using a list instead of a tuple for shape.
5fill in blank
hard

Fill all three blanks to open an existing memory-mapped file 'data4.dat' with dtype float64, shape (5, 5), in copy-on-write mode.

NumPy
mmap_copy = np.memmap('data4.dat', dtype=[1], mode=[2], shape=[3])
Drag options to blanks, or click blank then click option'
A'float32'
B'c'
C(5, 5)
D'float64'
Attempts:
3 left
💡 Hint
Common Mistakes
Using float32 instead of float64 for dtype.
Using 'w+' or 'r' instead of 'c' for mode.
Using a list instead of a tuple for shape.