[Numpy-discussion] Structured array dtype
Sat Aug 31 02:05:11 CDT 2013
Thanks for the explanation.
In fact, since the 4 components of 'b' are contiguous in memory, I wanted to find a way to express that fact in the dtype.
Z = np.zeros(10, [('a', np.float32, 3), ('b', np.float32, 4)])
Z = np.zeros((10,4), np.float32)
Z = np.zeros(10, (np.float32,4))
On Aug 31, 2013, at 7:51 AM, Stéfan van der Walt <firstname.lastname@example.org> wrote:
> Hi Nicolas
> On Fri, 30 Aug 2013 17:26:51 +0200, Nicolas Rougier wrote:
>>>>> Z = np.zeros(10, [('a', np.float32, 3), ('b', np.float32, 4)])
>> dtype(('<f4', (3,)))
>> Does that mean that dtype['a'] is the dtype of field 'a' when in Z, while Z['a'].dtype is the dtype of field 'a' when "extracted" or my way of thinking is totally wrong ?
> Apologies if this is a duplicate response; I'm sending it offline.
> In case 1, you are indexing into the array, and querying its dtype. In case
> two, you are indexing into a dtype.
> I.e., in case two, you are doing this:
> In : dtype = np.dtype([('a', float, 3), ('b', int)])
> In : dtype['a']
> Out: dtype(('<f8', (3,)))
>> What bothers me the most is that I cannot do:
>> ValueError: new type not compatible with array.
> That's quite a tricky operation to perform, since it has to take into account
> the underlying strides of the old array as well as calculate a shape for the
> new array. It should be possible to make it work using something similar to
> `np.lib.stride_tricks.as_strided`, but my quick attempt failed because of the
> In : class Foo:
> __array_interface__ = Z.__array_interface__
> In : f = Foo()
> In : np.asarray(f)
> array([, ,
> , ,
> , ,
> , ,
> , ],
> This does not seem right.
> NumPy-Discussion mailing list
More information about the NumPy-Discussion