Numpy load – Python NumPy load() Function

NumPy load() Function:

Numpy load: To load arrays or pickled objects from .npy files use the load() function of the NumPy module.

Syntax:

numpy.load(file, mmap_mode=None)

Parameters

file: This is required. It indicates the file to read. The seek() and read() methods must be supported by file-like objects.

mmap_mode: This is optional. None, ‘r+’, ‘r’, ‘w+’, and ‘c’ are all possible values. None is the default value. If not None, memory-map the file using the specified mode. On disk, a memory-mapped array is stored. It can, however, be accessed and sliced just like any other ndarray. Memory mapping is particularly useful for accessing small chunks of huge files without having to read the entire file into memory.

Return value

The data read from the.npy file is returned.

NumPy load() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function of the Numpy module to create an array.
  • Store it in a variable.
  • Print the above given array.
  • Save the above given array with some random filename in binary file with .npy extension using the save() function of the Numpy module.
  • Load the above file using the load() function of the Numpy module
  • Print the above file content.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function
# of the Numpy module to create an array. 
# Store it in a variable.
gvn_arry = np.array([1, 12, 35, 20, 70])                     
# Print the above given array.
print("The above given array is:")
print(gvn_arry)

# Save the above given array with some random filename in binary file
# with .npy extension using the save() function of the Numpy module
np.save("samplefile", gvn_arry)

# Load the above file using the load() function of the Numpy module
file_content = np.load("samplefile.npy")
print()
# Print the above file content
print("The above file content:")
print(file_content)

Output:

The above given array is:
[ 1 12 35 20 70]

The above file content:
[ 1 12 35 20 70]

Example2

Approach:

  • Import numpy module using the import keyword
  • Open some random file(.npy file) in write-binary mode using the open() function and store it in a variable.
  • Pass some random list as an argument to the array() function of the Numpy module to create an array.
  • Store it in another variable.
  • Save the above array into the above given file with .npy extension using the save() function of the Numpy module
  • Close the file using the close() function
  • Open the above saved file in read-binary mode using the open() function and store it in a variable.
  • Load the above file using the load() function of the Numpy module and
  • store it in another variable.
  • Print the above file content
  • Close the file using the close() function.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Open some random file(.npy file) in write-binary mode using the open() function
# and store it in a variable.
gvn_file = open("samplefile.npy", "wb")
# Pass some random list as an argument to the array() function
# of the Numpy module to create an array. 
# Store it in another variable.
gvn_arry = np.array([1, 12, 35, 20, 70])
# Save the above array into the above given file with .npy extension
# using the save() function of the Numpy module
np.save(gvn_file, gvn_arry)
# Close the file using the close() function
gvn_file.close()

# Open the above saved file in read-binary mode using the open() function
# and store it in a variable.
gvn_file = open("samplefile.npy", "rb")
# Load the above file using the load() function of the Numpy module and 
# store it in another variable.
file_content = np.load(gvn_file)
# Print the above file content
print("The above file content:")
print(file_content)
# Close the file using the close() function
gvn_file.close()

Output:

The above file content:
[ 1 12 35 20 70]