6

When I shrink a numpy array using the resize method (i.e. the array gets smaller due to the resize), is it guaranteed that no copy is made?

Example:

a = np.arange(10)            # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a.resize(5, refcheck=False)  # array([0, 1, 2, 3, 4])

From my understanding this should always be possible without making a copy. My question: Does the implementation indeed guarantee that this is always the case? Unfortunately the documentation of resize says nothing about it.

2
  • 1
    If your new size is always going to be smaller why not slice the array and assign back to yourself? Commented Sep 4, 2015 at 12:19
  • @EdChum: You mean a = a[:5]? To be honest I didn't think of this option. I don't know how slicing works internally, though. Will this work without a copy of the data being made somewhere? Commented Sep 4, 2015 at 12:26

1 Answer 1

4

A numpy array is a fixed size array in the background, any type of resizing will always copy the array.

Having that said, you could create a slice of the array effectively only using a subset of the array without having to resize/copy.

>>> import numpy
>>> a = numpy.arange(10)
>>> b = a[:5]
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b
array([0, 1, 2, 3, 4])
>>>
>>> a += 10
>>> a
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
>>> b
array([10, 11, 12, 13, 14])
>>>
>>> b += 10
>>> a
array([20, 21, 22, 23, 24, 15, 16, 17, 18, 19])
>>> b
array([20, 21, 22, 23, 24])
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, I wasn't really aware that I can use slicing for this. This has one caveat, however: The memory for the unused part of a is not released (this may be an issue in some cases). What will happen if I do del a? Will it keep the full data or is it smart enough to keep only the part that is accessible through b?
When doing del a it will keep a completely because it still exists. b only displays a small portion of a but it's still there. The only way to actually free the memory is by copying it to a new array and deleting the old one. In the background numpy just does a standard malloc/free when creating/deleting arrays and it's not possible to free part of an array in c.
And another question: Why does resize have to make a copy when shrinking the array? I am no expert in how memory allocation works but shouldn't it be possible to just release the backmost part of the allocated memory and keeping the front part without copying anything?
Here's some explanation about the workings in the background: stackoverflow.com/questions/2479766/…
@Mr.F: Correct, but as far as I am aware that only works when simply changing the shape (not the size) of the Array. Resizing from (4,1) to (2, 2) or (1, 4) for example. I should note that I haven't looked at the source too thoroughly, though. The source can be found here for further inspection: github.com/numpy/numpy/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.