faidros
faidros

Reputation: 56

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

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?

Upvotes: 3

Views: 642

Answers (2)

JE_Muc
JE_Muc

Reputation: 5784

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.

Upvotes: 3

Surya Talluri
Surya Talluri

Reputation: 9

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

Upvotes: -1

Related Questions