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