相当于NumPy中的命名元组?

2024-04-27 11:42:52 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以创建一个行为非常类似于collections.namedtuple,从某种意义上说,可以这样访问元素:

data[1] = 42
data['start date'] = '2011-09-20'  # Slight generalization of what is possible with a namedtuple

我试图使用复杂的数据类型:

^{pr2}$

这将创建一个具有一种namedtuple类型的0维值;它几乎可以工作:

>>> data['start date'] = '2011-09-20'
>>> data
array(('2011-09-20', -3241474627884561860), 
      dtype=[('start date', '|S11'), ('n', '<i8')])

但是,元素访问不起作用,因为“数组”是0维的:

>>> data[0] = '2011-09-20'
Traceback (most recent call last):
  File "<ipython-input-19-ed41131430b9>", line 1, in <module>
    data[0] = '2011-09-20'
IndexError: 0-d arrays can't be indexed.

有没有一种方法可以用NumPy对象获得上面描述的所需行为(通过字符串和索引进行项分配)?在


Tags: of元素datadateiswithnamedtuplewhat
3条回答

(根据EOL的建议进行编辑,以便在回答问题时更加具体。)

创建0维数组(我也没有找到标量构造函数。)

>>> data0 = np.array(('2011-09-20', 0), dtype=[('start date', 'S11'), ('n', int)])
>>> data0.ndim
0

0维数组中的访问元素

^{pr2}$

我认为最简单的方法是将结构化数组(或recarray)看作元组的列表或数组,而索引是根据选择列的名称和选择行的整数进行的。在

>>> tupleli = [('2011-09-2%s' % i, i) for i in range(5)]
>>> tupleli
[('2011-09-20', 0), ('2011-09-21', 1), ('2011-09-22', 2), ('2011-09-23', 3), ('2011-09-24', 4)]
>>> dt = dtype=[('start date', '|S11'), ('n', np.int64)]
>>> dt
[('start date', '|S11'), ('n', <class 'numpy.int64'>)]

零维数组,元素是元组,即一条记录,已更改:不是标量元素,请参见末尾

>>> data1 = np.array(tupleli[0], dtype=dt)
>>> data1.shape
()
>>> data1['start date']
array(b'2011-09-20', 
      dtype='|S11')
>>> data1['n']
array(0, dtype=int64)

有一个元素的数组

>>> data2 = np.array([tupleli[0]], dtype=dt)
>>> data2.shape
(1,)
>>> data2[0]
(b'2011-09-20', 0)

一维阵列

>>> data3 = np.array(tupleli, dtype=dt)
>>> data3.shape
(5,)
>>> data3[2]
(b'2011-09-22', 2)
>>> data3['start date']
array([b'2011-09-20', b'2011-09-21', b'2011-09-22', b'2011-09-23',
       b'2011-09-24'], 
      dtype='|S11')
>>> data3['n']
array([0, 1, 2, 3, 4], dtype=int64)

直接索引到单个记录中,与EOL的示例中相同,我不知道它是否有效

>>> data3[2][1]
2
>>> data3[2][0]
b'2011-09-22'

>>> data3[2]['n']
2
>>> data3[2]['start date']
b'2011-09-22'

试图理解EOL的例子:标量元素和零维数组是不同的

>>> type(data1)
<class 'numpy.ndarray'>
>>> type(data1[()])   #get element out of 0-dim array
<class 'numpy.void'>

>>> data1[0]
Traceback (most recent call last):
  File "<pyshell#98>", line 1, in <module>
    data1[0]
IndexError: 0-d arrays can't be indexed
>>> data1[()][0]
b'2011-09-20'

>>> data1.ndim
0
>>> data1[()].ndim
0

(注意:我无意中在一个开放的python3.2解释器中输入了这个示例,因此有一个b'…')

这是由Pandas包中的“Series”很好地实现的。在

例如从tutorial

>>> from pandas import *
>>> import numpy as np
>>> s = Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
>>> s
a    -0.125628696947
b    0.0942011098937
c    -0.71375003803
d    -0.590085433392
e    0.993157363933
>>> s[1]
0.094201109893723267
>>> s['b']
0.094201109893723267

我已经玩了几天了,但它看起来有很多功能。在

您可以使用numpy.rec模块执行类似的操作。您需要的是这个模块中的record类,但我不知道如何直接创建此类的实例。一种直接的方法是首先用一个条目创建一个recarray

>>> a = numpy.recarray(1, names=["start date", "n"], formats=["S11", "i4"])[0]
>>> a[0] = "2011-09-20"
>>> a[1] = 42
>>> a
('2011-09-20', 42)
>>> a["start date"]
'2011-09-20'
>>> a.n
42

如果您知道如何直接创建record的实例,请告诉我。在

相关问题 更多 >