What is Numpy memmap? Explained With Examples

Hello geeks, and welcome to this article. Today, we will cover the NumPy memmap(). Along with that, we will also look at its syntax and parameters for a better overall understanding. Then, we will see the application of all the theory parts through a couple of examples. But first, let us try what memmap means on its own.

“memmap” stands for memory map, and it creates a memory map to an array stored in a binary file on disk. These files are used to access a small portion of large files on disk. They are similar to the array of objects.

Next, we will cover the syntax associated with this.

Syntax Of Numpy Memmap


Above, we can see the general syntax of NumPy memmap(). Next, we will discuss the various parameters associated with it.

Parameter Of Numpy Memmap

Now, we are done with the syntax in this section. We will look at the various parameters associated with it. We will be discussing each of them in detail, along with the data type they accept.

1. filename: str, file-like object

This parameter represents the file name or object on which the operation must be performed.

2. dtype: data-type

This is an optional parameter. It represents the data type used for interpreting the file constant.

3. Offset:int

This parameter represents the offset at which the data starts in a particular file. This is measured in bytes and is generally a multiple of the byte size of dtype. By default, it will start at the beginning of any file.

4. Shape: Tuple

This parameter represents the desired shape of the array. By default, the returned will array will be 1-dimensional with the number of elements by file size and data type.

5. order:{“c” and “f”}

This parameter specifies the order of the ndarray memory layout. By default, it is set to “C”.

6. Mode:{“r+”, “r”, “w+”, “c”}

Here, different symbols represent different values that we will cover here

  • r: open existing file for reading only
  • r+: Open existing file for reading and writing
  • w+: Create or overwrite an existing file for reading and writing.
  • c: This represents copy on write


As we are done with all the theory portions related to NumPy memmap(), in this section, we will be looking at how this function works and how it helps us achieve our desired output.

We will start with an elementary-level example and gradually move our way to more complicated examples.

import numpy as ppool
from tempfile import mkdtemp
import os.path as path

name=path.join(mkdtemp(), 'newfile.dat')
fh = ppool.memmap(name, dtype='float32', mode='w+', shape=(4,4))

In the above example, at first, we have imported the NumPy module. After this, we have imported the Mkdtemp module and os.path, which will help us create a temporary file for our example. Next, after creating our file, we have used the syntax as discussed above for our function. In the end, the output justifies our input.

Now let us look at one more example.

import numpy as ppool
from tempfile import mkdtemp
import os.path as path

name=path.join(mkdtemp(), 'newfile.dat')
fh = ppool.memmap(name, dtype='float16', mode='w+', shape=(3,2))
Example of numpy memmap

The above example is somewhat similar to the first example. Here we have just used float16 instead of 32 and changed the shape of the array.

Try twisting and turning the syntax on your own. This way you will explore new things and learn a lot more.

Must Read


In this article, we have covered NumPy memmap(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples.

We varied the syntax and looked at the output for each case. We also understood that NumPy memmap() helps us create arrays directly mapped into an array. I hope this article was able to clear all doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.

If you’re done reading this, why not read about Zfill up next?

Notify of
Inline Feedbacks
View all comments