[英]how to set dtype for nested numpy ndarray?
I am working on the following data structure, from which I am trying to create a ndarray contains all the data: 我正在研究以下数据结构,从中试图创建一个包含所有数据的ndarray:
instrument filter response
-----------------------------------------------------
spire 250um array of response
... ... ...
where the array of response is:
linenumber wavelangth throughput
-----------------------------------------------------
0 1.894740e+06 0.000e+00
1 2.000000e+06 1.000e-02
2 2.026320e+06 3.799e-02
... .... ....
So, I hope I can turn the data to one ndarray, by using the following code: 因此,我希望可以通过使用以下代码将数据转换为一个ndarray:
import numpy as np
data = [('spire', '250um', [(0, 1.89e6, 0.0), (1,2e6, 1e-2), (2,2.02e6,3.8e-2), ...]),
('spire', '350', [ (...), (...), ...]),
...,
]
table = np.array(data, dtype=[('instrument', '|S32'),
('filter', '|S64'),
('response', [('linenumber', 'i'),
('wavelength', 'f'),
('throughput', 'f')])
])
This code raises exception because there is list(tuple, list(tuple))
pattern. 此代码引发异常,因为存在
list(tuple, list(tuple))
模式。 After changing the data
to: 将
data
更改为:
data = [('spire', '250um', np.array([(0, 1.89e6, 0.0), (1,2e6, 1e-2), (2,2.02e6,3.8e-2), ...],
dtype=[('linenumber','i'), ('wavelength','f'), ('throughput','f')])),
('spire', '350', np.array([ (...), (...), ...],dtype=[...])),
...,
]]
Then the code can run through, However, the result is wrong because for the response
field, only the first entry of the array of response is taken: 然后可以运行代码,但是结果是错误的,因为对于
response
字段,仅采用响应数组的第一个条目:
>>print table[0]
('spire', '250um', (0,1.89e6,0.0))
instead of the whole array. 而不是整个数组。
My question is, how to properly set the dtype
keyword to make this work? 我的问题是,如何正确设置
dtype
关键字以使其工作? in both cases: 1. a nested list of tuples in which list of tuples is contained; 在两种情况下:1.一个嵌套的元组列表,其中包含元组列表; 2. a nested list of tuples in which an inhomogeneous ndarray is contained.
2.一个嵌套的元组列表,其中包含不均匀的ndarray。
Thank you in advance! 先感谢您!
I can get this to work if the response array is of fixed length (perhaps Numpy has to be able to precompute the size of each record in a structured array?). 如果响应数组是固定长度的,我可以使它起作用(也许Numpy必须能够预先计算结构化数组中每个记录的大小?)。 As noted on the Numpy manual page for structured arrays , you can specify the shape for a field in a structured array.
如Numpy手册页中针对结构化数组所述 ,您可以为结构化数组中的字段指定形状。
import numpy as np
data = [('spire', '250um', [(0, 1.89e6, 0.0), (1, 2e6, 1e-2)]),
('spire', '350', [(0, 1.89e6, 0.0), (2, 2.02e6, 3.8e-2)])
]
table = np.array(data, dtype=[('instrument', '|S32'),
('filter', '|S64'),
('response', [('linenumber', 'i'),
('wavelength', 'f'),
('throughput', 'f')], (2,))
])
print table[0]
# gives ('spire', '250um', [(0, 1890000.0, 0.0), (1, 2000000.0, 0.009999999776482582)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.