{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(lecture02:numpy)=\n", "# Numerical Python\n", "\n", "The ability to do basic things with numerical arrays (vectors and matrices) is extremely important in data science applications.\n", "We do numerical algebra in Python using the [Numpy](https://numpy.org/).\n", "Numpy is a **huge** Python library and it has a lot of details.\n", "We are going to cover the very basics here.\n", "When there is something specific that you want to do, the best way to figure out how to do it is to Google it.\n", "\n", "First, let's import ``numpy``.\n", "We typically, do it by creating a shortcut called ``np``:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this way you can access whatever is in ``numpy`` by going throuh ``np`` which is less characters.\n", "Let's create a 1D numpy array from a list:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1.2 2. 3. -1. 2. ]\n" ] } ], "source": [ "a = np.array([1.2, 2.0, 3.0, -1.0, 2.0])\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This behaves just like a vector in Matlab.\n", "You can multiply with a number:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2.4, 4. , 6. , -2. , 4. ])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.0 * a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3.6, 6. , 9. , -3. , 6. ])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add another vector (of the same size):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.2, 4. , 6. , 3. , 7. ])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([1, 2, 3, 4, 5])\n", "c = a + b\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can raise all elements to a power:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.728, 8. , 27. , -1. , 8. ])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ** 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you can apply a standard function to all elements:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3.32011692, 7.3890561 , 20.08553692, 0.36787944, 7.3890561 ])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp(a)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.36235775, -0.41614684, -0.9899925 , 0.54030231, -0.41614684])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.cos(a)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.93203909, 0.90929743, 0.14112001, -0.84147098, 0.90929743])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin(a)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2.57215162, -2.18503986, -0.14254654, -1.55740772, -2.18503986])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.tan(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the functions you can use are in ``np``.\n", "Here is how you can get how many elements you have in the array:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5,)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also, to see if this is a 1D array (higher dimensional arrays, 2D, 3D, etc. are also possible), you can use this:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ndim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You access the elements of the array just like the elements of a list:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3., -1.])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:4]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1.2, 3. , 2. ])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::2]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2. , -1. , 3. , 2. , 1.2])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can find the minimum or the maximum of the array like this:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.min()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, notice that ``a`` is an object of a class.\n", "The class is called ``np.ndarray``:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and ``min()`` and ``max()`` are functions of the ``np.ndarray`` class.\n", "Another function of the class is the dot product:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.2" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dot(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also a submodule called ``numpy.linalg`` which has some linear algebra functions you can apply to arrays.\n", "For 1D arrays (aka vectors) the vector norm is a useful one:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.409081537009721" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.norm(a)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function norm in module numpy.linalg:\n", "\n", "norm(x, ord=None, axis=None, keepdims=False)\n", " Matrix or vector norm.\n", " \n", " This function is able to return one of eight different matrix norms,\n", " or one of an infinite number of vector norms (described below), depending\n", " on the value of the ``ord`` parameter.\n", " \n", " Parameters\n", " ----------\n", " x : array_like\n", " Input array. If `axis` is None, `x` must be 1-D or 2-D, unless `ord`\n", " is None. If both `axis` and `ord` are None, the 2-norm of\n", " ``x.ravel`` will be returned.\n", " ord : {non-zero int, inf, -inf, 'fro', 'nuc'}, optional\n", " Order of the norm (see table under ``Notes``). inf means numpy's\n", " `inf` object. The default is None.\n", " axis : {None, int, 2-tuple of ints}, optional.\n", " If `axis` is an integer, it specifies the axis of `x` along which to\n", " compute the vector norms. If `axis` is a 2-tuple, it specifies the\n", " axes that hold 2-D matrices, and the matrix norms of these matrices\n", " are computed. If `axis` is None then either a vector norm (when `x`\n", " is 1-D) or a matrix norm (when `x` is 2-D) is returned. The default\n", " is None.\n", " \n", " .. versionadded:: 1.8.0\n", " \n", " keepdims : bool, optional\n", " If this is set to True, the axes which are normed over are left in the\n", " result as dimensions with size one. With this option the result will\n", " broadcast correctly against the original `x`.\n", " \n", " .. versionadded:: 1.10.0\n", " \n", " Returns\n", " -------\n", " n : float or ndarray\n", " Norm of the matrix or vector(s).\n", " \n", " See Also\n", " --------\n", " scipy.linalg.norm : Similar function in SciPy.\n", " \n", " Notes\n", " -----\n", " For values of ``ord < 1``, the result is, strictly speaking, not a\n", " mathematical 'norm', but it may still be useful for various numerical\n", " purposes.\n", " \n", " The following norms can be calculated:\n", " \n", " ===== ============================ ==========================\n", " ord norm for matrices norm for vectors\n", " ===== ============================ ==========================\n", " None Frobenius norm 2-norm\n", " 'fro' Frobenius norm --\n", " 'nuc' nuclear norm --\n", " inf max(sum(abs(x), axis=1)) max(abs(x))\n", " -inf min(sum(abs(x), axis=1)) min(abs(x))\n", " 0 -- sum(x != 0)\n", " 1 max(sum(abs(x), axis=0)) as below\n", " -1 min(sum(abs(x), axis=0)) as below\n", " 2 2-norm (largest sing. value) as below\n", " -2 smallest singular value as below\n", " other -- sum(abs(x)**ord)**(1./ord)\n", " ===== ============================ ==========================\n", " \n", " The Frobenius norm is given by [1]_:\n", " \n", " :math:`||A||_F = [\\sum_{i,j} abs(a_{i,j})^2]^{1/2}`\n", " \n", " The nuclear norm is the sum of the singular values.\n", " \n", " Both the Frobenius and nuclear norm orders are only defined for\n", " matrices and raise a ValueError when ``x.ndim != 2``.\n", " \n", " References\n", " ----------\n", " .. [1] G. H. Golub and C. F. Van Loan, *Matrix Computations*,\n", " Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15\n", " \n", " Examples\n", " --------\n", " >>> from numpy import linalg as LA\n", " >>> a = np.arange(9) - 4\n", " >>> a\n", " array([-4, -3, -2, ..., 2, 3, 4])\n", " >>> b = a.reshape((3, 3))\n", " >>> b\n", " array([[-4, -3, -2],\n", " [-1, 0, 1],\n", " [ 2, 3, 4]])\n", " \n", " >>> LA.norm(a)\n", " 7.745966692414834\n", " >>> LA.norm(b)\n", " 7.745966692414834\n", " >>> LA.norm(b, 'fro')\n", " 7.745966692414834\n", " >>> LA.norm(a, np.inf)\n", " 4.0\n", " >>> LA.norm(b, np.inf)\n", " 9.0\n", " >>> LA.norm(a, -np.inf)\n", " 0.0\n", " >>> LA.norm(b, -np.inf)\n", " 2.0\n", " \n", " >>> LA.norm(a, 1)\n", " 20.0\n", " >>> LA.norm(b, 1)\n", " 7.0\n", " >>> LA.norm(a, -1)\n", " -4.6566128774142013e-010\n", " >>> LA.norm(b, -1)\n", " 6.0\n", " >>> LA.norm(a, 2)\n", " 7.745966692414834\n", " >>> LA.norm(b, 2)\n", " 7.3484692283495345\n", " \n", " >>> LA.norm(a, -2)\n", " 0.0\n", " >>> LA.norm(b, -2)\n", " 1.8570331885190563e-016 # may vary\n", " >>> LA.norm(a, 3)\n", " 5.8480354764257312 # may vary\n", " >>> LA.norm(a, -3)\n", " 0.0\n", " \n", " Using the `axis` argument to compute vector norms:\n", " \n", " >>> c = np.array([[ 1, 2, 3],\n", " ... [-1, 1, 4]])\n", " >>> LA.norm(c, axis=0)\n", " array([ 1.41421356, 2.23606798, 5. ])\n", " >>> LA.norm(c, axis=1)\n", " array([ 3.74165739, 4.24264069])\n", " >>> LA.norm(c, ord=1, axis=1)\n", " array([ 6., 6.])\n", " \n", " Using the `axis` argument to compute matrix norms:\n", " \n", " >>> m = np.arange(8).reshape(2,2,2)\n", " >>> LA.norm(m, axis=(1,2))\n", " array([ 3.74165739, 11.22497216])\n", " >>> LA.norm(m[0, :, :]), LA.norm(m[1, :, :])\n", " (3.7416573867739413, 11.224972160321824)\n", "\n" ] } ], "source": [ "help(np.linalg.norm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Questions\n", "Consider the two vectors:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r1 = np.array([1.0, -2.0, 3.0])\n", "r2 = np.array([2.0, 2.0, -3.0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use numpy functionality to:\n", "+ Find a unit vector in the direction of ``r1``:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ Find the angle between ``r1`` and ``r2`` in radians:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ The projection of ``r2`` on ``r1``:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ Use [numpy.cross](https://numpy.org/doc/stable/reference/generated/numpy.cross.html) to find the cross product between ``r2`` and ``r1``. Then, verify numerically (using numpy functionality) that $\\vec{r}_1\\cdot\\left(\\vec{r}_1\\times\\vec{r}_2\\right) = 0$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }