[Numpy-discussion] .data doesn't account for .transpose()?
Robert Kern
robert.kern at gmail.com
Thu Mar 29 18:32:00 EDT 2007
Glen W. Mabey wrote:
> Hello,
>
> I imagine that perhaps this issue I'm seeing is only an issue because I
> don't thoroughly understand the buffer issues associated with numpy
> arrays, but here it is anyway:
>
> In [16]:a1 = numpy.zero
> numpy.zeros numpy.zeros_like
>
> In [16]:a1 = numpy.zeros( (2,2) )
>
> In [17]:a1[0,:] = 1
>
> In [18]:a1
> Out[18]:
> array([[ 1., 1.],
> [ 0., 0.]])
>
> In [19]:str(a1.data)
> Out[19]:'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>
> In [20]:a2 = a1.transpose()
>
> In [21]:str(a2.data)
> Out[21]:'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>
> That is, when getting the .data from an array, if it was C_CONTIGUOUS
> but was .transposed(), the .data does not reflect this operation, right?
Correct, it just gives you the data as-is in memory, not as interpreted via the
strides. Transposition and slicing are implemented by changing the strides and
not changing any data.
> So, would that imply that a .copy() should be done first on any array
> that you want to access .data on?
asarray(a, order='C') should be sufficient, I think. That shouldn't copy if it
is already correctly ordered.
--
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