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

Fine tuning a Hugging Face transformer model for multi-track music generation

To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…

3 weeks ago

what are Transformer, LLM , and GPT

In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…

3 weeks ago

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

4 weeks ago

affine_grid() in pytorch

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

3 months 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:…

12 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago