我有一些数据(来自HDF5文件),只想获取一些列。我试图对数组进行切片,但是得到了IndexError
,我无法理解为什么。 有什么想法吗?
代码:
>>> type(x)
numpy.ndarray
>>> x
array([((1537445457, 517647), 0.45301986, 13.807418, 0.10681067, 6.856901 , 2.8895614, 15.341972, 2.8160472, 5.7942305, 67.95573, 2.5007493, 13.925896, 1.4587704, 6.1075644, 68.347534, 2.6885383, 15.334871, 0.31526774, 5.9454284, 0.713507 , nan, nan, nan, 0., 0., 0., 0., 0.),
((1537445457, 630955), 0.5777189 , 13.807683, 0.10421388, 6.8743234, 2.7194788, 14.866684, 2.753199 , 5.7411118, 68.38666, 3.0199409, 14.754977, 1.4933671, 5.7361865, 67.82245 , 3.4682775, 15.384485, 0.3508615 , 6.3675985, 0.31907487, nan, nan, nan, 0., 0., 0., 0., 0.)],
dtype=[('time', [('sec', '<u4'), ('usec', '<u4')]), ('0', '<f4'), ('1', '<f4'), ('2', '<f4'), ('3', '<f4'), ('4', '<f4'), ('5', '<f4'), ('6', '<f4'), ('7', '<f4'), ('8', '<f4'), ('9', '<f4'), ('10', '<f4'), ('11', '<f4'), ('12', '<f4'), ('13', '<f4'), ('14', '<f4'), ('15', '<f4'), ('16', '<f4'), ('17', '<f4'), ('18', '<f4'), ('19', '<f4'), ('20', '<f4'), ('21', '<f4'), ('22', '<f4'), ('23', '<f4'), ('24', '<f4'), ('25', '<f4'), ('26', '<f4')])
>>> x[0, [0, 1]]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-123-41118eda084a> in <module>()
----> 1 x[0, [0, 1]]
IndexError: too many indices for array
答案 0 :(得分:2)
您的数组是一个结构化的数组,带有一个复合dtype
。您的hdf5
负载没有任何问题。
In [135]: arr.shape
Out[135]: (2,)
In [136]: arr.dtype
Out[136]: dtype([('time', [('sec', '<u4'), ('usec', '<u4')]), ('0', '<f4'), ('1', '<f4'), ('2', '<f4'), ('3', '<f4'), ('4', '<f4'), ('5', '<f4'), ('6', '<f4'), ('7', '<f4'), ('8', '<f4'), ('9', '<f4'), ('10', '<f4'), ('11', '<f4'), ('12', '<f4'), ('13', '<f4'), ('14', '<f4'), ('15', '<f4'), ('16', '<f4'), ('17', '<f4'), ('18', '<f4'), ('19', '<f4'), ('20', '<f4'), ('21', '<f4'), ('22', '<f4'), ('23', '<f4'), ('24', '<f4'), ('25', '<f4'), ('26', '<f4')])
In [137]: len(arr.dtype.names)
Out[137]: 28
它有2条记录。每条记录包含28个字段
In [138]: arr.dtype.names
Out[138]:
('time',
'0',
'1',
'2',
'3',
....
第一个字段“时间”本身就是复合的:
In [139]: arr['time']
Out[139]:
array([(1537445457, 517647), (1537445457, 630955)],
dtype=[('sec', '<u4'), ('usec', '<u4')])
字段是按名称而不是“列号”引用的
在列表压缩方法中,您对记录进行迭代,然后按数字访问记录的元素:
In [148]: np.array([x[2] for x in arr])
Out[148]: array([13.807418, 13.807683], dtype=float32)
In [149]: arr['1']
Out[149]: array([13.807418, 13.807683], dtype=float32)
时间解析可能仍需要记录迭代:
In [152]: time = np.array(
...: [
...: np.datetime64(
...: datetime.utcfromtimestamp(
...: float("{0}.{1:06.0f}".format(x[0][0], x[0][1]))))
...: for x in arr
...: ],
...: dtype=np.datetime64)
...:
In [153]:
In [153]: time
Out[153]:
array(['2018-09-20T12:10:57.517647', '2018-09-20T12:10:57.630955'],
dtype='datetime64[us]')
datetime
一次只能处理一次:
In [176]: np.array(
...: [datetime.utcfromtimestamp(
...: float("{0}.{1:06.0f}".format(*x)))
...: for x in arr['time']
...: ],dtype=np.datetime64)
...:
Out[176]:
array(['2018-09-20T12:10:57.517647', '2018-09-20T12:10:57.630955'],
dtype='datetime64[us]')
答案 1 :(得分:0)
我无法重塑数据,需要使用旧的好方法来解析它:
import numpy as np
import h5py
h5data = h5py.File("test.h5", 'r')
log = h5data['/log']
time = np.array(
[
datetime.utcfromtimestamp(
float("{0}.{1:06.0f}".format(*x))) for x in log['time']
],
dtype=np.datetime64)
ook = np.array([x[2] for x in log], dtype=float)
这太糟了。 ☹