You can use the h5py library to input and output NumPy arrays to and from HDF5 files (.h5) in Python. HDF5 is a versatile file format commonly used for storing large datasets, including NumPy arrays. Here's how to do it:
import h5py import numpy as np # Create a NumPy array data = np.array([1, 2, 3, 4, 5]) # Open an HDF5 file for writing with h5py.File('mydata.h5', 'w') as f: # Create a dataset inside the file and write the NumPy array f.create_dataset('my_dataset', data=data) In this example:
We import h5py and NumPy (numpy) libraries.
We create a NumPy array named data.
We open an HDF5 file named 'mydata.h5' for writing using a context manager (with statement).
Inside the file, we create a dataset named 'my_dataset' and write the NumPy array data to it.
import h5py # Open an HDF5 file for reading with h5py.File('mydata.h5', 'r') as f: # Access the dataset and read it into a NumPy array loaded_data = np.array(f['my_dataset']) # Now, 'loaded_data' contains the NumPy array from the HDF5 file print(loaded_data) In this code:
We open the same HDF5 file, 'mydata.h5', but this time for reading.
We access the dataset 'my_dataset' inside the file and read its data into a NumPy array called loaded_data.
loaded_data now contains the data from the HDF5 file.
Make sure to replace 'mydata.h5' with the actual filename and dataset name you are working with.
With h5py, you can also store and retrieve multi-dimensional arrays, groups, and attributes within HDF5 files, making it a powerful tool for handling complex datasets.
How to save a NumPy array to an HDF5 file using h5py in Python:
import numpy as np import h5py # Create a sample NumPy array arr = np.arange(10) # Save the array to an HDF5 file with h5py.File('data.h5', 'w') as f: f.create_dataset('dataset_name', data=arr) Python code to load a NumPy array from an HDF5 file using h5py:
import h5py # Open the HDF5 file in read mode with h5py.File('data.h5', 'r') as f: # Access the dataset dataset = f['dataset_name'] # Convert the dataset to a NumPy array arr = dataset[:] How to append NumPy arrays to an existing HDF5 dataset using h5py:
import numpy as np import h5py # Open the HDF5 file in append mode with h5py.File('data.h5', 'a') as f: # Access the dataset dataset = f['dataset_name'] # Create a new NumPy array new_data = np.arange(10, 20) # Append the new data to the dataset dataset.resize((dataset.shape[0] + new_data.shape[0],)) dataset[-new_data.shape[0]:] = new_data Python code to read metadata attributes from an HDF5 dataset using h5py:
import h5py # Open the HDF5 file in read mode with h5py.File('data.h5', 'r') as f: # Access the dataset dataset = f['dataset_name'] # Read metadata attributes metadata = dataset.attrs # Print metadata print(metadata) How to create a new HDF5 file and save multiple NumPy arrays using h5py in Python:
import numpy as np import h5py # Create sample NumPy arrays arr1 = np.arange(10) arr2 = np.random.random((5, 5)) # Save arrays to an HDF5 file with h5py.File('data.h5', 'w') as f: f.create_dataset('array1', data=arr1) f.create_dataset('array2', data=arr2) Python code to read all datasets from an HDF5 file using h5py:
import h5py # Open the HDF5 file in read mode with h5py.File('data.h5', 'r') as f: # Iterate over all datasets for name in f: dataset = f[name] # Convert dataset to a NumPy array arr = dataset[:] print(f'Dataset {name}: {arr}') How to compress and save a NumPy array to an HDF5 file using h5py in Python:
import numpy as np import h5py # Create a sample NumPy array arr = np.random.random((100, 100)) # Save the compressed array to an HDF5 file with h5py.File('data.h5', 'w') as f: f.create_dataset('compressed_array', data=arr, compression='gzip', compression_opts=9) Python code to read a specific slice of data from an HDF5 dataset using h5py:
import h5py # Open the HDF5 file in read mode with h5py.File('data.h5', 'r') as f: # Access the dataset dataset = f['dataset_name'] # Read a specific slice of data slice_data = dataset[5:10] print(slice_data) How to save a NumPy array with custom metadata attributes to an HDF5 file using h5py:
import numpy as np import h5py # Create a sample NumPy array arr = np.random.random((100, 100)) # Save the array with metadata to an HDF5 file with h5py.File('data.h5', 'w') as f: dset = f.create_dataset('array_with_metadata', data=arr) # Add custom metadata attributes dset.attrs['description'] = 'Random data' dset.attrs['author'] = 'John Doe' Python code to read metadata attributes from an HDF5 file using h5py:
import h5py # Open the HDF5 file in read mode with h5py.File('data.h5', 'r') as f: # Read metadata attributes metadata = f.attrs # Print metadata print(metadata) outer-join web-testing rxjs6 valgrind video-player keystore multer alter-column dd bc