列表切片內存的觀點,我相信這是一個重複的,但我真的不能在其他地方找到答案。與指數
我的主要問題是,我使用numpy的布爾索引在我的代碼,我對其進行優化。
我做這樣的事情:
y_ind = ~isnan(y)
otheThing = y[t,y_ind[t,:]]
我之所以保存此片在otherThing
是因爲我用了好計算,讓我避免每次reslicing它。
我的解決方案是使用內存視圖而不是np.ndarray
爲y
和otherThing
。
的問題是,布爾索引不會用Cython工作,所以我試圖用整數列表來切它。就像我們會在numpy的做,是這樣的:
x = np.random.randn(5,5)
array([[-0.56313056, -1.27834803, -0.30052179, -2.00063006, -2.05059544],
[-0.44563071, -0.63580159, -0.67538499, -0.9192516 , 0.49663121],
[ 2.11943794, -0.51746142, -0.80092609, -0.89391932, 1.91359607],
[ 0.52593344, -0.59931707, 0.78093441, -1.62449658, -1.65887717],
[-0.46422764, 0.96595406, -0.47142008, -0.28886925, -1.86674776]])
x[2,[1,2]]
array([-0.51746142, -0.80092609])
,當我們試圖做的是一樣的東西,會發生什麼:
cdef np.ndarray[np.double_t,ndim=2] a = np.random.randn(n,n)
cdef double[:,:] a_mv = a
print(np.array(a_mv[5,(1,3)]))
.pyx:30:27: Invalid index for memoryview specified, type (long, long)
In [22]:
這可能嗎?有更好的方法嗎?
我編輯突出'cython'爭議的問題,甚至包括一工作'cython'功能。 – hpaulj
有人投下來的問題沒有解釋爲什麼。 –