python - Python中的数据导入错误
问题描述
我正在尝试在 Python 中导入 MNIST 数据集,如下所示:
import h5py
f = h5py.File("mnist.h5")
x_test = f["x_test"]
x_train = f["x_train"]
y_test = f["y_test"]
y_train = f["y_train"]
说的类型,y_train 说 h5py._hl.dataset.Dataset
为了数学上的方便,我想将它们转换为浮点数。我试试这个:
D = x_train.astype(float)
y_train = y_train.astype(float)+np.ones((60000,1));
但我得到了这个回溯:
Traceback (most recent call last):
File "<ipython-input-14-f3677d523d45>", line 1, in <module>
y_train = y_train.astype(float)+np.ones((60000,1));
TypeError: unsupported operand type(s) for +: 'AstypeContext' and 'float'
我错过了什么?谢谢。
解决方案
您正在使用两个不同的库,它们对astype
.
如果您在 中执行此操作,则可以执行以下numpy
操作:
a = np.array([1, 2, 3])
a = a.astype(float) + np.ones((60000,1))
但是在 h5py 中, astype 是一个不同的函数,并且打算在上下文管理器中使用:
这将引发与您得到的相同的错误:
import h5py
f = h5py.File('mytestfile.hdf5', 'w')
dset = f.create_dataset("default", (100,))
dset.astype(float) + np.ones((60000,1))
但是下面的代码将起作用(请参阅 h5py 文档中的astype):
f = h5py.File('mytestfile.hdf5', 'w')
dset = f.create_dataset("default", (100,))
with dset.astype('float'):
out = dset[:]
out += np.ones((100,))
推荐阅读
- vb.net - 图片不会在图片框中更改,vb.net
- swiftui - SwiftUI状态变量封装最佳实践/复杂视图中状态变量过多
- html - 阐明设备像素和 CSS 像素之间的关系
- wordpress - 允许 wp-admin 访问特定 IP 并阻止其他 IP
- java - Java/OpenAPI - 无法为对象数组生成模型类
- java - Java - 防止集合框架用于分配的方法?
- php - Symfony serve 在错误的地方寻找 PHP 库
- shinyproxy - 如何将自定义错误页面添加到使用 shinyproxy 提供的应用程序
- javascript - 未捕获的 ReferenceError:未定义 {Class}
- android - 尝试在 Android Studio 中升级到 Kotlin 1.4.10 时出现异常