通常你會存儲基於狀態的指標所做的陣列進行任何更改之前。您可以使用索引進行更改。
如果a
是你的數組:
>>> a = np.random.random((10,5))
>>> a
array([[ 0.22481885, 0.80522855, 0.1081426 , 0.42528799, 0.64471832],
[ 0.28044374, 0.16202575, 0.4023426 , 0.25480368, 0.87047212],
[ 0.84764143, 0.30580141, 0.16324907, 0.20751965, 0.15903343],
[ 0.55861168, 0.64368466, 0.67676172, 0.67871825, 0.01849056],
[ 0.90980614, 0.95897292, 0.15649259, 0.39134528, 0.96317126],
[ 0.20172827, 0.9815932 , 0.85661944, 0.23273944, 0.86819205],
[ 0.98363954, 0.00219531, 0.91348196, 0.38197302, 0.16002007],
[ 0.48069675, 0.46057327, 0.67085243, 0.05212357, 0.44870942],
[ 0.7031601 , 0.50889065, 0.30199446, 0.8022497 , 0.82347358],
[ 0.57058441, 0.38748261, 0.76947605, 0.48145936, 0.26650583]])
而且b
是你的子陣:
>>> b = a[2:4,2:7]
>>> b
array([[ 0.16324907, 0.20751965, 0.15903343],
[ 0.67676172, 0.67871825, 0.01849056]])
它可以證明a
仍然擁有b
數據:
>>> b.base
array([[ 0.22481885, 0.80522855, 0.1081426 , 0.42528799, 0.64471832],
[ 0.28044374, 0.16202575, 0.4023426 , 0.25480368, 0.87047212],
[ 0.84764143, 0.30580141, 0.16324907, 0.20751965, 0.15903343],
[ 0.55861168, 0.64368466, 0.67676172, 0.67871825, 0.01849056],
[ 0.90980614, 0.95897292, 0.15649259, 0.39134528, 0.96317126],
[ 0.20172827, 0.9815932 , 0.85661944, 0.23273944, 0.86819205],
[ 0.98363954, 0.00219531, 0.91348196, 0.38197302, 0.16002007],
[ 0.48069675, 0.46057327, 0.67085243, 0.05212357, 0.44870942],
[ 0.7031601 , 0.50889065, 0.30199446, 0.8022497 , 0.82347358],
[ 0.57058441, 0.38748261, 0.76947605, 0.48145936, 0.26650583]])
您可以對進行更改個b
在兩個方面:
>>> b+=1
>>> b
array([[ 1.16324907, 1.20751965, 1.15903343],
[ 1.67676172, 1.67871825, 1.01849056]])
>>> a
array([[ 0.22481885, 0.80522855, 0.1081426 , 0.42528799, 0.64471832],
[ 0.28044374, 0.16202575, 0.4023426 , 0.25480368, 0.87047212],
[ 0.84764143, 0.30580141, 1.16324907, 1.20751965, 1.15903343],
[ 0.55861168, 0.64368466, 1.67676172, 1.67871825, 1.01849056],
[ 0.90980614, 0.95897292, 0.15649259, 0.39134528, 0.96317126],
[ 0.20172827, 0.9815932 , 0.85661944, 0.23273944, 0.86819205],
[ 0.98363954, 0.00219531, 0.91348196, 0.38197302, 0.16002007],
[ 0.48069675, 0.46057327, 0.67085243, 0.05212357, 0.44870942],
[ 0.7031601 , 0.50889065, 0.30199446, 0.8022497 , 0.82347358],
[ 0.57058441, 0.38748261, 0.76947605, 0.48145936, 0.26650583]])
或者:
>>> a[2:4,2:7]+=1
>>> a
array([[ 0.22481885, 0.80522855, 0.1081426 , 0.42528799, 0.64471832],
[ 0.28044374, 0.16202575, 0.4023426 , 0.25480368, 0.87047212],
[ 0.84764143, 0.30580141, 1.16324907, 1.20751965, 1.15903343],
[ 0.55861168, 0.64368466, 1.67676172, 1.67871825, 1.01849056],
[ 0.90980614, 0.95897292, 0.15649259, 0.39134528, 0.96317126],
[ 0.20172827, 0.9815932 , 0.85661944, 0.23273944, 0.86819205],
[ 0.98363954, 0.00219531, 0.91348196, 0.38197302, 0.16002007],
[ 0.48069675, 0.46057327, 0.67085243, 0.05212357, 0.44870942],
[ 0.7031601 , 0.50889065, 0.30199446, 0.8022497 , 0.82347358],
[ 0.57058441, 0.38748261, 0.76947605, 0.48145936, 0.26650583]])
>>> b
array([[ 1.16324907, 1.20751965, 1.15903343],
[ 1.67676172, 1.67871825, 1.01849056]])
兩者是等價的,也不是比其他更昂貴。因此,只要您保留從a
創建b
的索引,就可以隨時查看基本陣列中已更改的數據。在切片上進行操作時,甚至不需要創建子陣列。
編輯
這是假設some_func
返回在某個條件爲真子數組的索引。
我認爲,當一個函數返回索引,並且你只想爲該函數提供一個子數組時,你仍然需要存儲該子數組的索引並使用它們來獲取基數組索引。例如:
>>> def some_func(a):
... return np.where(a>.8)
>>> a = np.random.random((10,4))
>>> a
array([[ 0.94495378, 0.55532342, 0.70112911, 0.4385163 ],
[ 0.12006191, 0.93091941, 0.85617421, 0.50429453],
[ 0.46246102, 0.89810859, 0.31841396, 0.56627419],
[ 0.79524739, 0.20768512, 0.39718061, 0.51593312],
[ 0.08526902, 0.56109783, 0.00560285, 0.18993636],
[ 0.77943988, 0.96168229, 0.10491335, 0.39681643],
[ 0.15817781, 0.17227806, 0.17493879, 0.93961027],
[ 0.05003535, 0.61873245, 0.55165992, 0.85543841],
[ 0.93542227, 0.68104872, 0.84750821, 0.34979704],
[ 0.06888627, 0.97947905, 0.08523711, 0.06184216]])
>>> i_off, j_off = 3,2
>>> b = a[i_off:,j_off:] #b
>>> i = some_func(b) #indicies in b
>>> i
(array([3, 4, 5]), array([1, 1, 0]))
>>> map(sum, zip(i,(i_off, j_off))) # indicies in a
[array([6, 7, 8]), array([3, 3, 2])]
編輯2
這假定some_func
返回子陣列b
的修改拷貝。
你的例子是這個樣子:
import numpy as np
def some_function(arr):
return arr*2.0
a = np.arange(100)*2. # size = 100
idx = np.array(range(0,100,5))
b = some_function(a[idx]) # size = 20
b_idx = np.argmax(b)
a_idx = idx[b_idx] # indices in a translated from indices in b
print b_idx, a_idx
print b[b_idx], a[a_idx]
assert b[b_idx] == 2* a[a_idx] #true!
不知道我是否理解你的問題,但是你想找出第二步計算的20個指數嗎?最終的「索引」與它有什麼關係? – ejel 2011-04-23 02:23:43
@ejel:試圖解釋它,讓我們假設some_function忽略輸入數組,只是返回一個與輸入長度相同的隨機整數數組。然後'Index'將包含具有最大(隨機)數字的b中的索引。 b中的索引確實對應於a中的某個索引,而a中的索引是我想要的。 – 2011-04-23 03:47:13