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.


0 Comments

Leave a Reply

Avatar placeholder