2

I was wondering if it is possible to have a numpy.array as a datatype in a structured array. This is the idea:

import numpy

raw_data = [(1, numpy.array([1,2,3])), 
            (2, numpy.array([4,5,6])), 
            (3, numpy.array([7,8,9]))]

data = numpy.array(raw_data, dtype=[('num', float),
                                    ('arr', numpy.array)])

I have a list of tuples consisting of an integer and an array and would like to turn this into a structured array. Right now, Python complains that it does not understand the 'numpy.array' datatype. Is there another way to refer to the array datatype?

The motivation behind is to be able to do things like:

print numpy.min(data['arr'], axis=0)
print numpy.min(data['arr'], axis=1)

and other operations.

1 Answer 1

4

Yes, you can create compound fields that look like arrays within the structured array; for example:

import numpy as np
raw_data = [(1, np.array([1,2,3])), 
            (2, np.array([4,5,6])), 
            (3, np.array([7,8,9]))]

tp = np.dtype([('id', int), ('arr', float, (3,))])

x = np.array(raw_data, dtype=tp)

Result looks like this:

>>> x
array([(1, [1.0, 2.0, 3.0]), (2, [4.0, 5.0, 6.0]), (3, [7.0, 8.0, 9.0])], 
      dtype=[('id', '<i8'), ('arr', '<f8', (3,))])
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! Thanks for the quick reply!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.