2012-01-17 137 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]) 

如果我們知道你爲什麼要做到這一點,我們也許可以給出更詳細的解答。