Reputation: 141
I have a numpy.ndarray having dimensions of 23411 x 3. I would like to add headers to the top of the matrix called: "summary", "age", and "label". In that order.
In:
matrix.shape
Out:
(23411L, 3L)
In:
type(matrix)
Out:
numpy.ndarray
I tried using the numpy.recarray but it did not work. any suggestions??
Upvotes: 0
Views: 7613
Reputation: 53029
You can fiddle the dtype:
>>> a = np.arange(12).reshape(4, 3)
>>>
>>> dt = a.dtype
>>>
>>> ahead = a.view(np.dtype([('summary', dt), ('age', dt), ('label', dt)]))
>>>
>>> ahead
array([[(0, 1, 2)],
[(3, 4, 5)],
[(6, 7, 8)],
[(9, 10, 11)]],
dtype=[('summary', '<i8'), ('age', '<i8'), ('label', '<i8')])
>>> ahead['summary']
array([[0],
[3],
[6],
[9]])
But be warned that those composite dtype arrays are not very useful as far as I can tell:
>>> ahead @ ahead.T
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: invalid data type for einsum
to give just one example.
Upvotes: 4
Reputation: 912
Create your own class with members including the numpy array and the headings as strings.
Upvotes: 1
Reputation: 508
You can achieve this with pandas.
import pandas as pd
matrix = [...] # your ndarray
matrix = pd.DataFrame(data=matrix, columns=["summary", "age", "label"])
Upvotes: 4
Reputation: 213
I think there is no way to do this. Try matrix.dtype. This should give you something like dtype('int32'). Your headers would be strings.
As far as I know you can only store one datatype in a numpy array/matrix.
Upvotes: 0