glossary.py 12 KB

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