tags:

views:

291

answers:

2

I am getting recarray from matplotlib.mlab.csv2rec function. My expectation was it would have 2 dimensions like 'x', but it has 1 dimension like 'y'. Is there any way to get x from y?

>>> import numpy as np
>>> from datetime import date
>>> x=np.array([(date(2000,1,1),0,1),
...              (date(2000,1,1),1,1),
...              (date(2000,1,1),1,0),
...              (date(2000,1,1),0,0),
...              ])
>>> x
array([[2000-01-01, 0, 1],
       [2000-01-01, 1, 1],
       [2000-01-01, 1, 0],
       [2000-01-01, 0, 0]], dtype=object)
>>> y = np.rec.fromrecords( x )
>>> y
rec.array([(datetime.date(2000, 1, 1), 0, 1),
       (datetime.date(2000, 1, 1), 1, 1),
       (datetime.date(2000, 1, 1), 1, 0), (datetime.date(2000, 1, 1), 0, 0)],
      dtype=[('f0', '|O4'), ('f1', '<i4'), ('f2', '<i4')])
>>> x.ndim
2
>>> y.ndim
1
>>> x.shape
(4, 3)
>>> y.ndim
1
>>> y.shape
(4,)
>>>
A: 

Sounds weird but... I can save to csv by using matplotlib.mlab.rec2csv, and then read to ndarray by using numpy.loadtxt. My case is simpler as I already have csv file. Here is an example how it works.

>>> a = np.loadtxt( 'name.csv', skiprows=1, delimiter=',', converters = {0: lambda x: 0} )
>>> a
array([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.29,  0.29,  0.43,  0.29,  0.  ],
       [ 0.  ,  0.71,  0.29,  0.57,  0.  ,  0.  ],
       [ 0.  ,  1.  ,  0.57,  0.71,  0.  ,  0.  ],
       [ 0.  ,  0.43,  0.29,  0.14,  0.14,  0.  ],
       [ 0.  ,  1.  ,  0.43,  0.71,  0.  ,  0.  ],
       [ 0.  ,  0.57,  0.57,  0.29,  0.14,  0.  ],
       [ 0.  ,  1.43,  0.43,  0.86,  0.43,  0.  ],
       [ 0.  ,  1.  ,  0.71,  0.57,  0.  ,  0.  ],
       [ 0.  ,  1.14,  0.57,  0.29,  0.  ,  0.  ],
       [ 0.  ,  1.43,  0.29,  0.71,  0.29,  0.29],
       [ 0.  ,  1.14,  0.43,  1.  ,  0.29,  0.29],
       [ 0.  ,  0.43,  1.14,  0.86,  0.43,  0.14],
       [ 0.  ,  1.14,  0.86,  0.86,  0.29,  0.29]])
>>> t = a.any( axis = 1 )
>>> t
array([False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True], dtype=bool)
>>> a.ndim
2

Also in my case I don't need a first column for making a decision.

maplpro
A: 

Well, there might be a more efficient way than this, but here is one way:

#!/usr/bin/env python
import numpy as np
from datetime import date
x=np.array([(date(2000,1,1),0,1),
              (date(2000,1,1),1,1),
              (date(2000,1,1),1,0),
              (date(2000,1,1),0,0),
              ])

y=np.rec.fromrecords( x )

z=np.empty((len(y),len(y.dtype)),dtype='object')
for idx,field in enumerate(y.dtype.names):
   z[:,idx]=y[field]
assert (x==z).all()
unutbu
sure, but I can't believe there is no elegant way for such conversion :(
maplpro