[Numpy-discussion] correct way to specify type in array definition

David M. Cooke cookedm@physics.mcmaster...
Thu Mar 15 16:18:15 CDT 2007


On Thu, Mar 15, 2007 at 11:23:36AM +0100, Francesc Altet wrote:
> El dj 15 de 03 del 2007 a les 06:01 -0400, en/na Brian Blais va
> escriure:
> > Hello,
> > 
> > Can someone tell me what the preferred way to specify the type of an array?  I want
> > it to be a float array, no matter what is given (say, integers).  I can do:
> > 
> > a=numpy.array([1,2,3],numpy.dtype('float'))
> > 
> > or
> > 
> > a=numpy.array([1,2,3],type(1.0))
> > 
> > or perhaps many others.  Is there a way that is recommended?
> 
> Well, this depends on your preferences, I guess, but I like to be
> explicit, so I normally use:
> 
> a=numpy.array([1,2,3], numpy.float64)
> 
> but, if you are a bit lazy to type, the next is just fine as well:
> 
> a=numpy.array([1,2,3], 'f8')
> 

I just do

a = numpy.array([1,2,3], dtype=float)

The Python types int, float, and bool translate to numpy.int_,
numpy.double, and numpy.bool (i.e., the C equivalents of the Pythonn
types; note that int_ is a C long).

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm@physics.mcmaster.ca


More information about the Numpy-discussion mailing list