Combining two views of same numpy array into single view without copying the array?

不打扰是莪最后的温柔 提交于 2020-01-16 18:48:26

问题


I have a large 2d numpy array and I want to remove subsets of it and handle what remains to a function. I need to do this for many subsets, and thus I would ideally not want to create a copy of the array each time. The function doesn't change any values in the array.

mat = np.load(filename)
mat_1 = mat[:i,:]
mat_2 = mat[j:,:]

So far, mat_1 and mat_2 are views. Then I would like to do

mat_s = np.concatenate((mat_1,mat_2))
result = func(mat_s)

but without making a copy. Is this possible?


回答1:


Since memory-views can only be created using a fixed set of strides, you will have to create a copy in your case, where mat.shape[0] > j > i.

That means views will only work, if you want to have a view to every x-th element in the array:

mat = np.arange(20)
view = mat[slice(0, 20, 4)]
view
# Out[41]: array([ 0,  4,  8, 12, 16])

So this only works for views to equally spaced cells. But if you want to have a view to one contiguous slice(0, i) and another contiguous slice(j, mat.shape[0]), it won't work. You'll have to make a copy.




回答2:


You can delete the rows that you want to remove and pass it directly into the function

mat = np.load(filename)
mat_s = np.delete(mat,list(range(i,j)),axis=0)

you can delete 2 diff subsets by adding the list of ranges like

mat_s = np.delete(mat,list(range(i,j))+list(range(k,l)),axis=0)

the above removes the rows i:j and k:l



来源:https://stackoverflow.com/questions/50330112/combining-two-views-of-same-numpy-array-into-single-view-without-copying-the-arr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!