From: Travis N. V. <tr...@en...> - 2006-06-06 18:55:56
|
I'd like to construct an array of tuples and I'm not sure how (without looping). Is there a quick way to do this with dtype? I've tried: >>> import numpy >>> x = [(1,2,3),(4,5,6)] >>> numpy.array(x) array([[1, 2, 3], [4, 5, 6]]) >>> numpy.array(x, dtype='p') array([[1, 2, 3], [4, 5, 6]]) >>> numpy.array(x, dtype='O') array([[1, 2, 3], [4, 5, 6]], dtype=object) Thanks, Travis |
From: Christopher B. <Chr...@no...> - 2006-06-06 20:22:17
|
Travis N. Vaught wrote: > I'd like to construct an array of tuples and I'm not sure how (without > looping). Is this what you want? >>> import numpy as N >>> a = N.empty((2,),dtype=object) >>> a[:] = [(1,2,3),(4,5,6)] >>> a array([(1, 2, 3), (4, 5, 6)], dtype=object) >>> a.shape (2,) By the way, I notice that the object dtype is not in the numpy namespace. While this mikes sense, as it's part of python, I keep getting confused because I do need to use numpy-specific dtypes for other things. I never use import *, so it might be a good idea to put the standard objects dtypes in the numpy namespace too. Or maybe not, just thinking out loud. Note: PyObject is there, but isn't that a deprecated Numeric name? -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: David M. C. <co...@ph...> - 2006-06-06 21:07:16
|
On Tue, 06 Jun 2006 13:21:56 -0700 Christopher Barker <Chr...@no...> wrote: > > > Travis N. Vaught wrote: > > I'd like to construct an array of tuples and I'm not sure how (without > > looping). > > Is this what you want? > > >>> import numpy as N > >>> a = N.empty((2,),dtype=object) > >>> a[:] = [(1,2,3),(4,5,6)] > >>> a > array([(1, 2, 3), (4, 5, 6)], dtype=object) > >>> a.shape > (2,) > > By the way, I notice that the object dtype is not in the numpy > namespace. While this mikes sense, as it's part of python, I keep > getting confused because I do need to use numpy-specific dtypes for > other things. I never use import *, so it might be a good idea to put > the standard objects dtypes in the numpy namespace too. Or maybe not, > just thinking out loud. None of the Python types are (int, float, etc.). For one reason, various Python checkers complain about overwriting a builtin type, and plus, I think it's messy and a potential for bugs. numpy takes those as convenience types, and converts them to the appropriate dtype. If you want the dtype used, it's spelled with an appended _. So in this case you'd want dtype=N.object_. N.object0 works too. -- |>|\/|< /--------------------------------------------------------------------------\ |David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/ |co...@ph... |
From: Christopher B. <Chr...@no...> - 2006-06-06 21:15:29
|
David M. Cooke wrote: > If you want the dtype > used, it's spelled with an appended _. > > So in this case you'd want dtype=N.object_. N.object0 works too. That will work, thanks. But what does object0 mean? -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: David M. C. <co...@ph...> - 2006-06-06 22:02:45
|
On Tue, 06 Jun 2006 14:15:14 -0700 Christopher Barker <Chr...@no...> wrote: > David M. Cooke wrote: > > If you want the dtype > > used, it's spelled with an appended _. > > > > So in this case you'd want dtype=N.object_. N.object0 works too. > > That will work, thanks. But what does object0 mean? I think it's "type object, default size". It's a holdover from Numeric. int0, for instance, is the same as int_ (= int64 on my 64-bit box, for instance). -- |>|\/|< /--------------------------------------------------------------------------\ |David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/ |co...@ph... |
From: Stefan v. d. W. <st...@su...> - 2006-06-06 21:02:30
|
On Tue, Jun 06, 2006 at 01:05:43PM -0500, Travis N. Vaught wrote: > looping). Is there a quick way to do this with dtype? >=20 > I've tried: >=20 > >>> import numpy > >>> x =3D [(1,2,3),(4,5,6)] > >>> numpy.array(x) > array([[1, 2, 3], > [4, 5, 6]]) > >>> numpy.array(x, dtype=3D'p') > array([[1, 2, 3], > [4, 5, 6]]) > >>> numpy.array(x, dtype=3D'O') > array([[1, 2, 3], > [4, 5, 6]], dtype=3Dobject) It works if you pre-allocate the array: In [18]: x =3D [(1,2),(3,4)] In [19]: z =3D N.empty(len(x),dtype=3D'O') In [20]: z[:] =3D x In [21]: z Out[21]: array([(1, 2), (3, 4)], dtype=3Dobject) Regards St=E9fan |
From: Christopher B. <Chr...@no...> - 2006-06-06 21:35:34
|
Stefan van der Walt wrote: > In [19]: z = N.empty(len(x),dtype='O') Which brings up: What is the "preferred" way to refer to types? String typecode or object? -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: Robert K. <rob...@gm...> - 2006-06-06 21:40:18
|
Christopher Barker wrote: > Stefan van der Walt wrote: > >>In [19]: z = N.empty(len(x),dtype='O') > > Which brings up: > > What is the "preferred" way to refer to types? String typecode or object? Object! The string typecodes are for backwards compatibility only. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |