NumPy > Save & Load

Name
Code
Output
# create array
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)

# save array to uncompressed .npy file
numpy.save('array_file', array)

# list directory
for entry in os.scandir('.'):
    print(entry.name)

# load array from file
array_loaded = numpy.load('array_file.npy')
print ("array from file \n",array_loaded)
array 
 [[2 4 2]
 [4 0 0]]
array_file.npy
array from file 
 [[2 4 2]
 [4 0 0]]
# create aray
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)

# save array to compressed .npz file
numpy.savez('array_file_compr', array_data = array)

#list directory
for entry in os.scandir('.'):
    print(entry.name)

# load array from file
array_loaded = numpy.load('array_file_compr.npz')['array_data']
print ("array from file \n",array_loaded)
array 
 [[1 4 4]
 [0 3 0]]
array_file_compr.npz
array from file 
 [[1 4 4]
 [0 3 0]]