python如何转换数据集_将数据集转换为HDF5数据

HDF5中的数据存储在数据集中,这些数据集是同构数组,可能是多维的,最多32个维度,每个维度的最大长度为无符号64位整数(列数),包含任意大小的数据类型,包括单个数据集的上限超过16 exabytes的复合数据类型。数据集用于保存结构化数据,如numpy数组、pandas数据帧、图像和电子表格。我还没有找到任何直接将纯文本或tar.gz文件放入HDF5的方法。但是,使用Python可以将文件读入字符串,并将其放入数据集中,如Strings in HDF5所示。除了数据集,组是HDF5中的另一种主要对象类型,是数据集和其他组的容器。数据集和组类似于文件和目录(或文件夹),并为分层格式(如Unix文件系统)提供了基础,在该文件系统中,可以使用以/开头的路径名访问对象。HDF5文件是可能包含多个数据集和组的容器,没有大小限制。

为了更好地了解HDF5是什么,我建议从HDF5 Downloads下载它和附带的实用程序以及HDFView,全部安装,然后通过Learning HDF5 with HDFView,这可以在30分钟内完成。HDFView是一个Java GUI,它使与HDF5的交互变得容易,但是您不能简单地将文件拖放到其中,而是可以将文件数据导入到数据集中。创建HDF5文件并使用pandas向其中添加数据帧非常容易,这是将数据放入HDF5文件的好方法。下面就是一个例子。有关HDF5的更多信息,您可以查看HDF5 Tutorials、HDF5 Python Examples by API、Additional HDF5 Python Examples上列出的其他教程和HDF5 for Python上的Python h5py包文档。关于熊猫的更多信息,10 Minutes to pandas是一个很好的开始,接下来是一系列代码示例的pandas Cookbook和Wes McKinney的Python for Data Analysis,这是自他发明和开发了大部分熊猫以来,关于熊猫的最好的教程,也是一位优秀的作者。

下面是一个使用pandas创建HDF5文件、将数据帧加载到其中并在另一个变量中检索和存储其副本的示例:In [193]: import pandas as pd

In [194]: frame = pd.read_csv('test.csv')

In [195]: frame

Out[195]:

a b c d message

0 1 2 3 4 one

1 5 6 7 8 two

2 9 10 11 12 three

In [196]: type(frame)

Out[196]: pandas.core.frame.DataFrame

In [197]: hdf5store = pd.HDFStore('mydata.h5')

In [198] %ls mydata.h5

Volume in drive C is OS

Volume Serial Number is 5B75-665D

Directory of C:\Users\tn\Documents\python\pydata

09/02/2015 12:41 PM 0 mydata.h5

1 File(s) 0 bytes

0 Dir(s) 300,651,331,584 bytes free

In [199]: hd5store['frame'] = frame

In [200]: hdf5store

Out[200]:

File path: mydata.h5

/frame frame (shape->[3,5])

In [201]: list(hdf5store.items())

Out[201]:

[('/frame', /frame (Group) ''

children := ['block0_values' (Array), 'block0_items' (Array), 'axis1' (Array), 'block1_items' (Array), 'axis0' (Array), 'block1_values' (VLArray)])]

In [202]: hdf5store.close()

现在演示从mydata.h5检索帧的能力:In [203]: hdf5store2 = pd.HDFStore('mydata.h5')

In [204]: list(hdf5store2.items())

Out[204]:

[('/frame', /frame (Group) ''

children := ['block0_values' (Array), 'block0_items' (Array), 'axis1' (Array), 'block1_items' (Array), 'axis0' (Array), 'block1_values' (VLArray)])]

In [205]: framecopy = hdf5store2['frame']

In [206]: framecopy

Out[206]:

a b c d message

0 1 2 3 4 one

1 5 6 7 8 two

2 9 10 11 12 three

In [207]: framecopy == frame

Out[207]:

a b c d message

0 True True True True True

1 True True True True True

2 True True True True True

In [208]: hdf5store2.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将.pb文件转换HDF5格式的完整代码: ```python import tensorflow as tf import os import argparse def convert_pb_to_hdf5(input_pb, output_hdf5): # Load the protobuf file from the disk with tf.io.gfile.GFile(input_pb, "rb") as f: graph_def = tf.compat.v1.GraphDef() graph_def.ParseFromString(f.read()) # Import the graph_def into a new Graph and return it with tf.Graph().as_default() as graph: tf.import_graph_def(graph_def, name="") # Convert variables to constants so that the graph can be saved as a single file with tf.compat.v1.Session(graph=graph) as sess: tf.compat.v1.global_variables_initializer().run() output_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants( sess, graph.as_graph_def(), ["output_node"]) # Save the converted graph as a HDF5 file with tf.io.gfile.GFile(output_hdf5, "wb") as f: f.write(tf.compat.as_bytes(output_graph_def.SerializeToString())) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Convert a frozen TensorFlow model (.pb file) to HDF5 (.h5) format') parser.add_argument('--input_pb', required=True, help='Path to the input .pb file') parser.add_argument('--output_hdf5', required=True, help='Path to the output .h5 file') args = parser.parse_args() convert_pb_to_hdf5(args.input_pb, args.output_hdf5) ``` 使用方式: 在命令行中输入以下命令: ```python python convert_pb_to_hdf5.py --input_pb <path to input .pb file> --output_hdf5 <path to output .h5 file> ``` 其中,`<path to input .pb file>` 为需要转换的.pb文件的路径,`<path to output .h5 file>` 为转换后的.h5文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值