[Numpy-discussion] Inplace remove some array rows

David Warde-Farley wardefar@iro.umontreal...
Sat Mar 12 13:43:11 CST 2011

On 2011-03-12, at 12:43 PM, Dmitrey wrote:

> hi all,
> currently I use
> a = array(m,n)
> ...
> a = delete(a, indices, 0) # delete some rows
> Can I somehow perform the operation in-place, without creating auxiliary array?
> If I'll use
> numpy.compress(condition, a, axis=0, out=a),
> or
> numpy.take(a, indices, axis=0, out=a)
> will the operation be inplace?

a will be the wrong shape to hold the output of either of those operations. You could use a[:len(indices)], and that will be "in-place", though it looks like numpy makes a temporary copy internally to avoid conflicts when the input array and output array share memory (at least in the case of take()). I was expecting

In [15]: a = arange(50).reshape(10, 5)

In [16]: numpy.take(a,[2,0,1],axis=0, out=a[:3])

to place 3 copies of original row 2 in rows 0, 1 and 2. The fact that it doesn't seems to suggest NumPy is being more clever (but also more memory-hungry).


More information about the NumPy-Discussion mailing list