[Numpy-discussion] another view puzzle
Stéfan van der Walt
stefan@sun.ac...
Wed Jun 3 17:55:12 CDT 2009
2009/6/3 Robert Kern <robert.kern@gmail.com>:
> On Wed, Jun 3, 2009 at 16:31, <josef.pktd@gmail.com> wrote:
>> I'm very happy with plain numpy arrays, but to handle different data
>> types in scipy.stats, I'm still trying to figure out how views and
>> structured arrays work. And I'm still confused.
>
> .view() is used two different ways, and I think that is confusing you.
> .view(some_dtype) constructs a view of the array's memory with a
> different dtype. This can cause a reinterpretation of the bytes of
> memory. .view(ndarray_subclass) just returns an instance of
> ndarray_subclass that looks at the same array (same shape, dtype,
> etc.). This does not cause a reinterpretation of the memory.
>
> These are two completely different things, unfortunately conflated
> into the same method.
One way to distinguish the difference in code is to use
x.view(dtype=float)
or
x.view(type=matrix)
A person can even do both:
x.view(dtype=float, type=matrix)
Not ideal, but maybe a little bit better.
Cheers
Stéfan
More information about the Numpy-discussion
mailing list