[Numpy-discussion] Setting contents of buffer for array object

Robert Kern robert.kern at gmail.com
Sun Feb 10 20:08:09 EST 2008


On Feb 10, 2008 6:48 PM, Matthew Brett <matthew.brett at gmail.com> wrote:
> > > import numpy as np
> > > a = np.arange(10)
> > > b = np.arange(10)+1
> > > a.data = b.data # raises error, but I hope you see what I mean
> > >
> > > ?
> >
> > Not really, no. Can you describe your use case in more detail?
>
> Yes - I am just writing the new median implementation.   To allow
> future optimization, I would like to have the same signature as
> mean():
>
> def median(a, axis=0, dtype=None, out=None)
>
> (axis=0 to change to axis=None default at some point).
>
> To do this, I need to copy the results of the median calculation in
> the routine into the array object given by 'out' - when passed.

Ah, I see. You definitely do not want to reassign the .data buffer in
this case. An out= parameter does not reassign the memory location
that the array object points to. It should use the allocated memory
that was already there. It shouldn't "copy" anything at all;
otherwise, "median(x, out=out)" is no better than "out[:] =
median(x)". Personally, I don't think that a function should expose an
out= parameter unless if it can make good on that promise of memory
efficency. Can you show us the current implementation that you have?

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list