With the NumPy package, we can easily solve many kinds of data processing tasks without writing complex loops. It is very helpful for us to control our code as well as the performance of the program. In this part, we want to introduce some mathematical and statistical functions.
See the following table for a listing of mathematical and statistical functions:
Loading and saving data
We can also save and load data to and from a disk, either in text or binary format, by using different supported functions in NumPy package.
Saving an array
Arrays are saved by default in an uncompressed raw binary format, with the file extension .npy by the np.save function:
>>> a = np.array([[0, 1, 2], [3, 4, 5]])>>> np.save('test1.npy', a)
Note
The library automatically assigns the .npy extension, if we omit it.
If we want to store several arrays into a single file in an uncompressed .npz format, we can use the np.savez function, as shown in the following example:
>>> a = np.arange(4)>>> b = np.arange(7)>>> np.savez('test2.npz', arr0=a, arr1=b)
The .npz file is a zipped archive of files named after the variables they contain. When we load an .npz file, we get back a dictionary-like object that can be queried for its lists of arrays: