如何为嵌套的numpy ndarray设置dtype? [英] how to set dtype for nested numpy ndarray?
问题描述
我正在研究以下数据结构,从中试图创建一个包含所有数据的ndarray:
I am working on the following data structure, from which I am trying to create a ndarray contains all the data:
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
... .... ....
因此,我希望可以通过使用以下代码将数据转换为一个ndarray:
So, I hope I can turn the data to one ndarray, by using the following code:
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')])
])
此代码引发异常,因为存在list(tuple, list(tuple))
模式.将data
更改为:
This code raises exception because there is list(tuple, list(tuple))
pattern. After changing the data
to:
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=[...])),
...,
]]
然后代码可以运行,但是,结果是错误的,因为对于response
字段,仅采用响应数组的第一个条目:
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:
>>print table[0]
('spire', '250um', (0,1.89e6,0.0))
而不是整个数组.
我的问题是,如何正确设置dtype
关键字以使其起作用?在两种情况下:1.一个嵌套的元组列表,其中包含元组列表;
2.嵌套的元组列表,其中包含不均匀的ndarray.
My question is, how to properly set the dtype
keyword to make this work? in both cases: 1. a nested list of tuples in which list of tuples is contained;
2. a nested list of tuples in which an inhomogeneous ndarray is contained.
提前谢谢!
推荐答案
如果响应数组的长度固定,我可以使它起作用(也许Numpy必须能够预先计算结构化数组中每个记录的大小? ).如结构化数组的Numpy手册页所述,您可以指定结构化数组中字段的形状.
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?). As noted on the Numpy manual page for structured arrays, you can specify the shape for a field in a structured array.
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)])
这篇关于如何为嵌套的numpy ndarray设置dtype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!