In this tutorial, we will learn about the ‘NumPy.load’. where we use the ‘NumPy.load’ and how to use it along with examples.
what is ‘NumPy.load()’ ?
‘NumPy.load()’ is a function in the NumPy library, a fundamental package for scientific computing in Python. The NumPy.load() loads arrays or pickled objects from the files with .npy and .npz extensions to the program.
How does ‘NumPy.load()’ work?
when you use ‘NumPy.load()’, that means you are loading data that has been previously saved in a file. here is how we can use it:
1. Loading a ‘.npy ‘file:
we use ‘.npy’ for saving a single ‘numPy’ array.
import numpy as np #assume you have a saved in 'content.npy' content=np.load('content.npy') print(content)
2. Loading an ‘.npz’ file:
we use ‘.npz’ for storing multiple ‘numPy’ arrays.
import numpy as np #assume you have saved multiple arrays in 'content.npz' content=np.load('content.npz') #accessing individual arrays array1=content['arr_0'] array2=content['arr_1'] print(array1) print(array2)
3. Loading pickled objects:
you can also load other Python objects that were saved using NumPy’s ‘save’ function with ‘allow_pickle=True’.
import numpy as np #assume you have saved the pickled object in 'content.pk' content=np.load('content.pk',allow_pickle=True) print(content)
Example:
import numpy as np #loading a single array from the '.npy' file simple_array=np.load('simple_array.npy') print("simple array:",simple_array) #loading a multiple arrays from '.npz' file with np.load('complex_arrays.npz') as content: first_array=content['arr_0'] second_array=content['arr_1'] print("first array:", first_array) print("second array:", second_array) #loading pickled object pickled_content=np.load('pickled_content.pk',allow pickle=True) print("pickled content:" pickled_content)