{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numpy\n", "\n", "Another one of the most popular and important\n", "packages available for Python is `numpy`\n", "(\"numerical python\"). Specifically, it provides\n", "numerical arrays and a variety of methods, functions,\n", "and operations for working with those arrays.\n", "While Python lists are very flexible, `numpy` arrays\n", "are fundamental when working with mathematical\n", "operations or larger numerical data sets. For those\n", "of you coming from MATLAB or R, numpy will provide\n", "an environment very similar to those mathematical\n", "and statistical programming languages. Before\n", "we dive in, as with other packages, the first step\n", "is to import it. For numpy, the standard is:\n", "```\n", "import numpy as np\n", "```\n", "Recall that `np` is then the short name we can use when\n", "accessing functions, values, etc. in numpy." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists vs Numpy Arrays\n", "\n", "The listing below compares python lists and `numpy` arrays.\n", "While lists are sometimes beneficial, the efficiency\n", "and convenience of `numpy` arrays makes them the best choice\n", "much of the time.\n", "\n", "* lists:\n", " * flexible\n", " * size can change (add/remove items)\n", " * can store different types\n", "* numpy arrays:\n", " * efficient - often far, far faster than lists\n", " * fixed size\n", " * fixed datatype\n", " * convenient operations/methods/functions\n", "\n", "You are likely wondering how `numpy` arrays and their \n", "functions/methods/operations can be that much faster\n", "than working with lists. Numpy operations and data structures\n", "are actually implemented underneath in a lower-level programming\n", "language that is much more efficient -- combining the\n", "benefits of python with the speed of lower level languages.\n", "\n", "### Efficiency Comparison\n", "Let's start by taking a look experimentally at this\n", "efficiency difference.\n", "Suppose we have two arrays/lists and we wish to multiply\n", "corresponding elements. We can do this with both\n", "arrays and lists. We'll use the `%%timeit` magic\n", "command to measure how much time it takes\n", "to perform this action easy way(recall: this is a special command to the\n", "jupyter notebook)\n", "\n", "#### List Way\n", "\n", "We'll start by using the random module to create 2 lists:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "n = 1000\n", "lst1 = random.choices([1,2,3,4,5],k=n)\n", "lst2 = random.choices([1,2,3,4,5],k=n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we'll go ahead and loop through, multiply corresponding elements,\n", "and append the result to a new list:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "res = []\n", "for i in range(n):\n", " res.append(lst1[i]*lst2[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at the same example as it would be implemented with numpy.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(1,6,n)\n", "arr2 = np.random.randint(1,6,n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "res = arr1*arr2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even with this small example, we can see that numpy takes\n", "approximately 1/100th of the time that lists do for this small\n", "problem -- illustrating just how much more efficient numpy\n", "is than simply using python lists." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Numpy Arrays\n", "\n", "There are wide variety of ways to create arrays:\n", "\n", "### From existing lists/tuples\n", "We can use the `np.array(lst)`function to create\n", "a numpy array from an existing list or tuple.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val_lst = [4,3,-1,8]\n", "val_arr = np.array(val_lst)\n", "\n", "print('val_list: ', val_lst)\n", "print('val_arr: ', val_arr)\n", "print(type(val_lst))\n", "print(type(val_arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also do the same thing without explicitly \n", "creating the separate variable for the list:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val_arr = np.array([4,3,-1,8])\n", "print(val_arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It also works with lists of lists to create a multidimensional array. Note, there's technically a thing called `matrix` in numpy, but you should **basically never** use it. It doesn't play nicely with all parts of numpy and often causes unexpected results. Instead, best practice is to create matrices as multidimensional arrays." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lst2d = [[1.1,4,8.3],[3,2.5,-1.8],[0,3.4,5]]\n", "mat = np.array(lst2d)\n", "\n", "print(mat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filled with zeros/ones\n", "\n", "Creating arrays from lists is common, but often one\n", "wants to create an array filled with some default value.\n", "Numpy has two very helpful functions to accomplish this:\n", "* `np.ones(shape)`\n", "* `np.zeros(shape)`\n", "\n", "With 1D arrays, `shape` is just a single number. For\n", "multidimensional arrays, `shape` is a tuple representing\n", "the size in each dimension.\n", "\n", "#### Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(np.zeros(5))\n", "print(np.ones(7))\n", "print('\\n5x5 filled with 0s')\n", "shape1 = (5,5)\n", "print(np.zeros(shape1))\n", "print('\\n4x3x2 filled with 1s')\n", "arr3d = np.ones((4,3,2))\n", "print(arr3d)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Identity Matrix\n", "There's also a fairly common special matrix that you may want to create, the *identity matrix*.\n", "This matrix has 1s on the diagonal and 0s everywhere else. To create this, we\n", "can use the `np.identity(n)` function, where we want an $n \\times n$ identity matrix." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matid = np.identity(3)\n", "print(matid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Zeros/Ones Resembling Existing Arrays\n", "In addition to creating arrays with specified sizes\n", "initialized to 0 or 1, it's also possible to create\n", "an array filled with 0s or 1s based on the form of\n", "an existing array with:\n", "* `np.zeros_like(arr)`\n", "* `np.ones_like(arr)`\n", "\n", "For instance:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "zarr3d = np.zeros_like(arr3d)\n", "print(zarr3d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Evenly spread values\n", "\n", "We saw with lists that we could use the result of the\n", "`range(start, stop, step)`\n", "function to create a lists with values that follow a\n", "certain pattern -- integers that can be represented\n", "by start, stop, and step. Numpy has it's own version of this\n", "function (designed to be more efficient for creating arrays):\n", "```\n", "np.arange(start,stop,step)\n", "```\n", "where `start` (inclusive), `stop` (exclusive), and `step` work exactly as they worked with `range`.\n", "\n", "This function creates an array directly. It should never\n", "be used with a non-integer step size (since computers\n", "must round numbers, it suffers from accuracy issues with\n", "non-integer step sizes.\n", "\n", "For non-integer step sizes, the better option is to use\n", "```\n", "np.linspace(start,stop,num=50)\n", "```\n", "This function doesn't worry about the step directly -- it instead\n", "identifies how they should be spaced by the length of the interval\n", "and the number of points specified.\n", "\n", "### Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.arange(1,10,2)\n", "print(arr)\n", "\n", "arr2 = np.linspace(1,10,20)\n", "print(arr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Types\n", "As we have seen, Python is dynamically typed: types\n", "are changed automatically as needed. Lists can hold\n", "various types and the types in a list can change.\n", "Numpy arrays, however, are homogeneous and statically typed, meaning:\n", "1. the data type stored in the array cannot change\n", "2. all of the elements in the array have the same datatype\n", "\n", "This means that all of the arrays created above had an implicit\n", "datatype chosen automatically by numpy. Let's start by looking\n", "at a couple of those data types. To look at the datatype,\n", "we access the `dtype` attribute of the array, which just indicates\n", "that it is a fundamental property of the array." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(arr.dtype)\n", "print(arr2.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also explicitly indicate what datatype we want\n", "when calling most of the array-creation functions, such\n", "as `np.zeros` and `np.ones`, by adding in the `dtype` argument." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val_lst = [1,4,6,8]\n", "val_arr = np.array(val_lst)\n", "val_arr2 = np.array(val_lst, dtype=float)\n", "print(val_arr)\n", "print(val_arr2)\n", "print(val_arr.dtype)\n", "print(val_arr2.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The datatype of an array resulting from some operation will\n", "automatically adjust to whatever makes the most sense.\n", "\n", "While in many cases the operation will have the desired behavior, there are times where it is necessary to change the datatype of an existing array. To do so, we can simply call\n", "the `astype()` method of the array. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.arange(8.0)\n", "print(arr.dtype)\n", "conv = arr.astype(int)\n", "print(conv.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing Array Elements\n", "\n", "Accessing array elements is similar to accessing items inside of a list:\n", "* indexed starting at 0 at the start\n", "* negative indices from the end\n", "* access with the `[]` notation\n", "\n", "So far, all of these should seem very similar to what we've already seen with lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val_arr = np.array([1,4,-13,-5,8,-9,24])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(val_arr)\n", "print(val_arr[0])\n", "print(val_arr[3])\n", "print(val_arr[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing\n", "\n", "We've already seen the fundamental idea of slicing a few\n", "times with lists, strings, and tuples. We also\n", "have slicing available for numpy arrays. As with \n", "lists, these take the general form:\n", "```\n", "arr[start:stop:step]\n", "```\n", "where any of the three can optionally be empty and negative\n", "values can be used to indicate negative indices or direction.\n", "You can also leave of the `:step` part entirely if not needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val_arr = np.array([1,4,-13,-5,8,-9,24])\n", "print(val_arr[1:-1:2])\n", "print(val_arr[1:-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing Multi-dimensional Arrays\n", "\n", "To access multi-dimensional arrays, we simply specify\n", "the indices in a single `[]` separated by commas, e.g.,\n", "```\n", "arr[i,j]\n", "```\n", "For instance, consider the 2d array:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mat = np.array([[5,8,1,7],[3,2,4,2.5],[9,0,1,6]])\n", "print(mat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To access the value 3 in the 2nd row, 1st column, we would do:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(mat[1,0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: this is slightly different than how we accessed lists of\n", "lists where we used a different set of `[]` for each level\n", "of list. It's a common mistake to mix these up, so watch out\n", "for it when you get error messages.\n", "\n", "One of the powerful things with python is the ability to take\n", "slices of multi-dimensional arrays. We can do this by just\n", "replacing an index (like the 1 or 0 in `mat[1,0]` with the slicing\n", "notation). So, suppose I want to extract the vector `[2,4]` from\n", "within this array. That is located in the row with index 1, columns\n", "with index 1 and 2. Let's look at how to slice to extract that portion:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(mat[1,1:3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To include everything in a particular dimension, we can use the `:`\n", "with no start or stop, which says give me all of this dimension.\n", "For instance, suppose we wish to access just\n", "the 2nd column of our 2d array:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(mat[:,1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you have had linear algebra, you probably noticed\n", "that the result is a 1d array. Numpy doesn't really\n", "care about row vs column vector. The operations typically\n", "figure out the correct way to behave based on the arguments\n", "so the standard is to use 1d arrays for both column and row\n", "vectors. You can force it to be one or the other by making it\n", "a 2d array where one axis has size 1, but this is rarely the\n", "correct choice (and often will make things behave unexpectedly).\n", "\n", "This was shown here for a 2d array, but this same logic applies for\n", "higher dimensional arrays -- you simply list the index\n", "or slice for each dimension separated by commas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Attributes\n", "Numpy arrays are technically *objects* that\n", "have both *methods*, which we've seen before, and *attributes* associated\n", "with them. Attributes are just values associated\n", "with that particular array object.\n", "\n", "Numpy arrays have many attributes, but the most important\n", "ones are:\n", "* `dtype` - the data type of the array\n", "* `shape` - the shape of the array as a tuple\n", "* `ndim` - the number of dimensions of the array\n", "* `size` - the total number of elements in the array\n", " (do not mix this up with `shape`\n", " \n", "Note that since attributes are not methods, we don't \"call\"\n", "them. This means that when we access an attribute, we do so\n", "with\n", "```\n", "arrname.attributename\n", "```\n", "Notice the lack of `()` at the end, which are used to call a\n", "function.\n", "\n", "### Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mat = np.array([[5,8,1,7],[3,2,4,2.5],[9,0,1,6]])\n", "print(mat)\n", "print(mat.dtype)\n", "print(mat.shape)\n", "print(mat.ndim)\n", "print(mat.size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators, Functions, Methods\n", "\n", "One of the nicest things about numpy is the extremely\n", "large number of methods, functions, and built-in operations\n", "that are available for arrays. There are far more than we\n", "can talk about here. In general,\n", "if you want to try to do something with a numpy array,\n", "there's a good chance there is a function or an easy way to\n", "accomplish it. The best way is to look at the\n", "[documentation](https://numpy.org/doc/1.19/reference/arrays.ndarray.html)\n", "or Google what you'd like to accomplish.\n", "\n", "The most important thing about numpy arrays is that\n", "the vast majority of these functions/methods/operations\n", "work on the entire array at once. Most loops can be avoided\n", "entirely with proper use of numpy array operations.\n", "As illustrated earlier, these operations are also far, far\n", "faster than the loop counterparts, so you should avoid\n", "using loops as much as possible.\n", "\n", "### Elementwise Operations\n", "Many elementwise operations happen automatically\n", "with arrays when using the standard operators. These\n", "include:\n", "* addition: `+` operator\n", "* subtraction: `-` operator\n", "* multiplication: `*` operator\n", "* division: `/` operator\n", "* exponentiation: `**` operator\n", "* comparisons: `==`, `>`, `>=`, `<`, `<=` operators\n", "These work with both 1d and higher dimensional arrays, simply\n", "performing the operation with corresponding elements.\n", "\n", "#### Examples 1D" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(1,5)\n", "arr2 = np.array([4,2,1,4])\n", "print(arr1)\n", "print(arr2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "add = arr1+arr2\n", "mul = arr1*arr2\n", "div = arr1/arr2\n", "sub = arr1-arr2\n", "squared = arr1**arr2\n", "print('addition:')\n", "print(add)\n", "print('multiplication:')\n", "print(mul)\n", "print('division:')\n", "print(div)\n", "print('subtraction:')\n", "print(sub)\n", "print('power:')\n", "print(squared)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(arr1 == arr2)\n", "print(arr1 < arr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the comparison operators return an array indicating\n", "whether each of the individual elementwise comparisons\n", "evaluated to `True` or `False`.\n", "\n", "Above we performed these operations with two 1d arrays of the same\n", "shape. These could have just as easily been 2d arrays,\n", "3d arrays, etc. Numpy can also perform operations between arrays\n", "and individual values, elementwise performing the operation\n", "with each element of the array." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mat = np.array([[5,8,1],[3,2,4],[9,0,1]])\n", "print(mat/2) # divides each element by 2\n", "print(mat+1) # adds 1 to each element\n", "print(mat**2) # squares each element" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Checking Containment\n", "\n", "Like with lists we can use `in` to see if a value is in\n", "an array with\n", "```\n", "val in arrname\n", "```\n", "Similarly to lists, we would often embed this as the condition\n", "in an `if` statement to change what code is executed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(9 in mat)\n", "print(-9 in mat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions\n", "When working with numpy arrays, it's important to\n", "use numpy functions (rather than math or built-in\n", "functions). The numpy functions are automatically\n", "designed to work on the array as a whole, performing\n", "the function on each element automatically, rather\n", "than having to loop through the entire array.\n", "\n", "#### Mathematical Functions\n", "Pretty much all of the mathematical functions\n", "exist in numpy as well including:\n", "\n", "* $e^x$: `exp(arr)`\n", "* absolute value: `abs(arr)`\n", "* logarithms: `log(arr)`, `log2(arr)`, `log10(arr)` \n", "* trigonometric functions: `sin(arr)`, `cos(arr)`, `tan(arr)`, etc.\n", "* rounding: `round(arr, ndecimals)`, `floor(arr)`, `ceil(arr)`\n", "\n", "Because these are all functions in numpy, we must preface\n", "them with `np.` when calling them.\n", "\n", "##### Mathematical Function Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.arange(1,4)\n", "arr2 = np.linspace(-2,2,10)\n", "print(np.exp(arr))\n", "print(np.log2(arr))\n", "print(np.abs(arr2))\n", "print(np.round(arr2,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Methods\n", "\n", "In addition to the functions in numpy, arrays have a variety of\n", "useful methods. You can check out the full list of methods in the\n", "[numpy array documentation](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html).\n", "Some of the most common ones are:\n", "* `arrname.reshape(newshape)` - returns a \"view\" of the data with a new shape (does not actually\n", " copy the data so be careful modifying values).\n", "* `arrname.sort()` - sorts the array in place. If you want a copy use `np.sort(arr)` instead\n", "* `arrname.argsort()` - returns the ordering of indices that would sort the array\n", "* `arrname.cumsum()` - computes cumulative sums\n", "* `arrname.cumprod()` - computes cumulative products\n", "* `arrname.flatten()` - returns copy of array flattened to one dimension\n", "* `arrname.astype(dtype)` - returns copy of the array with specified type (shown previously)\n", "* `arrname.copy()` - returns a copy of the array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Examples - Changing Shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.arange(12)\n", "mat = arr.reshape((4,3))\n", "print(mat)\n", "mat[0,0] = 100\n", "print(mat)\n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that when we changed the element at 0,0 in `mat`\n", "it changed the element in `arr` as well since `mat` was\n", "not actually a copy of `arr`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mat = np.array([[2,1],[3,4]])\n", "print(mat)\n", "arr = mat.flatten()\n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Examples - Sorting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.array([4,1,2,8])\n", "print(arr.argsort())\n", "arr2 = arr.copy()\n", "arr2.sort()\n", "print(arr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Example - Cumulative Sum" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(arr.cumsum())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many of these (`sort()`, `argsort()`, `cumsum()`) have the ability to specify the axis on\n", "which you want to perform the operation. This is helpful for doing things like sorting each\n", "row or column independently. For example, with `axis=1`, sort will sort each column separately." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mat = np.array([[3,6,1],[2,-1,9],[8,4,3]])\n", "print('unsorted')\n", "print(mat)\n", "mat.sort(axis=0)\n", "print('sorted down each column')\n", "print(mat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reduction Operations\n", "\n", "The previous operations all returned arrays of the same shape as the input. However, there are also methods for many *reduction* operations which go from a higher dimension solution to a lower dimensional one (reducing the dimension). These can be used to compute things such as the maximum entry in an array, the minimum entry in an array, or the sum of all entries in the array, median of \n", "items in an array, etc.\n", "\n", "There are many reduction operations, some of which are methods and others which are\n", "functions. \n", "\n", "### Methods for Reduction\n", "* `arrname.min()`\n", "* `arrname.max()`\n", "* `arrname.sum()`\n", "* `arrname.mean()`\n", "* `arrname.std()` - standard deviation\n", "* `arrname.argmin()` - the index of the minimum value\n", "* `arrname.argmax()` - the index of the maximum value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mat = np.array([5, 7, -8, 1, 3, -4, 12, 0, 1, 0, 2, 5]).reshape(4,3)\n", "print(mat)\n", "print(mat.min())\n", "print(mat.max())\n", "print(mat.sum())\n", "print(mat.mean())\n", "print(mat.flatten().argmin())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions for Reduction\n", "\n", "Those were all methods, meaning we called them by\n", "```\n", "arrname.methodname()\n", "```\n", "There are additional reduction operations that are\n", "functions in numpy rather than methods for the particular\n", "array. Some of the most frequently used ones are:\n", "* `count_nonzero(arr)` - counts number of nonzero\n", " elements. If argument evaluates to a boolean array,\n", " counts the number of `True`.\n", "* `any(arr)` - returns `True` if any element of `arr` is `True` (also available as an array method)\n", "* `all(arr)` - returns `True` if all elements of `arr` are `True` (also available as an array method)\n", "* `median(arr)` - returns median of all elements in `arr`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(np.count_nonzero(mat))\n", "print(np.median(mat))\n", "tfarr = np.array([True, False, False])\n", "print(np.any(tfarr))\n", "print(np.all(tfarr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's very common to combine `any()`, `all()`, and `count_nonzero()` with a logical comparison.\n", "For instance, I could use this to check if my array has at least one element greater than\n", "some number, or count the number of elements greater than some number:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.array([5,2,1,8,4,9])\n", "print(np.any(arr > 6))\n", "print(np.count_nonzero(arr > 6))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reducing over Certain Axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All of these reduction operations are actually\n", "far more powerful than that simple example. Each reduction operation\n", "takes an `axis` argument that indicates which axis we want to\n", "reduce over. The dimension represented by this axis will be removed during\n", "the course of the reduction operation. \n", "\n", "### Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(mat.min(axis=1)) # one min for each row\n", "print(mat.min(axis=0)) # one min for each col" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looping Through Arrays\n", "While it's best to avoid using loops as much as possible with numpy arrays,\n", "by using elementwise operations, reduction operations, and careful\n", "use of the `axis` argument,\n", "it is possible to loop through arrays. Typically when looping through numpy\n", "arrays we loop through the indices with something like:\n", "\n", "```\n", "for i in range(arr.shape[0]):\n", " # do something\n", "```\n", "where we can access whatever dimension in `shape` that is appropriate for\n", "our loop, e.g. you may want `shape[1]` instead of `shape[0]`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constants\n", "Much like the math module had many mathematical constants,\n", "numpy also contains mathematical constants. Multiple of these\n", "are the same as the ones in the math module, but are\n", "included as part of numpy so you don't need to `import math`\n", "just for these constants. \n", "\n", "The primary constants in numpy are:\n", "* np.e\n", "* np.pi\n", "* np.inf\n", "* np.nan\n", "\n", "The last 2 (`inf` and `nan`) are unique to numpy and represente\n", "infinity and not a number, respectively." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(np.inf*3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Files\n", "We have already seen a couple options for reading\n", "and writing to files. Typically these end up with either\n", "lists of lists or simply iterating through each row and breaking\n", "that row down into a list. Sometimes, however, if we have numerical data\n", "we may know that we want it in a numpy array to perform some operations.\n", "While we could go through the whole process of reading it in and creating\n", "a list or filling a numpy array, this is tedious and time consuming.\n", "\n", "If the data is numerical and in some well-formatted, delimited file (such\n", "as tab separated or comma separated), then we can read the data directly into\n", "a numpy array using numpy file input/output functions. These\n", "only work if the data that needs to go into the array are numerical values (as\n", "numpy is not designed to work with strings).\n", "\n", "There are two primary functions for reading in files with numpy:\n", "* `np.loadtxt(fname, dtype='float', delimiter=None, skiprows=0, usecols=None)`\n", " this shows the default values for many of the keyword arguments. \n", " * `delimiter`: what to separate columns with, default is whitespace\n", " * `skiprows`: how many rows to skip, usually for a header row, default is not\n", " to skip any rows\n", " * `usecols`: which columns to use, default is to use all columns. To specify more\n", " than one column, give tuple of which column indices (0-based indexing) that you\n", " want to keep\n", "* `np.genfromtxt(fname, dtype='float', delimiter=None, skiprows=0, usecols=None, missing_values=None, filling_values=None)`\n", " * very similar to `loadtxt`, but better equipped to handle missing values\n", " * `missing_values`: what represents a missing value (depends on your dataset,\n", " sometimes it's -1, sometimes it's 9999, sometimes it's '?')\n", " * `filling_values`: what value to insert instead of any missing values\n", "\n", "There's also one common function for writing out a numpy array to a file:\n", "* `np.savetxt(fname, arr, fmt='%.18e', delimiter=' ', header='')`\n", " * `fname`: the filename in which to save the array\n", " * `arr`: the array (1d or multidimensional) to save\n", " * `fmt`: format specifier for how to output values\n", " * `delimiter`: what to separate columns with when outputting (default is a spa)\n", "\n", "We'll illustrate one of these using the\n", "[Pima Indians Diabetes Dataset](https://www.kaggle.com/uciml/pima-indians-diabetes-database)\n", "which can be found in `'diabetes.csv'`. This dataset is all numerical,\n", "other than the first row which is a header row. Missing data does not appear\n", "to be a problem, so instead we'll use `loadtxt`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = np.loadtxt('data/diabetes.csv', skiprows=1, delimiter=',')\n", "print(data.shape)\n", "print(data[:10,:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linear Algebra\n", "\n", "One of the areas where numpy really shines is linear algebra.\n", "Numpy has a *subpackage* called `linalg`, that is automatically\n", "loaded when numpy is imported, which contains a large number\n", "of extremely efficient linear algebra operations.\n", "\n", "Big picture, the array method `arr1.dot(arr2)` or alternatively\n", "the numpy function `np.dot(arr1, arr2)` is an extremely helpful\n", "function. Depending on whether the arrays are 1D or 2D, determines\n", "what it performs:\n", "* if both `arr1` and `arr2` are 1D, then it performs an inner product of the vectors (remember -- we don't care about and typically\n", " shouldn't explicitly use column and row vectors in numpy, instead\n", " it's best to use 1D arrays)\n", "* if `arr1` is 2D and `arr1` is 1D, then it performs matrix-vector\n", " product\n", "* if `arr1` and `arr2` are both 2D arrays, then it performs\n", " matrix-matrix multiplication.\n", "\n", "#### Example - Inner Product" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(4)\n", "print('----- arr1 -----')\n", "print(arr1)\n", "arr2 = np.arange(-2,2)\n", "print('----- arr2 -----')\n", "print(arr2)\n", "\n", "print('----- arr1.dot(arr2) -----')\n", "result = arr1.dot(arr2)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Example - Matrix-vector product" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = np.arange(8).reshape(4,2)\n", "print('----- A -----')\n", "print(A)\n", "x = np.array([1,2])\n", "print('----- x -----')\n", "print(x)\n", "\n", "print('----- A.dot(x) -----')\n", "result = A.dot(x)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Example - Matrix-matrix product" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = np.array([[3,4,2],[-1,2,2]])\n", "print('----- A -----')\n", "print(A)\n", "B = np.array([[2,1],[1,1],[3,-1]])\n", "print('----- B -----')\n", "print(B)\n", "\n", "print('----- A.dot(B) -----')\n", "result = A.dot(B)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since matrix-matrix multiplication is such a common task, it has it's own\n", "operator, the `@` symbol." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(A@B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Beyond just the vector-vector, matrix-vector, and matrix-matrix operations, `numpy.linalg` has quite a few other functions for\n", "tasks such as:\n", "* outer products\n", "* computing decompositions\n", "* computing eigenvalues\n", "* norms, determinant, trace, rank, etc.\n", "* solving linear systems and inverting matrices\n", "\n", "If you are\n", "familiar with linear algebra, I strongly recommend glancing\n", "through the full list of available functions in the \n", "[documentation for numpy.linalg](https://numpy.org/doc/stable/reference/routines.linalg.html#matrix-and-vector-products)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Broadcasting\n", "\n", "The final major aspect of numpy arrays, and in particular understanding how they behave,\n", "is *broadcasting*, which describes how numpy arrays with different sizes are handled during operations.\n", "Rather than having elementwise operations require both arrays be the same size, instead numpy will automatically extend the smaller array conceptually to be compatible. For instance, we've already seen one example where we can add a scalar to a vector and it adds the scalar to each element of the vector. Essentially, you can think of it as stretching the scalar value to match the shape of the vector, without ever having to explicitly form the array that comes from stretching the scalar value.\n", "\n", "Numpy's broadcasting is significantly more powerful than just performing an operation with a scalar and a vector. The same idea applies to multi-dimensional arrays. Numpy works from the end dimension to the start dimension checking if the dimensions are compatible for each of the arrays involved in the operation. Dimensions are compatible if either:\n", "* they are equal\n", "* one of them is 1\n", "\n", "Let's take a brief look at an example:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mat = np.arange(12).reshape((4,3))\n", "print('----- mat -----')\n", "print(mat)\n", "vec = np.array([3,0,1])\n", "print('----- vec -----')\n", "print(vec)\n", "\n", "print('----- mat+vec -----')\n", "print(mat+vec)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can take quite a while to wrap your head around, but it also has the ability to be extremely\n", "powerful. For now, just know that the idea exists and deal with each instance as it comes up." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }