I'm trying to cast a numpy matrix that I have already defined:
matrix = numpy.array([['name','23','45','1'],
['name2','223','43','5'],
['name3','12','33','2']])
resulting in this:
array([['name1', '23', '45', '1'],
['name2', '223', '43', '5'],
['name3', '12', '33', '2']],
dtype='|S5')
I would like to name and cast each column of my matrix to the following types:
dt = numpy.dtype({'names':['name','x','y','n'],'formats': ['S10', 'S10', 'S10', 'S10']})
For now, I will consider matrix all strings because it doesn't work, but what was expected a format like this 'formats': ['S10', 'f3', 'f3', 'i']
and do something like this:
matrix.astype(dtype=dt,casting='safe')
Result:
array([[('name', 'name', 'name', 'name'), ('23', '23', '23', '23'),
('45', '45', '45', '45'), ('1', '1', '1', '1')],
[('name2', 'name2', 'name2', 'name2'), ('223', '223', '223', '223'),
('43', '43', '43', '43'), ('5', '5', '5', '5')],
[('name3', 'name3', 'name3', 'name3'), ('12', '12', '12', '12'),
('33', '33', '33', '33'), ('2', '2', '2', '2')]],
dtype=[('name', 'S10'), ('x', 'S10'), ('y', 'S10'), ('n', 'S10')])
What am I missing? How can I define types for each matrix columns using numpy module?