Data science needs fast computation and transformation of data. Native NumPy objects in Python provides that advantage over regular programming objects. It works for as simple a task as reading numeric data set from a file on the disk. We demonstrate it in few easy lines of code.
Numpy, short for Numerical Python, is the fundamental package required for high performance scientific computing and data analysis in Python ecosystem. It is the foundation on which nearly all of the higher-level tools such as Pandas and scikit-learn are built. TensorFlow uses NumPy arrays as the fundamental building block on top of which they built their Tensor objects and graphflow for deep learning tasks (which makes heavy use of linear algebra operations on a long list/vector/matrix of numbers).
Many articles have been written demonstrating the advantage of Numpy array over plain vanilla Python lists. You will often come across this assertion in the data science, machine learning, and Python community that Numpy is much faster due to its vectorized implementation and due to the fact that many of its core routines are written in C (based on CPython framework). And it is indeed true (this article is a beautiful demonstration of various options that one can work with Numpy, even writing bare-bone C routines with Numpy APIs). Numpy arrays are densely packed arrays of homogeneous type. Python lists, by contrast, are arrays of pointers to objects, even when all of them are of the same type. You get the benefits of locality of reference.
In one of my highly cited articles on Towards Data Science platform, I demonstrated the advantage of using Numpy vectorized operations over traditional programming constructs like for-loop.
However, what is less appreciated is the fact, when it comes to repeated reading of the same data from a local (or networked) disk storage, Numpy offers another gem called .npy file format. This file format makes incredibly fast reading speed enhancement over reading from plain text or CSV files.
The catch is — of course you have to read the data in traditional manner for the first time and create a in-memory NumPy
ndarray object. But if you use the same CSV file for repeated reading of the same numerical data set, it makes perfect sense to store the
ndarray in a
npy file instead of reading it over and over from the original CSV.
What is this .NPY file?
It is a standard binary file format for persisting a single arbitrary NumPy array on disk. The format stores all of the shape and data type information necessary to reconstruct the array correctly even on another machine with a different architecture. The format is designed to be as simple as possible while achieving its limited goals. The implementation is intended to be pure Python and distributed as part of the main numpy package.
The format MUST be able to:
Represent all NumPy arrays including nested record arrays and object arrays.
Represent the data in its native binary form.
Be contained in a single file.
Store all of the necessary information to reconstruct the array including shape and data type on a machine of a different architecture. Both little-endian and big-endian arrays must be supported and a file with little-endian numbers will yield a little-endian array on any machine reading the file. The types must be described in terms of their actual sizes. For example, if a machine with a 64-bit C “long int” writes out an array with “long ints”, a reading machine with 32-bit C “long ints” will yield an array with 64-bit integers.
Be reverse engineered. Datasets often live longer than the programs that created them. A competent developer should be able to create a solution in his preferred programming language to read most NPY files that he has been given without much documentation.
Allow memory-mapping of the data.
Demo using simple code
As always, you can download the boiler plate code Notebook from my Github repository. Here I am showing the basic code snippet.
First, the usual method of reading the CSV file in a list and converting that to an ndarray.
So this was the first time read, which you have to do anyway. But if you are likely to use the same dataset many more times, then, after your data science process is finished, don’t forget to save the ndarray object in .npy format.
Because if you do so, the next time, reading from the disk will be blazing fast!
It does not matter if you want to load the data in some other shape,
It turns out that at least in this particular case, the file size on disk is also smaller for the .npy format.
In this article, we demonstrate the utility of using native NumPy file format .npy over CSV for reading large numerical data set. It may be an useful trick if the same CSV data file needs to be read many times.
Read more details about this file format here.
If you have any questions or ideas to share, please contact the author at tirthajyoti[AT]gmail.com. Also you can check author’s GitHub repositories for other fun code snippets in Python, R, or MATLAB and machine learning resources. If you are, like me, passionate about machine learning/data science, please feel free to add me on LinkedIn or follow me on Twitter.