Working with Binary Files in Numpy: save and load

Numpy provides efficient ways to store and retrieve data in binary format using the save and load functions. These methods are ideal for large datasets since binary files are more compact than text files.

1. Saving Data to a Binary File: save

The save function in Numpy allows you to save arrays to a binary file in the `.npy` format. This format is optimized for performance and retains the array's shape, datatype, and other important properties.

    import numpy as np
    
    # Creating a sample 2D array
    data = np.array([[1.5, 2.3, 3.8],
                     [4.1, 5.2, 6.3],
                     [7.4, 8.5, 9.6]])
    
    # Saving the array to a binary file
    np.save('data.npy', data)
    print("Data saved successfully to binary file.")
        

In this example, we create a 2D Numpy array and save it to a binary file named data.npy. The `.npy` file format is specifically designed to store Numpy arrays.

Result

After running the above code, a binary file data.npy will be created. This file contains the binary representation of the Numpy array.

2. Loading Data from a Binary File: load

The load function is used to load data from a binary file back into a Numpy array. The function can also be used to load arrays stored in `.npy` or `.npz` files.

    # Loading the data from the binary file
    loaded_data = np.load('data.npy')
    
    # Displaying the loaded data
    print("Loaded data:")
    print(loaded_data)
        

In this example, we load the data from the data.npy file and print it to verify that the data was loaded correctly.

Result

The output will be:

    Loaded data:
    [[1.5 2.3 3.8]
     [4.1 5.2 6.3]
     [7.4 8.5 9.6]]
        

3. Saving Multiple Arrays to a Binary File: savez

If you want to save multiple arrays to a single file, you can use the savez function, which stores the arrays in a compressed or uncompressed `.npz` file format.

    # Creating multiple arrays
    data1 = np.array([1.5, 2.3, 3.8])
    data2 = np.array([4.1, 5.2, 6.3])
    
    # Saving multiple arrays to a binary file
    np.savez('multiple_data.npz', array1=data1, array2=data2)
    print("Multiple arrays saved successfully.")
        

Result

The multiple_data.npz file will contain both data1 and data2 arrays. The file can be loaded and the arrays can be accessed using the respective names.

4. Loading Multiple Arrays from a Binary File: load with npz File

To load multiple arrays from a `.npz` file, you can use the load function and access the arrays by their names.

    # Loading the multiple arrays from the .npz file
    loaded_data = np.load('multiple_data.npz')
    
    # Accessing individual arrays
    array1 = loaded_data['array1']
    array2 = loaded_data['array2']
    
    # Displaying the arrays
    print("Array 1:", array1)
    print("Array 2:", array2)
        

Result

The output will be:

    Array 1: [1.5 2.3 3.8]
    Array 2: [4.1 5.2 6.3]
        

5. Handling Data Types in Binary Files

When saving data to binary files, Numpy automatically preserves the data types of the arrays. However, you can explicitly specify the data type while saving using the dtype parameter.

    # Creating an array with integer data type
    int_data = np.array([1, 2, 3, 4], dtype=int)
    
    # Saving the integer array to a binary file
    np.save('int_data.npy', int_data)
    print("Integer data saved to binary file.")
        

When you load this file using np.load, the array will retain the integer data type:

    # Loading the integer data from the binary file
    loaded_int_data = np.load('int_data.npy')
    
    # Displaying the loaded data and its type
    print("Loaded integer data:", loaded_int_data)
    print("Data type:", loaded_int_data.dtype)
        

Result

The output will be:

    Loaded integer data: [1 2 3 4]
    Data type: int64
        

Conclusion

The save and load functions in Numpy provide an efficient way to store and retrieve data in binary format. These functions are highly optimized for working with large datasets. By using the `.npy` and `.npz` file formats, you can easily manage your Numpy arrays while preserving their structure and data types.





Advertisement