newbie: convert recarray to floatingpoint ndarray with mixed types
Apologies for what is likely a simple question and I hope it hasn't been asked before ... Given a recarray with a dtype consisting of more than one type, e.g.
import numpy as n a = n.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) b = a.view(n.recarray) b rec.array([(1.0, 2), (3.0, 4)], dtype=[('x', '<f8'), ('y', '<i4')])
Is there a simple way to convert 'b' to a floatingpoint ndarray, casting the integer field to a floatingpoint? I've tried the naïve:
c = b.view(dtype='float').reshape(b.size,1)
but that fails with: ValueError: new type not compatible with array. I understand why this would fail (as it is a view and not a copy), but I'm lost on a method to do this conversion simply. thanks, matt
On 05/12/2010 12:37 PM, Gregory, Matthew wrote:
Apologies for what is likely a simple question and I hope it hasn't been asked before ...
Given a recarray with a dtype consisting of more than one type, e.g.
import numpy as n a = n.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) b = a.view(n.recarray) b rec.array([(1.0, 2), (3.0, 4)], dtype=[('x', '<f8'), ('y', '<i4')])
Is there a simple way to convert 'b' to a floatingpoint ndarray, casting the integer field to a floatingpoint? I've tried the naïve:
c = b.view(dtype='float').reshape(b.size,1)
but that fails with:
ValueError: new type not compatible with array.
I understand why this would fail (as it is a view and not a copy), but I'm lost on a method to do this conversion simply.
It may not be as simple as you would like, but the following works efficiently: import numpy as np a = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) b = np.empty((a.shape[0], 2), dtype=np.float) b[:,0] = a['x'] b[:,1] = a['y'] Eric
thanks, matt
If you want to do it in just one line (the third line below), this seems to work  unless you have zillions of types in the structured array it should be plenty fast, too:
import numpy as np A = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) array([A[n] for n in A.dtype.names],dtype=float).T array([[1., 2.], [3., 4.]])
You may or may not want the transpose depending on which way you meant to have the matrix aligned... On Wed, May 12, 2010 at 10:40 PM, Eric Firing <efiring@hawaii.edu> wrote:
On 05/12/2010 12:37 PM, Gregory, Matthew wrote:
Apologies for what is likely a simple question and I hope it hasn't been asked before ...
Given a recarray with a dtype consisting of more than one type, e.g.
>>> import numpy as n >>> a = n.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) >>> b = a.view(n.recarray) >>> b rec.array([(1.0, 2), (3.0, 4)], dtype=[('x', '<f8'), ('y', '<i4')])
Is there a simple way to convert 'b' to a floatingpoint ndarray, casting the integer field to a floatingpoint? I've tried the naïve:
>>> c = b.view(dtype='float').reshape(b.size,1)
but that fails with:
ValueError: new type not compatible with array.
I understand why this would fail (as it is a view and not a copy), but I'm lost on a method to do this conversion simply.
It may not be as simple as you would like, but the following works efficiently:
import numpy as np a = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) b = np.empty((a.shape[0], 2), dtype=np.float) b[:,0] = a['x'] b[:,1] = a['y']
Eric
thanks, matt
NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
 Erik Tollerud http://ps.uci.edu/~etolleru
participants (3)

Eric Firing

Erik Tollerud

Gregory, Matthew