0

I am looking for validation that overwriting a numpy array with numpy.zeros overwrites the array at the location(s) in memory where the original array's elements are stored.

The documentation discusses this, but it seems I don't have enough background to understand whether just setting new values with the zeros function will overwrite the location in-place. One question asked here appears to indicate that it does. On the other hand, if I am interpreting the answer to this question correctly, it may not.

wb1210
  • 50
  • 8
  • 1
    Most numpy functions will return a new array object, such as zeros(). If you want to overwrite the elements of an existing array, you need to do so with direct assignment (e.g. array[:] = 0). – Andrew Holmgren Apr 10 '23 at 16:07
  • 1
    Your first link, 'documentation' is meant for C-API users, and the second for `cython`, so are of limited value to you. All my answer in the last link tells you is how to identify "where" the data is stored. None tell you what operations modify the existing data as opposed to returning a new array. – hpaulj Apr 10 '23 at 16:26
  • 1
    Why are you concerned about this? It's important to know when an operation returns a new array versus modifying an existing one. Closely related it knowing when the result is a `view` versus `copy`. But trying to "overwrite" to save time, or conserve memory, is an advanced topic. Also operations may create temporary arrays, that are later removed when whole assignment is done. – hpaulj Apr 10 '23 at 16:39
  • @hpaulj In this case, I am trying to ensure that the array does not persist beyond a certain point in the program, anywhere on the machine. I don't want data fragments remaining anywhere on the hardware. – wb1210 Apr 10 '23 at 20:55
  • That doesn't make sense to me, but then I'm not an expert on how `numpy`, `python` and the OS together manage memory. Is some sort of 'security' concern? – hpaulj Apr 10 '23 at 21:14

1 Answers1

1

There is no way to overwrite an existing NumPy array's memory with numpy.zeros. numpy.zeros does not offer such functionality. If you thought you were doing that, you most likely had code like

existing_array_name = numpy.zeros(...)

which does not clear the existing array's memory. It creates a new array.

If you want to zero out an array's elements, use

array[...] = 0

For reference, NumPy functions that do let you overwrite an existing array's memory usually do so by taking that array as an out parameter. For example, you can add the elements of two arrays a and b and write the result into an existing array c by doing

numpy.add(a, b, out=c)
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Thanks for your help! So, if you were trying to overwrite a numpy array to zeros, I could use something like: numpy_array *= 0.0? – wb1210 Apr 10 '23 at 16:16
  • 1
    @wb1210: That fails if any elements are infinities or NaNs. `array[...] = 0` handles those cases, and also more directly displays the intent. – user2357112 Apr 10 '23 at 16:28
  • 1
    The `*=0` approach is slower. It is approximately, `temp=arr*0.0; arr[:]=temp`, creating a temporary buffer. – hpaulj Apr 10 '23 at 16:41