Hello geeks and welcome in this article, we will cover the NumPy memmap(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But at 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
numpy.memmap(filename,dtype="")
Here above we can see the general syntax of NumPy memmap(). Next we will discuss the various parameters associated with.
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 file object on which the operation needs to 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
EXAMPLE
As we are done with all the theory portion 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.
#input 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)) print(fh)
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)) print(fh)
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
- NUMPY INSERT IN PYTHON WITH EXAMPLES
- PYTHON DIVMOD AND ITS APPLICATION
- NUMPY POLYFIT EXPLAINED WITH EXAMPLES
- WHAT IS NUMPY CHOOSE()? EXPLAINED IN DETAIL
- NUMPY QUANTILE() EXPLAINED WITH EXAMPLES
CONCLUSION
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 in case you have any unsolved queries feel free to write them below in the comment section. Done reading this why not read about Zfill up next.
The post What is Numpy memmap? Explained with examples appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment