basics.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. The primitive types supported are tied closely to those in C:
  10. .. list-table::
  11. :header-rows: 1
  12. * - Numpy type
  13. - C type
  14. - Description
  15. * - `np.bool`
  16. - ``bool``
  17. - Boolean (True or False) stored as a byte
  18. * - `np.byte`
  19. - ``signed char``
  20. - Platform-defined
  21. * - `np.ubyte`
  22. - ``unsigned char``
  23. - Platform-defined
  24. * - `np.short`
  25. - ``short``
  26. - Platform-defined
  27. * - `np.ushort`
  28. - ``unsigned short``
  29. - Platform-defined
  30. * - `np.intc`
  31. - ``int``
  32. - Platform-defined
  33. * - `np.uintc`
  34. - ``unsigned int``
  35. - Platform-defined
  36. * - `np.int_`
  37. - ``long``
  38. - Platform-defined
  39. * - `np.uint`
  40. - ``unsigned long``
  41. - Platform-defined
  42. * - `np.longlong`
  43. - ``long long``
  44. - Platform-defined
  45. * - `np.ulonglong`
  46. - ``unsigned long long``
  47. - Platform-defined
  48. * - `np.half` / `np.float16`
  49. -
  50. - Half precision float:
  51. sign bit, 5 bits exponent, 10 bits mantissa
  52. * - `np.single`
  53. - ``float``
  54. - Platform-defined single precision float:
  55. typically sign bit, 8 bits exponent, 23 bits mantissa
  56. * - `np.double`
  57. - ``double``
  58. - Platform-defined double precision float:
  59. typically sign bit, 11 bits exponent, 52 bits mantissa.
  60. * - `np.longdouble`
  61. - ``long double``
  62. - Platform-defined extended-precision float
  63. * - `np.csingle`
  64. - ``float complex``
  65. - Complex number, represented by two single-precision floats (real and imaginary components)
  66. * - `np.cdouble`
  67. - ``double complex``
  68. - Complex number, represented by two double-precision floats (real and imaginary components).
  69. * - `np.clongdouble`
  70. - ``long double complex``
  71. - Complex number, represented by two extended-precision floats (real and imaginary components).
  72. Since many of these have platform-dependent definitions, a set of fixed-size
  73. aliases are provided:
  74. .. list-table::
  75. :header-rows: 1
  76. * - Numpy type
  77. - C type
  78. - Description
  79. * - `np.int8`
  80. - ``int8_t``
  81. - Byte (-128 to 127)
  82. * - `np.int16`
  83. - ``int16_t``
  84. - Integer (-32768 to 32767)
  85. * - `np.int32`
  86. - ``int32_t``
  87. - Integer (-2147483648 to 2147483647)
  88. * - `np.int64`
  89. - ``int64_t``
  90. - Integer (-9223372036854775808 to 9223372036854775807)
  91. * - `np.uint8`
  92. - ``uint8_t``
  93. - Unsigned integer (0 to 255)
  94. * - `np.uint16`
  95. - ``uint16_t``
  96. - Unsigned integer (0 to 65535)
  97. * - `np.uint32`
  98. - ``uint32_t``
  99. - Unsigned integer (0 to 4294967295)
  100. * - `np.uint64`
  101. - ``uint64_t``
  102. - Unsigned integer (0 to 18446744073709551615)
  103. * - `np.intp`
  104. - ``intptr_t``
  105. - Integer used for indexing, typically the same as ``ssize_t``
  106. * - `np.uintp`
  107. - ``uintptr_t``
  108. - Integer large enough to hold a pointer
  109. * - `np.float32`
  110. - ``float``
  111. -
  112. * - `np.float64` / `np.float_`
  113. - ``double``
  114. - Note that this matches the precision of the builtin python `float`.
  115. * - `np.complex64`
  116. - ``float complex``
  117. - Complex number, represented by two 32-bit floats (real and imaginary components)
  118. * - `np.complex128` / `np.complex_`
  119. - ``double complex``
  120. - Note that this matches the precision of the builtin python `complex`.
  121. NumPy numerical types are instances of ``dtype`` (data-type) objects, each
  122. having unique characteristics. Once you have imported NumPy using
  123. ::
  124. >>> import numpy as np
  125. the dtypes are available as ``np.bool_``, ``np.float32``, etc.
  126. Advanced types, not listed in the table above, are explored in
  127. section :ref:`structured_arrays`.
  128. There are 5 basic numerical types representing booleans (bool), integers (int),
  129. unsigned integers (uint) floating point (float) and complex. Those with numbers
  130. in their name indicate the bitsize of the type (i.e. how many bits are needed
  131. to represent a single value in memory). Some types, such as ``int`` and
  132. ``intp``, have differing bitsizes, dependent on the platforms (e.g. 32-bit
  133. vs. 64-bit machines). This should be taken into account when interfacing
  134. with low-level code (such as C or Fortran) where the raw memory is addressed.
  135. Data-types can be used as functions to convert python numbers to array scalars
  136. (see the array scalar section for an explanation), python sequences of numbers
  137. to arrays of that type, or as arguments to the dtype keyword that many numpy
  138. functions or methods accept. Some examples::
  139. >>> import numpy as np
  140. >>> x = np.float32(1.0)
  141. >>> x
  142. 1.0
  143. >>> y = np.int_([1,2,4])
  144. >>> y
  145. array([1, 2, 4])
  146. >>> z = np.arange(3, dtype=np.uint8)
  147. >>> z
  148. array([0, 1, 2], dtype=uint8)
  149. Array types can also be referred to by character codes, mostly to retain
  150. backward compatibility with older packages such as Numeric. Some
  151. documentation may still refer to these, for example::
  152. >>> np.array([1, 2, 3], dtype='f')
  153. array([ 1., 2., 3.], dtype=float32)
  154. We recommend using dtype objects instead.
  155. To convert the type of an array, use the .astype() method (preferred) or
  156. the type itself as a function. For example: ::
  157. >>> z.astype(float) #doctest: +NORMALIZE_WHITESPACE
  158. array([ 0., 1., 2.])
  159. >>> np.int8(z)
  160. array([0, 1, 2], dtype=int8)
  161. Note that, above, we use the *Python* float object as a dtype. NumPy knows
  162. that ``int`` refers to ``np.int_``, ``bool`` means ``np.bool_``,
  163. that ``float`` is ``np.float_`` and ``complex`` is ``np.complex_``.
  164. The other data-types do not have Python equivalents.
  165. To determine the type of an array, look at the dtype attribute::
  166. >>> z.dtype
  167. dtype('uint8')
  168. dtype objects also contain information about the type, such as its bit-width
  169. and its byte-order. The data type can also be used indirectly to query
  170. properties of the type, such as whether it is an integer::
  171. >>> d = np.dtype(int)
  172. >>> d
  173. dtype('int32')
  174. >>> np.issubdtype(d, np.integer)
  175. True
  176. >>> np.issubdtype(d, np.floating)
  177. False
  178. Array Scalars
  179. =============
  180. NumPy generally returns elements of arrays as array scalars (a scalar
  181. with an associated dtype). Array scalars differ from Python scalars, but
  182. for the most part they can be used interchangeably (the primary
  183. exception is for versions of Python older than v2.x, where integer array
  184. scalars cannot act as indices for lists and tuples). There are some
  185. exceptions, such as when code requires very specific attributes of a scalar
  186. or when it checks specifically whether a value is a Python scalar. Generally,
  187. problems are easily fixed by explicitly converting array scalars
  188. to Python scalars, using the corresponding Python type function
  189. (e.g., ``int``, ``float``, ``complex``, ``str``, ``unicode``).
  190. The primary advantage of using array scalars is that
  191. they preserve the array type (Python may not have a matching scalar type
  192. available, e.g. ``int16``). Therefore, the use of array scalars ensures
  193. identical behaviour between arrays and scalars, irrespective of whether the
  194. value is inside an array or not. NumPy scalars also have many of the same
  195. methods arrays do.
  196. Extended Precision
  197. ==================
  198. Python's floating-point numbers are usually 64-bit floating-point numbers,
  199. nearly equivalent to ``np.float64``. In some unusual situations it may be
  200. useful to use floating-point numbers with more precision. Whether this
  201. is possible in numpy depends on the hardware and on the development
  202. environment: specifically, x86 machines provide hardware floating-point
  203. with 80-bit precision, and while most C compilers provide this as their
  204. ``long double`` type, MSVC (standard for Windows builds) makes
  205. ``long double`` identical to ``double`` (64 bits). NumPy makes the
  206. compiler's ``long double`` available as ``np.longdouble`` (and
  207. ``np.clongdouble`` for the complex numbers). You can find out what your
  208. numpy provides with ``np.finfo(np.longdouble)``.
  209. NumPy does not provide a dtype with more precision than C
  210. ``long double``\\s; in particular, the 128-bit IEEE quad precision
  211. data type (FORTRAN's ``REAL*16``\\) is not available.
  212. For efficient memory alignment, ``np.longdouble`` is usually stored
  213. padded with zero bits, either to 96 or 128 bits. Which is more efficient
  214. depends on hardware and development environment; typically on 32-bit
  215. systems they are padded to 96 bits, while on 64-bit systems they are
  216. typically padded to 128 bits. ``np.longdouble`` is padded to the system
  217. default; ``np.float96`` and ``np.float128`` are provided for users who
  218. want specific padding. In spite of the names, ``np.float96`` and
  219. ``np.float128`` provide only as much precision as ``np.longdouble``,
  220. that is, 80 bits on most x86 machines and 64 bits in standard
  221. Windows builds.
  222. Be warned that even if ``np.longdouble`` offers more precision than
  223. python ``float``, it is easy to lose that extra precision, since
  224. python often forces values to pass through ``float``. For example,
  225. the ``%`` formatting operator requires its arguments to be converted
  226. to standard python types, and it is therefore impossible to preserve
  227. extended precision even if many decimal places are requested. It can
  228. be useful to test your code with the value
  229. ``1 + np.finfo(np.longdouble).eps``.
  230. """
  231. from __future__ import division, absolute_import, print_function