Define dtypes in NumPy using a list?
I just am having a problem with NumPy dtypes. Essentially I'm trying to create a table that looks like the following (and then save it using rec2csv):
name1 name2 name3 . . .
name1 # # #
name2 # # #
name2 # # #
.
.
.
The matrix (numerical array in the center), is开发者_JAVA技巧 already computed before I attempt to add the name tags. I've tried to use the following code:
dt = dtype({'names' : tuple(blah), 'formats' : tuple(fmt)})
ReadArray = array(tuplelist, dtype=dt)
where tuplelist is a list of rows (i.e. the row [name1, #, #, #...]), blah is a list of strings (i.e. the names, blah = ['name1', 'name2', ...]
) and fmt is the list of format,s (i.e. fmt = [str, float, float, ...]
).
The error I'm getting is the following:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "table_calc_try2.py", line 152, in table_calc_try2
dt = dtype({'names' : tuple(blah), 'formats' : tuple(fmt)})
TypeError: data type not understood
Can anyone help?
Thanks!
The following code might help:
import numpy as np
dt = np.dtype([('name1', '|S10'), ('name2', '<f8')])
tuplelist=[
('n1', 1.2),
('n2', 3.4),
]
arr = np.array(tuplelist, dtype=dt)
print(arr['name1'])
# ['n1' 'n2']
print(arr['name2'])
# [ 1.2 3.4]
Your immediate problem was that np.dtype
expects the format specifiers to be numpy types, such as '|S10'
or '<f8'
and not Python types, such as str
or float
. If you type help(np.dtype)
you'll see many examples of how np.dtypes
can be specified. (I've only mentioned a few.)
Note that np.array expects a list of tuples. It's rather particular about that.
A list of lists raises TypeError: expected a readable buffer object
.
A (tuple of tuples) or a (tuple of lists) raises ValueError: setting an array element with a sequence
.
精彩评论