有没有可能将recarray转换为ndarray并更改ndim?

时间:2009-10-19 11:44:09

标签: python numpy

我正在从matplotlib.mlab.csv2rec函数中重新获得。我的期望是它有2个维度,如'x',但它有1个维度,如'y'。有没有办法从y获得x?

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

2 个答案:

答案 0 :(得分:2)

你可以通过熊猫来实现:

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)

但如果我是你,我会考虑在熊猫中做我的项目。对于大熊猫,对命名列的支持比普通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

答案 1 :(得分:0)

听起来很奇怪,但是......我可以使用matplotlib.mlab.rec2csv保存到csv,然后使用numpy.loadtxt读取到ndarray。我的情况比较简单,因为我已经有了csv文件。这是一个如何工作的例子。

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

同样在我的情况下,我不需要第一列来做出决定。