2012-01-17 66 views
2

我想知道是否有一种简单的方法来创建一个类来处理数字的numpy数组的整数和关键字索引。python numpy array/dict多重继承

最终目标是有一个numpy数组,我也可以使用每个变量的名称进行索引。 例如,如果我有列表

import numpy as np 
a = [0,1,2,3,4] 
names = ['name0','name1','name2','name3','name4'] 
A = np.array(a) 

我想能够得到A的值容易地与(例如)A [“NAME1”]的呼叫,但在阵列保存所有一个numpy数组的功能。

谢谢!

彼得

编辑:

非常感谢您的帮助,我会尽量使用目的更加清晰!我有一组现成的代码,它使用一个numpy数组来存储和应用一个变量向量。我的矢量有大约30个条目。

当我想查看特定变量的值,或者想要对其中一个变量进行更改时,我必须记住哪个条目对应哪个变量(条目的顺序或数量不一定一旦数组被创建就改变)。现在我用字典来跟踪。例如,我有一个具有30个值的numpy数组'VarVector'。 “vmax”是条目15,值为0.432。然后我将有一个包含30个'VarDict'键的并发字典,这样VarDict [entry] = index。这样我可以通过链接调用

VarVector [VarDict [“VMAX”]]找到VMAX的值

这将返回0.432

我想知道是否有将是一个简单的好方法结合这两种结构,例如VarVector [15](兼容性)和VarVector [“vmax”](为了方便我)将指向相同的数字。

谢谢! 彼得

+2

numpy数组的点在于它们用C编写,因此速度很快。如果你这样做,你会失去numpy数组的好处 - 你也可以使用Python列表! – katrielalex 2012-01-17 22:33:22

+0

你能给出一个理由_why_你想这样做吗? – katrielalex 2012-01-17 22:33:57

+1

@katrielalex - 不一定... numpy数组的'__getitem__'已经很慢了。通过添加它,你不会显着减慢速度。然而,这是一个相当常见的用例,并且已经完成了几次('pandas'和'larry')。看看这个比较:http://scipy.org/StatisticalDataStructures在某些情况下,使用“标记轴”或“标记项”是一件好事。 – 2012-01-18 00:06:52

回答

1

从您的描述来看,这听起来像只是想要一个structured array(这是内置numpy)。例如。

# Let's suppose we have 30 observations with 5 variables each... 
# The five variables are temp, pressure, x-velocity, y-velocity, and z-velocity 
x = np.random.random((30, 5)) 

# Make a structured dtype to represent our variables... 
dtype=dict(names=['temp', 'pressure', 'x_vel', 'y_vel', 'z_vel'], 
      formats=5 * [np.float]) 

# Now view "x" as a structured array with the dtype we created... 
data = x.view(dtype) 

# Each measurement will now have the name fields we created... 
print data[0] 
print data[0]['temp'] 

# If we want, say, all the "temp" measurements: 
print data['temp'] 

# Or all of the "temp" and "x_vel" measurements: 
print data[['temp', 'x_vel']] 

也看看rec arrays。他们稍微灵活一些,但速度要慢得多。

data = np.rec.fromarrays(*x, 
       names=['temp', 'pressure', 'x_vel', 'y_vel', 'z_vel']) 
print data.temp 

但是,你很快就会打到了上述两种方法的局限性(即你能说出两个轴)。在这种情况下,如果您只想标记项目,请查看larry,如果您想标记具有很多很好的缺失值处理的数组,请查看pandas

0

我还没有测试过,但它应该工作。

这个想法是假设输入是一个int,并将其用于numpy数组,如果不是,则将其用于字典。

import numbers 
import numpy 

class ThingArray: 
    def __init__(self): 
     self.numpy_array = numpy.array() 
     self.other_array = dict() 

    def __setitem__(self, key, value): 
     if isinstance(key, numbers.Integral): 
      self.numpy_array[key] = value 
     else: 
      self.other_array[key] = value 

    def __getitem__(self, key): 
     if isinstance(key, numbers.Integral): 
      return self.numpy_array[key] 
     else: 
      return self.other_array[key] 


thing = ThingArray() 

thing[1] = 100 
thing["one"] = "hundred"   

print thing[1] 
print thing["one"] 
0

你可以继承的ndarray并覆盖相关的方法(即__getitem____setitem__ ...)。 More info here。这与@ Joe的回答类似,但具有保留ndarray几乎所有功能的优点。你显然不能办了以下几点:

In [25]: array = np.empty(3, dtype=[('char', '|S1'), ('int', np.int)]) 

In [26]: array['int'] = [0, 1, 2] 

In [27]: array['char'] = ['a', 'b', 'c'] 

In [28]: array 
Out[28]: 
array([('a', 0), ('b', 1), ('c', 2)], 
     dtype=[('char', '|S1'), ('int', '<i8')]) 

In [29]: array['char'] 
Out[29]: 
array(['a', 'b', 'c'], 
     dtype='|S1') 

In [30]: array['int'] 
Out[30]: array([0, 1, 2]) 

如果我们知道你为什么要做到这一点,我们也许可以给出更详细的解答。