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

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

4 weeks ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

10 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

11 months ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

12 months ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

1 year ago

Topic Modeling using Latent Dirichlet Allocation with Python

Topic modeling is a subcategory of unsupervised machine learning method, and a clustering task in…

1 year ago