开发者

Is there any possibility to convert recarray to ndarray and change ndim?

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 n开发者_StackOverflow社区p
>>> 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,)
>>>


You can do it via pandas:

import pandas as pd
pd.DataFrame(y).values

array([[2000-01-01, 0, 1],
       [2000-01-01, 1, 1],
       [2000-01-01, 1, 0],
       [2000-01-01, 0, 0]], dtype=object)

But I would consider doing my project in pandas if I were you. Support for named columns is built much more deeply into pandas than into regular numpy.

>>> z = pd.DataFrame.from_records(y, index="f0")
>>> z
            f1  f2
f0                
2000-01-01   0   1
2000-01-01   1   1
2000-01-01   1   0
2000-01-01   0   0
>>> z["f1"]
f0
2000-01-01    0
2000-01-01    1
2000-01-01    1
2000-01-01    0
Name: f1


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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜