
All, I have a set of arrays that I want to transform to records. Viewing them as a new dtype is usually sufficient, but fails occasionally. Here's an example: #--------------------------------------- import numpy as np testdtype = [('a',float),('b',float),('c',float)] test = np.random.rand(15).reshape(5,3) # View the (5,3) array as 5 records of 3 fields newrecord = test.view(testdtype) # Create a new array with the wrong shape test = np.random.rand(15).reshape(3,5) #Try to view it try: newrecord = test.T.view(testdtype) except ValueError, msg: print "Error creating new record on transpose: %s" % msg # That failed, but won't with a copy try: newrecord = test.T.copy().view(testdtype) except ValueError, msg: print "Error creating new record on transpose+copy: %s" % msg #--------------------------------------- * Could somebody explain me what goes wrong in the second case (transpose+view) ? Is it because the transpose doesn't own the data ? * Is there a way to transform my (3,5) array into a (5,) recordarray without a copy ? Thanks a lot in advance.

On Thu, May 29, 2008 at 2:05 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
All, I have a set of arrays that I want to transform to records. Viewing them as a new dtype is usually sufficient, but fails occasionally. Here's an example:
#--------------------------------------- import numpy as np testdtype = [('a',float),('b',float),('c',float)] test = np.random.rand(15).reshape(5,3) # View the (5,3) array as 5 records of 3 fields newrecord = test.view(testdtype) # Create a new array with the wrong shape test = np.random.rand(15).reshape(3,5) #Try to view it try: newrecord = test.T.view(testdtype) except ValueError, msg: print "Error creating new record on transpose: %s" % msg # That failed, but won't with a copy try: newrecord = test.T.copy().view(testdtype) except ValueError, msg: print "Error creating new record on transpose+copy: %s" % msg #---------------------------------------
* Could somebody explain me what goes wrong in the second case (transpose+view) ? Is it because the transpose doesn't own the data ?
* Is there a way to transform my (3,5) array into a (5,) recordarray without a copy ?
I don't think so. The transpose is just a view, it doesn't move the elements around, =so the three elements you want to be contiguous, aren't. It's possible to transpose in place, but it can be a tricky operation and I don't think it is available in numpy. Chuck

On Thursday 29 May 2008 16:25:24 Charles R Harris wrote:
* Could somebody explain me what goes wrong in the second case (transpose+view) ? Is it because the transpose doesn't own the data ?
* Is there a way to transform my (3,5) array into a (5,) recordarray without a copy ?
I don't think so. The transpose is just a view, it doesn't move the elements around, =so the three elements you want to be contiguous, aren't. It's possible to transpose in place, but it can be a tricky operation and I don't think it is available in numpy.
Ahah, so that was a problem of contiguity. OK, that makes sense. Thanks.
participants (2)
-
Charles R Harris
-
Pierre GM