Warning: opendir(/home/idphekra/public_html/rdatacode.com/wp-content/cache/db/options//870): failed to open dir: No such file or directory in /home/idphekra/public_html/rdatacode.com/wp-content/plugins/w3-total-cache/Util_File.php on line 158
How to save and read Numpy array in Python - We provide R, Python, Statistics Online-Learning Course
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 source files for R Machine learning

Click here to go to source files for R Machine Learning

2 days ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

4 weeks ago

Install PyTorch on Windows

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

1 month ago

Topic Modeling using Latent Dirichlet Allocation with Python

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

2 months ago

Document sentiment classification using bag-of-words in Python

For online Python training registration, click here ! Sentiment classification is a type of machine…

2 months ago

Download R Course source files

Click here to download R Course source files !

11 months ago