"*NumPy is the fundamental library for scientific computing with Python. NumPy is centered around a powerful N-dimensional array object, and it also contains useful linear algebra, Fourier transform, and random number functions.*\n",
"The `zeros` function creates an array containing any number of zeros:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print np.zeros(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's just as easy to create a 2D array (ie. a matrix) by providing a tuple with the desired number of rows and columns. For example, here's a 3x4 matrix:"
"For this reason, it is generally preferable to use the `linspace` function instead of `arange` when working with floats. The `linspace` function returns an array containing a specific number of points evenly distributed between two values (note that the maximum value is *included*, contrary to `arange`):"
"Here's a 3x4 matrix containing random floats sampled from a univariate [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution) (Gaussian distribution) of mean 0 and variance 1:"
"To give you a feel of what these distributions look like, let's use matplotlib (see the [matplotlib tutorial](tools_matplotlib.ipynb) for more details):"
"NumPy first creates three `ndarrays` (one per dimension), each of shape `(3, 2, 10)`. Each array has values equal to the coordinate along a specific axis. For example, all elements in the `z` array are equal to their z-coordinate:\n",
"So the terms x, y and z in the expression `x * y + z` above are in fact `ndarray`s (we will discuss arithmetic operations on arrays below). The point is that the function `my_function` is only called *once*, instead of once per element. This makes initialization very efficient."
"Available data types include `int8`, `int16`, `int32`, `int64`, `uint8`/`16`/`32`/`64`, `float16`/`32`/`64` and `complex64`/`128`. Check out [the documentation](http://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html) for the full list.\n",
"An array's data is actually stored in memory as a flat (one dimensional) byte buffer. It is available *via* the `data` attribute (you will rarely need it, though)."
"print h + [10, 20, 30, 40, 50] # same as: h + [[[10, 20, 30, 40, 50]]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Second rule\n",
"*Arrays with a 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is repeated along that dimension.*"
"When trying to combine arrays with different `dtype`s, NumPy will *upcast* to a type capable of handling all possible values (regardless of what the *actual* values are)."
"Note that `int16` is required to represent all *possible* `int8` and `uint8` values (from -128 to 255), even though in this case a uint8 would have sufficed."
"These functions accept an optional argument `axis` which lets you ask for the operation to be performed on elements along the given axis. For example:"
"NumPy also provides fast elementwise functions called *universal functions*, or **ufunc**. They are vectorized wrappers of simple functions. For example `square` returns a new `ndarray` which is a copy of the original `ndarray` except that each element is squared:"
"There are also many binary ufuncs, that apply elementwise on two `ndarray`s. Broadcasting rules are applied if the arrays do not have the same shape:"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = np.array([1, -2, 3, 4])\n",
"b = np.array([2, 8, -1, 7])\n",
"print np.add(a, b) # equivalent to a + b"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print np.greater(a, b) # equivalent to a > b"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print np.maximum(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print np.copysign(a, b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Array indexing\n",
"### One-dimensional arrays\n",
"One-dimensional NumPy arrays can be accessed more or less like regular python arrays:"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = np.array([1, 5, 3, 19, 13, 7, 3])\n",
"print a[3]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print a[2:5]"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print a[2:-1]"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print a[:2]"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print a[2::2]"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print a[::-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Of course, you can modify elements:"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a[3]=999\n",
"print a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also modify an `ndarray` slice:"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a[2:5] = [997, 998, 999]\n",
"print a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Differences with regular python arrays\n",
"Contrary to regular python arrays, if you assign a single value to an `ndarray` slice, it is copied across the whole slice, thanks to broadcasting rules discussed above."
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a[2:5] = -1\n",
"print a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also, you cannot grow or shrink `ndarray`s this way:"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"try:\n",
" a[2:5] = [1,2,3,4,5,6] # too long\n",
"except ValueError, e:\n",
" print e"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You cannot delete elements either:"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"try:\n",
" del a[2:5]\n",
"except ValueError, e:\n",
" print e"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Last but not least, `ndarray` **slices are actually *views*** on the same data buffer. This means that if you create a slice and modify it, you are actually going to modify the original `ndarray` as well!"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a_slice = a[2:6]\n",
"a_slice[1] = 1000\n",
"print a # the original array was modified!"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a[3] = 2000\n",
"print a_slice # similarly, modifying the original array modifies the slice!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want a copy of the data, you need to use the `copy` method:"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"another_slice = a[2:6].copy()\n",
"another_slice[1] = 3000\n",
"print a # the original array is untouched"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a[3] = 4000\n",
"print another_slice # similary, modifying the original array does not affect the slice copy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multi-dimensional arrays\n",
"Multi-dimensional arrays can be accessed in a similar way by providing an index or slice for each axis, separated by commas:"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"b = np.arange(48).reshape(4, 12)\n",
"print b"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print b[1, 2] # row 1, col 2"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print b[1, :] # row 1, all columns"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print b[:, 1] # all rows, column 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Caution**: note the subtle difference between these two expressions: "
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"print b[1, :]\n",
"print b[1:2, :]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first expression returns row 1 as a 1D array of shape `(12,)`, while the second returns that same row as a 2D array of shape `(1, 12)`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fancy indexing\n",
"You may also specify a list of indices that you are interested in. This is referred to as *fancy indexing*."
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"print b[(0,2), 2:5] # rows 0 and 2, columns 2 to 4 (5-1)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print b[:, (-1, 2, -1)] # all rows, columns -1 (last), 2 and -1 (again, and in this order)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you provide multiple index arrays, you get a 1D `ndarray` containing the values of the elements at the specified coordinates."
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print b[(-1, 2, -1, 2), (5, 9, 1, 9)] # returns a 1D array with b[-1, 5], b[2, 9], b[-1, 1] and b[2, 9] (again)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Higher dimensions\n",
"Everything works just as well with higher dimensional arrays, but it's useful to look at a few examples:"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c = b.reshape(4,2,6)\n",
"print c"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print c[2, 1, 4] # matrix 2, row 1, col 4"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print c[2, :, 3] # matrix 2, all rows, col 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you omit coordinates for some axes, then all elements in these axes are returned:"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print c[2, 1] # Return matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ellipsis (`...`)\n",
"You may also write an ellipsis (`...`) to ask that all non-specified axes be entirely included."
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print c[2, ...] # matrix 2, all rows, all columns. This is equivalent to c[2, :, :]"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print c[2, 1, ...] # matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print c[2, ..., 3] # matrix 2, all rows, column 3. This is equivalent to c[2, :, 3]"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"print c[..., 3] # all matrices, all rows, column 3. This is equivalent to c[:, :, 3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Boolean indexing\n",
"You can also provide an `ndarray` of boolean values on one axis to specify the indices that you want to access."
"print b[rows_on, :] # Rows 0 and 2, all columns. Equivalent to b[(0, 2), :]"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cols_on = np.array([False, True, False] * 4)\n",
"print b[:, cols_on] # All rows, columns 1, 4, 7 and 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `np.ix_`\n",
"You cannot use boolean indexing this way on multiple axes, but you can work around this by using the `ix_` function:"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print b[np.ix_(rows_on, cols_on)]"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print np.ix_(rows_on, cols_on)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you use a boolean array that has the same shape as the `ndarray`, then you get in return a 1D array containing all the values that have `True` at their coordinate. This is generally used along with conditional operators:"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print b[b % 3 == 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterating\n",
"Iterating over `ndarray`s is very similar to iterating over regular python arrays. Note that iterating over multidimensional arrays is done with respect to the first axis."
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c = np.arange(24).reshape(2, 3, 4) # A 3D array (composed of two 3x4 matrices)\n",
"print c"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for m in c:\n",
" print \"Item:\"\n",
" print m"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in range(len(c)): # Note that len(c) == c.shape[0]\n",
" print \"Item:\"\n",
" print c[i]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to iterate on *all* elements in the `ndarray`, simply iterate over the `flat` attribute:"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in c.flat:\n",
" print \"Item:\", i"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stacking arrays\n",
"It is often useful to stack together different arrays. NumPy offers several functions to do just that. Let's start by creating a few arrays."
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"q1 = np.full((3,4), 1.0)\n",
"q2 = np.full((4,4), 2.0)\n",
"q3 = np.full((3,4), 3.0)\n",
"print \"q1\"\n",
"print q1\n",
"print \"q2\"\n",
"print q2\n",
"print \"q3\"\n",
"print q3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `vstack`\n",
"Now let's stack them vertically using `vstack`:"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"q4 = np.vstack((q1, q2, q3))\n",
"print q4\n",
"print \"shape =\", q4.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This was possible because q1, q2 and q3 all have the same shape (except for the vertical axis, but that's ok since we are stacking on that axis).\n",
"\n",
"### `hstack`\n",
"We can also stack arrays horizontally using `hstack`:"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"q5 = np.hstack((q1, q3))\n",
"print q5\n",
"print \"shape =\", q5.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is possible because q1 and q3 both have 3 rows. But since q2 has 4 rows, it cannot be stacked horizontally with q1 and q3:"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"try:\n",
" q5 = np.hstack((q1, q2, q3))\n",
"except ValueError, e:\n",
" print e"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `concatenate`\n",
"The `concatenate` function stacks arrays along any given existing axis."
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"q7 = np.concatenate((q1, q2, q3), axis=0) # Equivalent to vstack\n",
"print q7\n",
"print \"shape =\", q7.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you might guess, `hstack` is equivalent to calling `concatenate` with `axis=1`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `stack`\n",
"The `stack` function stacks arrays along a new axis. All arrays have to have the same shape."
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"q8 = np.stack((q1, q3))\n",
"print q8\n",
"print \"shape =\", q8.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Splitting arrays\n",
"Splitting is the opposite of stacking. For example, let's use the `vsplit` function to split a matrix vertically.\n",
"\n",
"First let's create a 6x4 matrix:"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r = np.arange(24).reshape(6,4)\n",
"print r"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's split it in three equal parts, vertically:"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r1, r2, r3 = np.vsplit(r, 3)\n",
"print \"r1\"\n",
"print r1\n",
"print \"r2\"\n",
"print r2\n",
"print \"r3\"\n",
"print r3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is also a `split` function which splits an array along any given axis. Calling `vsplit` is equivalent to calling `split` with `axis=0`. There is also an `hsplit` function, equivalent to calling `split` with `axis=1`:"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r4, r5 = np.hsplit(r, 2)\n",
"print \"r4\"\n",
"print r4\n",
"print \"r5\"\n",
"print r5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transposing arrays\n",
"The `transpose` method creates a new view on an `ndarray`'s data, with axes permuted in the given order.\n",
"\n",
"For example, let's create a 3D array:"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t = np.arange(24).reshape(4,2,3)\n",
"print t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's create an `ndarray` such that the axes `0, 1, 2` (depth, height, width) are re-ordered to `1, 2, 0` (depth→width, height→depth, width→height):"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t1 = t.transpose((1,2,0))\n",
"print t1\n",
"print \"shape =\", t1.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, `transpose` reverses the order of the dimensions:"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t2 = t.transpose() # equivalent to t.transpose((2, 1, 0))\n",
"print t2\n",
"print \"shape =\", t2.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NumPy provides a convenience function `swapaxes` to swap two axes. For example, let's create a new view of `t` with depth and height swapped:"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t3 = t.swapaxes(0,1) # equivalent to t.transpose((1, 0, 2))\n",
"print t3\n",
"print \"shape =\", t3.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Linear algebra\n",
"NumPy 2D arrays can be used to represent matrices efficiently in python. Let's go through some of the main matrix operations available.\n",
"\n",
"### Matrix transpose\n",
"The `T` attribute is equivalent to calling `transpose()` when the rank is ≥2:"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"m1 = np.arange(10).reshape(2,5)\n",
"print m1"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print m1.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `T` attribute has no effect on rank 0 (empty) or rank 1 arrays:"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"m2 = np.arange(5)\n",
"print m2"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"print m2.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can get the desired transposition by first reshaping the 1D array to a single-row matrix (2D):"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"m2r = m2.reshape(1,5)\n",
"print m2r"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print m2r.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matrix dot product\n",
"Let's create two matrices and execute a matrix dot product using the `dot` method."
"Many of the linear algebra functions are available in the `numpy.linalg` module, in particular the `inv` function to compute a square matrix's inverse:"
"Instead of executing operations on individual array items, one at a time, your code is much more efficient if you try to stick to array operations. This is called *vectorization*. This way, you can benefit from NumPy's many optimizations.\n",
"\n",
"For example, let's say we want to generate a 768x1024 array based on the formula $sin(xy/40)$. A **bad** option would be to do the math in python using nested loops:"
"Sure, this works, but it's terribly inefficient since the loops are taking place in pure python. Let's vectorize this algorithm. First, we will use NumPy's `meshgrid` function which generates coordinate matrices from coordinate vectors."
"As you can see, both `X` and `Y` are 768x1024 arrays, and all values in `X` correspond to the horizontal coordinate, while all values in `Y` correspond to the the vertical coordinate.\n",
"\n",
"Now we can simply compute the result using array operations:"
"Now you know all the fundamentals of NumPy, but there are many more options available. The best way to learn more is to experiment with NumPy, and go through the excellent [reference documentation](http://docs.scipy.org/doc/numpy/reference/index.html) to find more functions and features you may be interested in."