[SciPy-user] what does 'd' mean in array([0.0, 1.0, 2.0, 3.0], 'd')

Steve Schmerler elcorto at gmx.net
Thu Aug 30 12:59:58 EDT 2007


dmitrey wrote:
> Unfortunately noone of the links give answer to my questions:
> 1. What should I type in command line to obtain Python print output
> array([0.5, 1.5, 2.5, 3.5], 'd')

Nothing. Just use <array>.dtype to the the dtype of the array.

> if I just type
> print array([0.5, 1.5, 2.5, 3.5], double)
> I get
>  >>> print array([0.5, 1.5, 2.5, 3.5], double)
> [ 0.5  1.5  2.5  3.5]

Me too, with numpy arrays and the Ipython shell (and 'double' instead of double).

>  >>> print x
> array([0.5, 1.5, 2.5, 3.5], 'd')
> 

Hmmm. Maybe your shell behaves differently with when you print x.

> 2. Why the x (that "print x" yields array([0.5, 1.5, 2.5, 3.5], 'd')) 
> has some attributes as ordinary array (dir(x) = ['__copy__', 
> '__deepcopy__', 'astype', 'byteswapped', 'copy', 'iscontiguous', 
> 'itemsize', 'resize', 'savespace', 'spacesaver', 'tolist', 'toscalar', 
> 'tostring', 'typecode']), x.shape works((4,)),but x.size yields error?
> dir(ordinary array) yields much more fields:
>  >>> dir(a)
> ['T', '__abs__', '__add__', '__and__', '__array__', 
> '__array_finalize__', '__array_interface__', '__array_priority__', 
> '__array_struct__', '__array_wrap__', '__class__', '__contains__', 
> '__copy__', '__deepcopy__', '__delattr__', '__delitem__', 
> '__delslice__', '__div__', '__divmod__', '__doc__', '__eq__', 
> '__float__', '__floordiv__', '__ge__', '__getattribute__', 
> '__getitem__', '__getslice__', '__gt__', '__hash__', '__hex__', 
> '__iadd__', '__iand__', '__idiv__', '__ifloordiv__', '__ilshift__', 
> '__imod__', '__imul__', '__index__', '__init__', '__int__', 
> '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', 
> '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__long__', 
> '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', 
> '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', 
> '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', 
> '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', 
> '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', 
> '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__setitem__', 
> '__setslice__', '__setstate__', '__str__', '__sub__', '__truediv__', 
> '__xor__', 'all', 'any', 'argmax', 'argmin', 'argsort', 'astype', 
> 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 
> 'copy', 'ctypes', 'cumprod', 'cumsum', 'data', 'diagonal', 'dtype', 
> 'dump', 'dumps', 'fill', 'flags', 'flat', 'flatten', 'getfield', 'imag', 
> 'item', 'itemset', 'itemsize', 'max', 'mean', 'min', 'nbytes', 'ndim', 
> 'newbyteorder', 'nonzero', 'prod', 'ptp', 'put', 'ravel', 'real', 
> 'repeat', 'reshape', 'resize', 'round', 'searchsorted', 'setfield', 
> 'setflags', 'shape', 'size', 'sort', 'squeeze', 'std', 'strides', 'sum', 
> 'swapaxes', 'take', 'tofile', 'tolist', 'tostring', 'trace', 
> 'transpose', 'var', 'view']
> 
> It seems it's important - I got this x from fortran code, that uses 
> numpy (ALGENCAN).
>  >>> type(x)
> <type 'array'>
>  >>> x.shape
> (4,)
> 

I'm not familiar with this code, but from a little playing arround, I suspect 
that this code gives you back a Numeric array rather than a numpy array:

In [16]: import numpy

In [17]: import Numeric

In [18]: a = numpy.array([1,2,3], 'd')

In [19]: a
Out[19]: array([ 1.,  2.,  3.])

In [20]: type(a)
Out[20]: <type 'numpy.ndarray'>

In [21]: print dir(a)
['T', '__abs__', '__add__', '__and__', '__array__', '__array_finalize__', 
'__array_interface__', '__array_priority__', '__array_struct__', 
'__array_wrap__', '__class__', '__contains__', '__copy__', '__deepcopy__', 
'__delattr__', '__delitem__', '__delslice__', '__div__', '__divmod__', 
'__doc__', '__eq__', '__float__', '__floordiv__', '__ge__', '__getattribute__', 
'__getitem__', '__getslice__', '__gt__', '__hash__', '__hex__', '__iadd__', 
'__iand__', '__idiv__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', 
'__init__', '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__', 
'__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', 
'__long__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', 
'__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', 
'__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', 
'__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', 
'__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', 
'__rtruediv__', '__rxor__', '__setattr__', '__setitem__', '__setslice__', 
'__setstate__', '__str__', '__sub__', '__truediv__', '__xor__', 'all', 'any', 
'argmax', 'argmin', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 
'compress', 'conj', 'conjugate', 'copy', 'ctypes', 'cumprod', 'cumsum', 'data', 
'diagonal', 'dtype', 'dump', 'dumps', 'fill', 'flags', 'flat', 'flatten', 
'getfield', 'imag', 'item', 'itemset', 'itemsize', 'max', 'mean', 'min', 
'nbytes', 'ndim', 'newbyteorder', 'nonzero', 'prod', 'ptp', 'put', 'ravel', 
'real', 'repeat', 'reshape', 'resize', 'round', 'searchsorted', 'setfield', 
'setflags', 'shape', 'size', 'sort', 'squeeze', 'std', 'strides', 'sum', 
'swapaxes', 'take', 'tofile', 'tolist', 'tostring', 'trace', 'transpose', 
'var', 'view']

In [22]: a.size
Out[22]: 3

In [23]: x = Numeric.array([1,2,3], 'd')

In [24]: x
Out[24]: array([ 1.,  2.,  3.])

In [25]: type(x)
Out[25]: <type 'array'>

In [26]: print dir(x)
['__copy__', '__deepcopy__', 'astype', 'byteswapped', 'copy', 'iscontiguous', 
'itemsize', 'resize', 'savespace', 'spacesaver', 'tolist', 'toscalar', 
'tostring', 'typecode']

In [27]: x.size
---------------------------------------------------------------------------
exceptions.AttributeError                            Traceback (most recent 
call last)

/home/schmerler/work/pycts/<ipython console>

AttributeError: size


But even with Numeric arrays, the 'd' isn't printed for me, neither with 
IPython, nor with the normal Python shell.

-- 
cheers,
steve

Random number generation is the art of producing pure gibberish as quickly as 
possible.



More information about the SciPy-User mailing list