2014-05-14 160 views
2

我想打印numpy的陣列更好,使用索引到數組[0 1]指標排零和一列:NumPy的索引來索引陣列

假設我們有一個很大的numpy的陣列

x = np.arange(400*8000).reshape(400,8000).astype(float) 

現在我們要打印行100,但只有最後十個條目:

print x[100, -10:] 

這將導致類似:

 Index  | X  
    [ 100 7990] | 807990 
    [ 100 7991] | 807991 
    [ 100 7992] | 807992 
    [ 100 7993] | 807993 
    [ 100 7994] | 807994 
    [ 100 7995] | 807995 
    [ 100 7996] | 807996 
    [ 100 7997] | 807997 
    [ 100 7998] | 807998 
    [ 100 7999] | 807999 

我通過獲取指標如下產生這樣的:

index = np.indices(X.shape)[:, 100, -10:] 
print index 
# array([[ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100], 
#  [7990, 7991, 7992, 7993, 7994, 7995, 7996, 7997, 7998, 7999]]) 

但是,這似乎是非常低效的,尤其是,當索引變得更爲複雜,或X變大了(如在數十億項) 。

有沒有更好的方法從索引創建索引數組(索引是[100, -10:],索引數組是index)?

+4

我認爲這個解決方案非常可讀。如果您只想將其用於打印目的,在大多數情況下,速度不會成爲問題。過早優化是邪惡的。 – gg349

回答

2

創建數組索引僅僅是一個數學問題。你不需要爲此而構建一個棘手的功能。例如,下面的函數應該適合你,並且很快就會運行。

def getIndices(x, row, lastNCols): 
    """ 
    x = your matrix 
    row = the index of the row you want (100 in your case) 
    lastNCols = the number of columns you want to grab off the end of that 
       row (10 in your case) 
    """ 
    rows = np.full(lastNCols, row, dtype=int32) 
    cols = np.arange(x.shape[1] - lastNCols, x.shape[1], dtype=int32) 
    return np.column_stack((rows, cols)).T 

運行:

x = np.arange(400*8000).reshape(400,8000).astype(float) 
getIndices(x, 100, 10) 

輸出:

array([[ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100], 
     [7990, 7991, 7992, 7993, 7994, 7995, 7996, 7997, 7998, 7999]]) 
+0

非常好的主意!我的問題可能太嚴格了,因爲我希望函數可以適用於任何維度,不僅是2的特例... – maxz

0

numpy.ix是索引的問題非常有用的工具。爲了從矩陣得到必要片,你需要做以下幾點:

x = np.arange(400*8000).reshape(400,8000).astype(float) 
print x[np.ix_(np.tile(np.array([100]), 10), np.arange(7990, 8000))] 

,你傳遞np.ix功能指標。您必須傳遞行和列的序列作爲np.ix()函數的參數。在這個例子中,行是[100,100,...,100],列是[7990,7991,...,7999]。如果您嘗試打印由np.ix作爲

print np.ix_(np.tile(np.array([100]), 10), np.arange(7990, 8000)) 

你會看到,它返回

(array([[100], 
    [100], 
    [100], 
    [100], 
    [100], 
    [100], 
    [100], 
    [100], 
    [100], 
    [100]]), array([[7990, 7991, 7992, 7993, 7994, 7995, 7996, 7997, 7998, 7999]])) 

,其中第一組的第一個值與第二陣列的第一個值等交叉陣列的回報,正確地切分矩陣。如果您需要更多維度,只需在np.ix函數中添加額外參數,其中第一個參數是行序列,第二個參數是列序列,第三個參數是深度序列,依此類推。例如:np.ix(np.arange(0,3), np.arange(3,8), np.arange(4,2,-1))這將返回切片數組,其中第3行,第3到第7列和第4到第3頁切片矩陣。看一些例子here