简体   繁体   中英

Numpy array.resize() - zeros 'first'

I can use array.resize(shape) to resize my array and have zeros added to those indices without any value. If my array is [1,2,3,4] and I use array.resize[5,0] I get [1,2,3,4,0] . How can I append / pad the zeros to the front, yielding [0,1,2,3,4] ?

I am doing this dynamically - trying to use:

array.resize(arrayb.shape)

I would like to avoid (at all costs) making an in-memory copy of the array. That is to reverse the array, resize, and reverse again. Working with a view would be ideal.

You could try working on an array with negative strides (though you can never be sure that resize may not have to make a copy):

_a = np.empty(0) # original array
a = _a[::-1] # the array you work with...

# now instead of a, resize the original _a:
del a # You need to delete it first. Or resize will want refcheck=False, but that
      # will be dangerous!
_a.resize(5)
# And update a to the new array:
a = _a[::-1]

But I would really suggest you make the array large enough if in any way possible, this does not seem very beautiful, but I think this is the only way short of copying around data. Your array will also have a negative stride, so it won't be contiguous, so if that means that some function you use on it must make copy, you are out of luck.

Also if you slice your a or _a you have to either make a copy , or make sure you delete them before resizing. While you can give refcheck=False this seems to invalidate the data.

I believe you can use slice assignment to do this. I see no reason why numpy would need to make a copy for an operation like this, as long as it does the necessary checks for overlaps (though of course as others have noted, resize may itself have to allocate a new block of memory). I tested this method with a very large array, and I saw no jump in memory usage.

>>> a = numpy.arange(10)
>>> a.resize(15)
>>> a[5:] = a[:10]
>>> a[0:5] = 0
>>> a
array([0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

The following showed no jump in memory usage for the assignment operation:

>>> a = numpy.arange(100000000)
>>> a.resize(150000000)
>>> a[50000000:] = a[:100000000]

I don't know of a better way, and this is just a conjecture. Let me know if it doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM