python - 没有 dtype ('<U1' 的转换路径)问题

标签 python python-3.x numpy hdf5 h5py

我有一个包含 3d 数组和标签(0 或 1)的 2d 列表(Data_set),我想制作包含两个数据集的 h5py 文件,一个用于 3d 数组,另一个用于标签,这是我的代码为此: `

    data = []
    label = []
    for i in range(len(Data_set)):
        data.append(Data_set[i][0])# 3d array
        label.append(Data_set[i][1])#label
    data = np.array(data)
    label = np.array(label)
    dt = np.dtype('int16')
    with h5py.File(output_path+'dataset.h5', 'w') as hf:
        hf.create_dataset('data',dtype=dt ,data=data, compression='lzf')
        hf.create_dataset('label', dtype=dt, data=label, compression='lzf')

` 二维列表的内容如下图所示: enter image description here 但是当我运行代码时,它给了我一个错误:见下图 enter image description here 请帮我解决这个问题吗?

最佳答案

你的标签不是整数,它们是字符串,这是 HDF5 的问题。您的错误消息与由长度为 1 的字符串组成的数组相关。请参阅 Strings in HDF5了解更多详情。

您可以在构造 NumPy 数组之前或之后转换为整数,以下是几个示例:

label = np.array(label).astype(int)
# or, label = np.array(list(map(int, label)))

或者,由于您的值为 01,因此选择 bool 可能会更有效:

label = np.array(label).astype(int).astype(bool)

另外,考虑将元数据保存为 attributes .

关于python - 没有 dtype ('<U1' 的转换路径)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51578212/

相关文章:

Python:获取多维列表中的所有对和对的频率

python - 如何在不维护 Python 中的目录结构的情况下从 zip 中提取文件?

python-3.x - ClientConnectorCertificateError : Cannot connect to host discordapp. com :443, AWS 上的认证错误。(ec2)

python - 标量/数组乘法返回任意类型

python - 使python程序可执行的问题

python - 如果 Google App Engine cron 作业有 10 分钟的限制,那么为什么我会在正常的 30 秒后收到 DeadlineExceededError?

python - 在 Python 中对日期时间进行计算

python - 网站更改代码后网络爬虫抛出错误

python - 已知 x、y 的条形图

python - 从文件中检索 10 个随机行