In this tutorial, we will learn about how to load data in numpy. This won’t be limited to just loading information from the external directories, but we will also learn how to save arrays in numpy.

I/O in Numpy

Numpy has an available format for ndarray objects inside it known as .npy format. This particular format (.npy) can store file data, knows the shape, data type and other different type of information that helps to build an ndarray object inside the disk. If the process is successful, then the same ndarray array object can be fetched or transferred from or to another computer even it has a different build.

Reasons for I/O in Numpy

Many fields like finance, medical, research or even business domains use different kind of numeric data. Although the data maybe numeric, but it can come from different sources like SQL Databases, Pytables, CSV files etc. Numpy is all-ready to process this challenge by using the np.load() function to load data of different format files into numpy for processing.

This feature is highly relevant to the numpy functionality since the creators must have designed numpy in a way that it is independent to work along different data/numeric formats and be more robust and efficient.

Saving an Array

To save an array in numpy, we use the method known as numpy.save() which saves the file in your main directory (selected by default by the code editor that you are using)

load data in numpy

Let’s look at a quick example of how to use histograms in numpy:

#The numpy.save() will save the file in the directory with a npy extension.

import numpy as np 
arr = np.array([1,2,3,4,5]) 


This code will save the file ‘my_array’ in the main code editor’s directory with an extension of ‘npy’.

Loading an Array

Now in order to fetch or load the array that we have saved in the ‘my_array’ file, we will use the numpy.load method. To use the function, we will use the following lines of code:

load_file = np.load('my_array.npy') 


[1 2 3 4 5]