In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use h5py open source library in python". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to use h5py open source library in python" can help you solve the problem.
First, the introduction of h6py module
Brief introduction of h6py
A HDF5 file is a container for two types of objects: dataset and group. Dataset is an array-like dataset, while group is a folder-like container, like a dictionary in python, with keys (key) and values (value), holding dataset and other group. You need to keep one sentence in mind when using h6py: groups analogy dictionary, dataset analogy array in Numpy.
Although HDF5's dataset is similar to Numpy's array in interface, it supports more transparent storage features, such as data compression, error detection, and block transmission.
Second, the use of h6py module
The file created by h6py is suffixed with: .hdf5
1. Brief introduction of h6py interface
The use of the h6py module is mainly divided into two steps:
1) create an .hdf5 type file handle (create an object) # read the file and change "w" to "r"
F=h6py.File ("myh6py.hdf5", "w")
2) create data (dataset) or group (group)
Create data (dataset):
F.create_dataset (self, name, shape=None, dtype=None, data=None, * * kwds)
Create a group (group):
Sample use of create_group (self, name, track_order=False) 2 and h6py
Create a h6py file
If import h6py# reads the file, replace w with rf=h6py.File ("myh6py.hdf5", "w")
A myh6py.hdf5 file is generated in the current directory
Create a dataset dataset
Import h6pyf=h6py.File ("myh6py.hdf5", "w") # deset1 is the name of the dataset, (20,) represents the shape of the dataset I represents the element type of the dataset d1=f.create_dataset ("dset1", (20,),'i') for key in f.keys (): print (key) print (f [key] .name) print (f [key] .shape) print (f [key] .value) output: dset1/dset1 (20,) [0000000000000000000]
Here we only create a dataset of 20 integer elements, and there is no assignment, the default is all 0, how to assign it, see the following code.
Import h6pyimport numpy as npf=h6py.File ("myh6py.hdf5", "w") d1=f.create_dataset ("dset1", (20,) 'i') # assign D1 [...] = np.arange (20) # or we can directly create a dataset and assign f ["dset2"] = np.arange (15) for key in f.keys (): print (f [key] .name) print (f [key] .value) output: / dset1 [0 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] / dset2 [0 12 3 4 5 6 7 9 10 11 12 13 14]
If we have a ready-made numpy array, we can assign values when the dataset is created, instead of specifying the type and shape of the data, we just need to pass the array name to the parameter data.
Import h6pyimport numpy as npf=h6py.File ("myh6py.hdf5", "w") a=np.arange (20) d1=f.create_dataset ("dset1", data=a) for key in f.keys (): print (f [key] .name) print (f [key] .value) output: / dset1 [0 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Now write down a mix of these creation methods. Look at the following code
Import h6pyimport numpy as npf=h6py.File ("myh6py.hdf5", "w") # creates three data sets of dset1,dset2,dset3: a=np.arange (20) d1=f.create_dataset ("dset1", data=a) d2=f.create_dataset ("dset2", (3d4)) 'i') D2 [...] = np.arange (12). Reshape ((3p4)) f ["dset3"] = np.arange (15) for key in f.keys (): print (f [key] .name) print (f [key] .value) output: / dset1 [0 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] / dset2 [[0 12 3] [4 5 6 7] [8 9 10 11] / dset3 [ 0 12 3 4 5 6 7 8 9 10 11 12 13 14] 3. Create group group import h6pyimport numpy as npf=h6py.File ("myh6py.hdf5", "w") # create a group named bar G1 = f.create_group ("bar") # create datasets with name as dset1,dset2 in the bar group and assign values. G1 ["dset1"] = np.arange (10) G1 ["dset2"] = np.arange (12). Reshape ((3je 4)) for key in g1.keys (): print (G1 [key] .name) print (G1 [key] .value) output: / bar/dset1 [0 12 3 4 5 6 7 8 9] / bar/dset2 [[0 12 3] [4 56 7] [8 9 10 11]]
Pay attention to whether the name of the dataset dset1 and dset2 is a little different from the previous one. If the dataset is created directly and is not in any group, then its name is / + name. Now both datasets are in the group (group) of bar, and the name becomes the name of / bar+/. Does it feel like a folder? Continue to look at the code below to learn more about the relationship between group and dataset.
Import h6pyimport numpy as npf=h6py.File ("myh6py.hdf5", "w") # create group bar1, group bar2, dataset dsetg1=f.create_group ("bar1") g2=f.create_group ("bar2") d=f.create_dataset ("dset", data=np.arange (10)) # create a group car1 and a dataset dset1 in the bar1 group. C1=g1.create_group ("car1") d1=g1.create_dataset ("dset1", data=np.arange (10)) # create a group car2 and a dataset dset2c2=g2.create_group ("car2") d2=g2.create_dataset ("dset2") in the bar2 group Data=np.arange (10)) # groups and datasets under the root directory print (".") for key in f.keys (): print (f [key] .name) # bar1 groups and datasets under this group print (".") for key in g1.keys (): print (G1 [key] .name) # groups and datasets under this group Print (".") for key in g2.keys (): print (G2 [key] .name) # by the way, take a look at what's under the car1 group and car2 group. I guess you guessed it. It's empty. Print (".") print (c1.keys ()) print (c2.keys ()) output:. / bar1/bar2/dset./bar1/car1/bar1/dset1./bar2/car2/bar2/dset2. []
Reference:
1. Blog.csdn.net/csdn1569884...
2. Blog.csdn.net/yudf2010/ar...
This is the end of the introduction on "how to use the h5py open source library in python". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.