Categories: NumpyPython

How to save and read Numpy array in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

When you are doing data analysis with Numpy module in Python, it is not uncommon that the array data information needs to be stored on local driver, then it is possible to read it again later into working session. Numpy provides two functions on this purpose. save() function is used to save an Numpy array into binary format, with ‘npy’ extension in the file. save() function does the opposite operation, namely to read a binary format file into Python session and return as an Numpy array.

#Import Numpy module
import numpy as np
#create an array of shape(6,6)
arr1 = np.random.random(36)
arr1 = arr1.reshape(6,6)
arr1
#output
array([[0.26560098, 0.38050862, 0.42372644, 0.10164516, 0.33201436,
        0.82577639],
       [0.13577651, 0.74927784, 0.88341742, 0.95773475, 0.4315829 ,
        0.70086377],
       [0.68581215, 0.85567562, 0.11510042, 0.60318129, 0.03797972,
        0.9735223 ],
       [0.43650332, 0.02414757, 0.48002342, 0.92832013, 0.01598822,
        0.19135038],
       [0.55913271, 0.81924688, 0.69758161, 0.30405608, 0.02600315,
        0.72799825],
       [0.12714053, 0.57794667, 0.67743952, 0.66171684, 0.32395517,
        0.84255039]])
#save array to a file 'saved_arr1.npy' in the same working directory on computer
np.save('saved_arr1', arr1)
#load saved file into another array
arr2 = np.load('saved_arr1.npy')
arr2
#output
array([[0.26560098, 0.38050862, 0.42372644, 0.10164516, 0.33201436,
        0.82577639],
       [0.13577651, 0.74927784, 0.88341742, 0.95773475, 0.4315829 ,
        0.70086377],
       [0.68581215, 0.85567562, 0.11510042, 0.60318129, 0.03797972,
        0.9735223 ],
       [0.43650332, 0.02414757, 0.48002342, 0.92832013, 0.01598822,
        0.19135038],
       [0.55913271, 0.81924688, 0.69758161, 0.30405608, 0.02600315,
        0.72799825],
       [0.12714053, 0.57794667, 0.67743952, 0.66171684, 0.32395517,
        0.84255039]])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago