basics.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """
  2. ============
  3. Array basics
  4. ============
  5. Array types and conversions between types
  6. =========================================
  7. Numpy supports a much greater variety of numerical types than Python does.
  8. This section shows which are available, and how to modify an array's data-type.
  9. ========== ==========================================================
  10. Data type Description
  11. ========== ==========================================================
  12. bool_ Boolean (True or False) stored as a byte
  13. int_ Default integer type (same as C ``long``; normally either
  14. ``int64`` or ``int32``)
  15. intc Identical to C ``int`` (normally ``int32`` or ``int64``)
  16. intp Integer used for indexing (same as C ``ssize_t``; normally
  17. either ``int32`` or ``int64``)
  18. int8 Byte (-128 to 127)
  19. int16 Integer (-32768 to 32767)
  20. int32 Integer (-2147483648 to 2147483647)
  21. int64 Integer (-9223372036854775808 to 9223372036854775807)
  22. uint8 Unsigned integer (0 to 255)
  23. uint16 Unsigned integer (0 to 65535)
  24. uint32 Unsigned integer (0 to 4294967295)
  25. uint64 Unsigned integer (0 to 18446744073709551615)
  26. float_ Shorthand for ``float64``.
  27. float16 Half precision float: sign bit, 5 bits exponent,
  28. 10 bits mantissa
  29. float32 Single precision float: sign bit, 8 bits exponent,
  30. 23 bits mantissa
  31. float64 Double precision float: sign bit, 11 bits exponent,
  32. 52 bits mantissa
  33. complex_ Shorthand for ``complex128``.
  34. complex64 Complex number, represented by two 32-bit floats (real
  35. and imaginary components)
  36. complex128 Complex number, represented by two 64-bit floats (real
  37. and imaginary components)
  38. ========== ==========================================================
  39. Additionally to ``intc`` the platform dependent C integer types ``short``,
  40. ``long``, ``longlong`` and their unsigned versions are defined.
  41. Numpy numerical types are instances of ``dtype`` (data-type) objects, each
  42. having unique characteristics. Once you have imported NumPy using
  43. ::
  44. >>> import numpy as np
  45. the dtypes are available as ``np.bool_``, ``np.float32``, etc.
  46. Advanced types, not listed in the table above, are explored in
  47. section :ref:`structured_arrays`.
  48. There are 5 basic numerical types representing booleans (bool), integers (int),
  49. unsigned integers (uint) floating point (float) and complex. Those with numbers
  50. in their name indicate the bitsize of the type (i.e. how many bits are needed
  51. to represent a single value in memory). Some types, such as ``int`` and
  52. ``intp``, have differing bitsizes, dependent on the platforms (e.g. 32-bit
  53. vs. 64-bit machines). This should be taken into account when interfacing
  54. with low-level code (such as C or Fortran) where the raw memory is addressed.
  55. Data-types can be used as functions to convert python numbers to array scalars
  56. (see the array scalar section for an explanation), python sequences of numbers
  57. to arrays of that type, or as arguments to the dtype keyword that many numpy
  58. functions or methods accept. Some examples::
  59. >>> import numpy as np
  60. >>> x = np.float32(1.0)
  61. >>> x
  62. 1.0
  63. >>> y = np.int_([1,2,4])
  64. >>> y
  65. array([1, 2, 4])
  66. >>> z = np.arange(3, dtype=np.uint8)
  67. >>> z
  68. array([0, 1, 2], dtype=uint8)
  69. Array types can also be referred to by character codes, mostly to retain
  70. backward compatibility with older packages such as Numeric. Some
  71. documentation may still refer to these, for example::
  72. >>> np.array([1, 2, 3], dtype='f')
  73. array([ 1., 2., 3.], dtype=float32)
  74. We recommend using dtype objects instead.
  75. To convert the type of an array, use the .astype() method (preferred) or
  76. the type itself as a function. For example: ::
  77. >>> z.astype(float) #doctest: +NORMALIZE_WHITESPACE
  78. array([ 0., 1., 2.])
  79. >>> np.int8(z)
  80. array([0, 1, 2], dtype=int8)
  81. Note that, above, we use the *Python* float object as a dtype. NumPy knows
  82. that ``int`` refers to ``np.int_``, ``bool`` means ``np.bool_``,
  83. that ``float`` is ``np.float_`` and ``complex`` is ``np.complex_``.
  84. The other data-types do not have Python equivalents.
  85. To determine the type of an array, look at the dtype attribute::
  86. >>> z.dtype
  87. dtype('uint8')
  88. dtype objects also contain information about the type, such as its bit-width
  89. and its byte-order. The data type can also be used indirectly to query
  90. properties of the type, such as whether it is an integer::
  91. >>> d = np.dtype(int)
  92. >>> d
  93. dtype('int32')
  94. >>> np.issubdtype(d, int)
  95. True
  96. >>> np.issubdtype(d, float)
  97. False
  98. Array Scalars
  99. =============
  100. Numpy generally returns elements of arrays as array scalars (a scalar
  101. with an associated dtype). Array scalars differ from Python scalars, but
  102. for the most part they can be used interchangeably (the primary
  103. exception is for versions of Python older than v2.x, where integer array
  104. scalars cannot act as indices for lists and tuples). There are some
  105. exceptions, such as when code requires very specific attributes of a scalar
  106. or when it checks specifically whether a value is a Python scalar. Generally,
  107. problems are easily fixed by explicitly converting array scalars
  108. to Python scalars, using the corresponding Python type function
  109. (e.g., ``int``, ``float``, ``complex``, ``str``, ``unicode``).
  110. The primary advantage of using array scalars is that
  111. they preserve the array type (Python may not have a matching scalar type
  112. available, e.g. ``int16``). Therefore, the use of array scalars ensures
  113. identical behaviour between arrays and scalars, irrespective of whether the
  114. value is inside an array or not. NumPy scalars also have many of the same
  115. methods arrays do.
  116. Extended Precision
  117. ==================
  118. Python's floating-point numbers are usually 64-bit floating-point numbers,
  119. nearly equivalent to ``np.float64``. In some unusual situations it may be
  120. useful to use floating-point numbers with more precision. Whether this
  121. is possible in numpy depends on the hardware and on the development
  122. environment: specifically, x86 machines provide hardware floating-point
  123. with 80-bit precision, and while most C compilers provide this as their
  124. ``long double`` type, MSVC (standard for Windows builds) makes
  125. ``long double`` identical to ``double`` (64 bits). Numpy makes the
  126. compiler's ``long double`` available as ``np.longdouble`` (and
  127. ``np.clongdouble`` for the complex numbers). You can find out what your
  128. numpy provides with``np.finfo(np.longdouble)``.
  129. Numpy does not provide a dtype with more precision than C
  130. ``long double``s; in particular, the 128-bit IEEE quad precision
  131. data type (FORTRAN's ``REAL*16``) is not available.
  132. For efficient memory alignment, ``np.longdouble`` is usually stored
  133. padded with zero bits, either to 96 or 128 bits. Which is more efficient
  134. depends on hardware and development environment; typically on 32-bit
  135. systems they are padded to 96 bits, while on 64-bit systems they are
  136. typically padded to 128 bits. ``np.longdouble`` is padded to the system
  137. default; ``np.float96`` and ``np.float128`` are provided for users who
  138. want specific padding. In spite of the names, ``np.float96`` and
  139. ``np.float128`` provide only as much precision as ``np.longdouble``,
  140. that is, 80 bits on most x86 machines and 64 bits in standard
  141. Windows builds.
  142. Be warned that even if ``np.longdouble`` offers more precision than
  143. python ``float``, it is easy to lose that extra precision, since
  144. python often forces values to pass through ``float``. For example,
  145. the ``%`` formatting operator requires its arguments to be converted
  146. to standard python types, and it is therefore impossible to preserve
  147. extended precision even if many decimal places are requested. It can
  148. be useful to test your code with the value
  149. ``1 + np.finfo(np.longdouble).eps``.
  150. """
  151. from __future__ import division, absolute_import, print_function