glossary.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. """
  2. ========
  3. Glossary
  4. ========
  5. .. glossary::
  6. along an axis
  7. Axes are defined for arrays with more than one dimension. A
  8. 2-dimensional array has two corresponding axes: the first running
  9. vertically downwards across rows (axis 0), and the second running
  10. horizontally across columns (axis 1).
  11. Many operations can take place along one of these axes. For example,
  12. we can sum each row of an array, in which case we operate along
  13. columns, or axis 1::
  14. >>> x = np.arange(12).reshape((3,4))
  15. >>> x
  16. array([[ 0, 1, 2, 3],
  17. [ 4, 5, 6, 7],
  18. [ 8, 9, 10, 11]])
  19. >>> x.sum(axis=1)
  20. array([ 6, 22, 38])
  21. array
  22. A homogeneous container of numerical elements. Each element in the
  23. array occupies a fixed amount of memory (hence homogeneous), and
  24. can be a numerical element of a single type (such as float, int
  25. or complex) or a combination (such as ``(float, int, float)``). Each
  26. array has an associated data-type (or ``dtype``), which describes
  27. the numerical type of its elements::
  28. >>> x = np.array([1, 2, 3], float)
  29. >>> x
  30. array([ 1., 2., 3.])
  31. >>> x.dtype # floating point number, 64 bits of memory per element
  32. dtype('float64')
  33. # More complicated data type: each array element is a combination of
  34. # and integer and a floating point number
  35. >>> np.array([(1, 2.0), (3, 4.0)], dtype=[('x', int), ('y', float)])
  36. array([(1, 2.0), (3, 4.0)],
  37. dtype=[('x', '<i4'), ('y', '<f8')])
  38. Fast element-wise operations, called a :term:`ufunc`, operate on arrays.
  39. array_like
  40. Any sequence that can be interpreted as an ndarray. This includes
  41. nested lists, tuples, scalars and existing arrays.
  42. attribute
  43. A property of an object that can be accessed using ``obj.attribute``,
  44. e.g., ``shape`` is an attribute of an array::
  45. >>> x = np.array([1, 2, 3])
  46. >>> x.shape
  47. (3,)
  48. big-endian
  49. When storing a multi-byte value in memory as a sequence of bytes, the
  50. sequence addresses/sends/stores the most significant byte first (lowest
  51. address) and the least significant byte last (highest address). Common in
  52. micro-processors and used for transmission of data over network protocols.
  53. BLAS
  54. `Basic Linear Algebra Subprograms <https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms>`_
  55. broadcast
  56. NumPy can do operations on arrays whose shapes are mismatched::
  57. >>> x = np.array([1, 2])
  58. >>> y = np.array([[3], [4]])
  59. >>> x
  60. array([1, 2])
  61. >>> y
  62. array([[3],
  63. [4]])
  64. >>> x + y
  65. array([[4, 5],
  66. [5, 6]])
  67. See `numpy.doc.broadcasting` for more information.
  68. C order
  69. See `row-major`
  70. column-major
  71. A way to represent items in a N-dimensional array in the 1-dimensional
  72. computer memory. In column-major order, the leftmost index "varies the
  73. fastest": for example the array::
  74. [[1, 2, 3],
  75. [4, 5, 6]]
  76. is represented in the column-major order as::
  77. [1, 4, 2, 5, 3, 6]
  78. Column-major order is also known as the Fortran order, as the Fortran
  79. programming language uses it.
  80. decorator
  81. An operator that transforms a function. For example, a ``log``
  82. decorator may be defined to print debugging information upon
  83. function execution::
  84. >>> def log(f):
  85. ... def new_logging_func(*args, **kwargs):
  86. ... print("Logging call with parameters:", args, kwargs)
  87. ... return f(*args, **kwargs)
  88. ...
  89. ... return new_logging_func
  90. Now, when we define a function, we can "decorate" it using ``log``::
  91. >>> @log
  92. ... def add(a, b):
  93. ... return a + b
  94. Calling ``add`` then yields:
  95. >>> add(1, 2)
  96. Logging call with parameters: (1, 2) {}
  97. 3
  98. dictionary
  99. Resembling a language dictionary, which provides a mapping between
  100. words and descriptions thereof, a Python dictionary is a mapping
  101. between two objects::
  102. >>> x = {1: 'one', 'two': [1, 2]}
  103. Here, `x` is a dictionary mapping keys to values, in this case
  104. the integer 1 to the string "one", and the string "two" to
  105. the list ``[1, 2]``. The values may be accessed using their
  106. corresponding keys::
  107. >>> x[1]
  108. 'one'
  109. >>> x['two']
  110. [1, 2]
  111. Note that dictionaries are not stored in any specific order. Also,
  112. most mutable (see *immutable* below) objects, such as lists, may not
  113. be used as keys.
  114. For more information on dictionaries, read the
  115. `Python tutorial <https://docs.python.org/tutorial/>`_.
  116. field
  117. In a :term:`structured data type`, each sub-type is called a `field`.
  118. The `field` has a name (a string), a type (any valid :term:`dtype`, and
  119. an optional `title`. See :ref:`arrays.dtypes`
  120. Fortran order
  121. See `column-major`
  122. flattened
  123. Collapsed to a one-dimensional array. See `numpy.ndarray.flatten`
  124. for details.
  125. homogenous
  126. Describes a block of memory comprised of blocks, each block comprised of
  127. items and of the same size, and blocks are interpreted in exactly the
  128. same way. In the simplest case each block contains a single item, for
  129. instance int32 or float64.
  130. immutable
  131. An object that cannot be modified after execution is called
  132. immutable. Two common examples are strings and tuples.
  133. instance
  134. A class definition gives the blueprint for constructing an object::
  135. >>> class House(object):
  136. ... wall_colour = 'white'
  137. Yet, we have to *build* a house before it exists::
  138. >>> h = House() # build a house
  139. Now, ``h`` is called a ``House`` instance. An instance is therefore
  140. a specific realisation of a class.
  141. iterable
  142. A sequence that allows "walking" (iterating) over items, typically
  143. using a loop such as::
  144. >>> x = [1, 2, 3]
  145. >>> [item**2 for item in x]
  146. [1, 4, 9]
  147. It is often used in combination with ``enumerate``::
  148. >>> keys = ['a','b','c']
  149. >>> for n, k in enumerate(keys):
  150. ... print("Key %d: %s" % (n, k))
  151. ...
  152. Key 0: a
  153. Key 1: b
  154. Key 2: c
  155. list
  156. A Python container that can hold any number of objects or items.
  157. The items do not have to be of the same type, and can even be
  158. lists themselves::
  159. >>> x = [2, 2.0, "two", [2, 2.0]]
  160. The list `x` contains 4 items, each which can be accessed individually::
  161. >>> x[2] # the string 'two'
  162. 'two'
  163. >>> x[3] # a list, containing an integer 2 and a float 2.0
  164. [2, 2.0]
  165. It is also possible to select more than one item at a time,
  166. using *slicing*::
  167. >>> x[0:2] # or, equivalently, x[:2]
  168. [2, 2.0]
  169. In code, arrays are often conveniently expressed as nested lists::
  170. >>> np.array([[1, 2], [3, 4]])
  171. array([[1, 2],
  172. [3, 4]])
  173. For more information, read the section on lists in the `Python
  174. tutorial <https://docs.python.org/tutorial/>`_. For a mapping
  175. type (key-value), see *dictionary*.
  176. little-endian
  177. When storing a multi-byte value in memory as a sequence of bytes, the
  178. sequence addresses/sends/stores the least significant byte first (lowest
  179. address) and the most significant byte last (highest address). Common in
  180. x86 processors.
  181. mask
  182. A boolean array, used to select only certain elements for an operation::
  183. >>> x = np.arange(5)
  184. >>> x
  185. array([0, 1, 2, 3, 4])
  186. >>> mask = (x > 2)
  187. >>> mask
  188. array([False, False, False, True, True])
  189. >>> x[mask] = -1
  190. >>> x
  191. array([ 0, 1, 2, -1, -1])
  192. masked array
  193. Array that suppressed values indicated by a mask::
  194. >>> x = np.ma.masked_array([np.nan, 2, np.nan], [True, False, True])
  195. >>> x
  196. masked_array(data = [-- 2.0 --],
  197. mask = [ True False True],
  198. fill_value = 1e+20)
  199. <BLANKLINE>
  200. >>> x + [1, 2, 3]
  201. masked_array(data = [-- 4.0 --],
  202. mask = [ True False True],
  203. fill_value = 1e+20)
  204. <BLANKLINE>
  205. Masked arrays are often used when operating on arrays containing
  206. missing or invalid entries.
  207. matrix
  208. A 2-dimensional ndarray that preserves its two-dimensional nature
  209. throughout operations. It has certain special operations, such as ``*``
  210. (matrix multiplication) and ``**`` (matrix power), defined::
  211. >>> x = np.mat([[1, 2], [3, 4]])
  212. >>> x
  213. matrix([[1, 2],
  214. [3, 4]])
  215. >>> x**2
  216. matrix([[ 7, 10],
  217. [15, 22]])
  218. method
  219. A function associated with an object. For example, each ndarray has a
  220. method called ``repeat``::
  221. >>> x = np.array([1, 2, 3])
  222. >>> x.repeat(2)
  223. array([1, 1, 2, 2, 3, 3])
  224. ndarray
  225. See *array*.
  226. record array
  227. An :term:`ndarray` with :term:`structured data type` which has been
  228. subclassed as ``np.recarray`` and whose dtype is of type ``np.record``,
  229. making the fields of its data type to be accessible by attribute.
  230. reference
  231. If ``a`` is a reference to ``b``, then ``(a is b) == True``. Therefore,
  232. ``a`` and ``b`` are different names for the same Python object.
  233. row-major
  234. A way to represent items in a N-dimensional array in the 1-dimensional
  235. computer memory. In row-major order, the rightmost index "varies
  236. the fastest": for example the array::
  237. [[1, 2, 3],
  238. [4, 5, 6]]
  239. is represented in the row-major order as::
  240. [1, 2, 3, 4, 5, 6]
  241. Row-major order is also known as the C order, as the C programming
  242. language uses it. New NumPy arrays are by default in row-major order.
  243. self
  244. Often seen in method signatures, ``self`` refers to the instance
  245. of the associated class. For example:
  246. >>> class Paintbrush(object):
  247. ... color = 'blue'
  248. ...
  249. ... def paint(self):
  250. ... print("Painting the city %s!" % self.color)
  251. ...
  252. >>> p = Paintbrush()
  253. >>> p.color = 'red'
  254. >>> p.paint() # self refers to 'p'
  255. Painting the city red!
  256. slice
  257. Used to select only certain elements from a sequence::
  258. >>> x = range(5)
  259. >>> x
  260. [0, 1, 2, 3, 4]
  261. >>> x[1:3] # slice from 1 to 3 (excluding 3 itself)
  262. [1, 2]
  263. >>> x[1:5:2] # slice from 1 to 5, but skipping every second element
  264. [1, 3]
  265. >>> x[::-1] # slice a sequence in reverse
  266. [4, 3, 2, 1, 0]
  267. Arrays may have more than one dimension, each which can be sliced
  268. individually::
  269. >>> x = np.array([[1, 2], [3, 4]])
  270. >>> x
  271. array([[1, 2],
  272. [3, 4]])
  273. >>> x[:, 1]
  274. array([2, 4])
  275. structure
  276. See :term:`structured data type`
  277. structured data type
  278. A data type composed of other datatypes
  279. tuple
  280. A sequence that may contain a variable number of types of any
  281. kind. A tuple is immutable, i.e., once constructed it cannot be
  282. changed. Similar to a list, it can be indexed and sliced::
  283. >>> x = (1, 'one', [1, 2])
  284. >>> x
  285. (1, 'one', [1, 2])
  286. >>> x[0]
  287. 1
  288. >>> x[:2]
  289. (1, 'one')
  290. A useful concept is "tuple unpacking", which allows variables to
  291. be assigned to the contents of a tuple::
  292. >>> x, y = (1, 2)
  293. >>> x, y = 1, 2
  294. This is often used when a function returns multiple values:
  295. >>> def return_many():
  296. ... return 1, 'alpha', None
  297. >>> a, b, c = return_many()
  298. >>> a, b, c
  299. (1, 'alpha', None)
  300. >>> a
  301. 1
  302. >>> b
  303. 'alpha'
  304. ufunc
  305. Universal function. A fast element-wise array operation. Examples include
  306. ``add``, ``sin`` and ``logical_or``.
  307. view
  308. An array that does not own its data, but refers to another array's
  309. data instead. For example, we may create a view that only shows
  310. every second element of another array::
  311. >>> x = np.arange(5)
  312. >>> x
  313. array([0, 1, 2, 3, 4])
  314. >>> y = x[::2]
  315. >>> y
  316. array([0, 2, 4])
  317. >>> x[0] = 3 # changing x changes y as well, since y is a view on x
  318. >>> y
  319. array([3, 2, 4])
  320. wrapper
  321. Python is a high-level (highly abstracted, or English-like) language.
  322. This abstraction comes at a price in execution speed, and sometimes
  323. it becomes necessary to use lower level languages to do fast
  324. computations. A wrapper is code that provides a bridge between
  325. high and the low level languages, allowing, e.g., Python to execute
  326. code written in C or Fortran.
  327. Examples include ctypes, SWIG and Cython (which wraps C and C++)
  328. and f2py (which wraps Fortran).
  329. """
  330. from __future__ import division, absolute_import, print_function