{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", "\n", "___\n", "
Content Copyright by Pierian Data
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Math and Random Modules\n", "\n", "Python comes with a built in math module and random module. In this lecture we will give a brief tour of their capabilities. Usually you can simply look up the function call you are looking for in the online documentation.\n", "\n", "* [Math Module](https://docs.python.org/3/library/math.html)\n", "\n", "* [Random Module](https://docs.python.org/3/library/random.html)\n", "\n", "We won't go through every function available in these modules since there are so many, but we will show some useful ones." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Useful Math Functions" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in module math:\n", "\n", "NAME\n", " math\n", "\n", "DESCRIPTION\n", " This module is always available. It provides access to the\n", " mathematical functions defined by the C standard.\n", "\n", "FUNCTIONS\n", " acos(...)\n", " acos(x)\n", " \n", " Return the arc cosine (measured in radians) of x.\n", " \n", " acosh(...)\n", " acosh(x)\n", " \n", " Return the inverse hyperbolic cosine of x.\n", " \n", " asin(...)\n", " asin(x)\n", " \n", " Return the arc sine (measured in radians) of x.\n", " \n", " asinh(...)\n", " asinh(x)\n", " \n", " Return the inverse hyperbolic sine of x.\n", " \n", " atan(...)\n", " atan(x)\n", " \n", " Return the arc tangent (measured in radians) of x.\n", " \n", " atan2(...)\n", " atan2(y, x)\n", " \n", " Return the arc tangent (measured in radians) of y/x.\n", " Unlike atan(y/x), the signs of both x and y are considered.\n", " \n", " atanh(...)\n", " atanh(x)\n", " \n", " Return the inverse hyperbolic tangent of x.\n", " \n", " ceil(...)\n", " ceil(x)\n", " \n", " Return the ceiling of x as an Integral.\n", " This is the smallest integer >= x.\n", " \n", " copysign(...)\n", " copysign(x, y)\n", " \n", " Return a float with the magnitude (absolute value) of x but the sign \n", " of y. On platforms that support signed zeros, copysign(1.0, -0.0) \n", " returns -1.0.\n", " \n", " cos(...)\n", " cos(x)\n", " \n", " Return the cosine of x (measured in radians).\n", " \n", " cosh(...)\n", " cosh(x)\n", " \n", " Return the hyperbolic cosine of x.\n", " \n", " degrees(...)\n", " degrees(x)\n", " \n", " Convert angle x from radians to degrees.\n", " \n", " erf(...)\n", " erf(x)\n", " \n", " Error function at x.\n", " \n", " erfc(...)\n", " erfc(x)\n", " \n", " Complementary error function at x.\n", " \n", " exp(...)\n", " exp(x)\n", " \n", " Return e raised to the power of x.\n", " \n", " expm1(...)\n", " expm1(x)\n", " \n", " Return exp(x)-1.\n", " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", " \n", " fabs(...)\n", " fabs(x)\n", " \n", " Return the absolute value of the float x.\n", " \n", " factorial(...)\n", " factorial(x) -> Integral\n", " \n", " Find x!. Raise a ValueError if x is negative or non-integral.\n", " \n", " floor(...)\n", " floor(x)\n", " \n", " Return the floor of x as an Integral.\n", " This is the largest integer <= x.\n", " \n", " fmod(...)\n", " fmod(x, y)\n", " \n", " Return fmod(x, y), according to platform C. x % y may differ.\n", " \n", " frexp(...)\n", " frexp(x)\n", " \n", " Return the mantissa and exponent of x, as pair (m, e).\n", " m is a float and e is an int, such that x = m * 2.**e.\n", " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", " \n", " fsum(...)\n", " fsum(iterable)\n", " \n", " Return an accurate floating point sum of values in the iterable.\n", " Assumes IEEE-754 floating point arithmetic.\n", " \n", " gamma(...)\n", " gamma(x)\n", " \n", " Gamma function at x.\n", " \n", " gcd(...)\n", " gcd(x, y) -> int\n", " greatest common divisor of x and y\n", " \n", " hypot(...)\n", " hypot(x, y)\n", " \n", " Return the Euclidean distance, sqrt(x*x + y*y).\n", " \n", " isclose(...)\n", " isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) -> bool\n", " \n", " Determine whether two floating point numbers are close in value.\n", " \n", " rel_tol\n", " maximum difference for being considered \"close\", relative to the\n", " magnitude of the input values\n", " abs_tol\n", " maximum difference for being considered \"close\", regardless of the\n", " magnitude of the input values\n", " \n", " Return True if a is close in value to b, and False otherwise.\n", " \n", " For the values to be considered close, the difference between them\n", " must be smaller than at least one of the tolerances.\n", " \n", " -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n", " is, NaN is not close to anything, even itself. inf and -inf are\n", " only close to themselves.\n", " \n", " isfinite(...)\n", " isfinite(x) -> bool\n", " \n", " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", " \n", " isinf(...)\n", " isinf(x) -> bool\n", " \n", " Return True if x is a positive or negative infinity, and False otherwise.\n", " \n", " isnan(...)\n", " isnan(x) -> bool\n", " \n", " Return True if x is a NaN (not a number), and False otherwise.\n", " \n", " ldexp(...)\n", " ldexp(x, i)\n", " \n", " Return x * (2**i).\n", " \n", " lgamma(...)\n", " lgamma(x)\n", " \n", " Natural logarithm of absolute value of Gamma function at x.\n", " \n", " log(...)\n", " log(x[, base])\n", " \n", " Return the logarithm of x to the given base.\n", " If the base not specified, returns the natural logarithm (base e) of x.\n", " \n", " log10(...)\n", " log10(x)\n", " \n", " Return the base 10 logarithm of x.\n", " \n", " log1p(...)\n", " log1p(x)\n", " \n", " Return the natural logarithm of 1+x (base e).\n", " The result is computed in a way which is accurate for x near zero.\n", " \n", " log2(...)\n", " log2(x)\n", " \n", " Return the base 2 logarithm of x.\n", " \n", " modf(...)\n", " modf(x)\n", " \n", " Return the fractional and integer parts of x. Both results carry the sign\n", " of x and are floats.\n", " \n", " pow(...)\n", " pow(x, y)\n", " \n", " Return x**y (x to the power of y).\n", " \n", " radians(...)\n", " radians(x)\n", " \n", " Convert angle x from degrees to radians.\n", " \n", " sin(...)\n", " sin(x)\n", " \n", " Return the sine of x (measured in radians).\n", " \n", " sinh(...)\n", " sinh(x)\n", " \n", " Return the hyperbolic sine of x.\n", " \n", " sqrt(...)\n", " sqrt(x)\n", " \n", " Return the square root of x.\n", " \n", " tan(...)\n", " tan(x)\n", " \n", " Return the tangent of x (measured in radians).\n", " \n", " tanh(...)\n", " tanh(x)\n", " \n", " Return the hyperbolic tangent of x.\n", " \n", " trunc(...)\n", " trunc(x:Real) -> Integral\n", " \n", " Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.\n", "\n", "DATA\n", " e = 2.718281828459045\n", " inf = inf\n", " nan = nan\n", " pi = 3.141592653589793\n", " tau = 6.283185307179586\n", "\n", "FILE\n", " (built-in)\n", "\n", "\n" ] } ], "source": [ "help(math)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rounding Numbers" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "value = 4.35" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.floor(value)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.ceil(value)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mathematical Constants" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.pi" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import pi" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.e" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.283185307179586" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.tau" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.inf" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "nan" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.nan" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Logarithmic Values" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.e" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Log Base e\n", "math.log(math.e)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "math domain error", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: math domain error" ] } ], "source": [ "# Will produce an error if value does not exist mathmatically\n", "math.log(0)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.302585092994046" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(10)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.000000000000002" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.e ** 2.302585092994046" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom Base" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# math.log(x,base)\n", "math.log(100,10)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Trigonometrics Functions" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.5440211108893698" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Radians\n", "math.sin(10)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "90.0" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.degrees(pi/2)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.radians(180)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Random Module\n", "\n", "Random Module allows us to create random numbers. We can even set a seed to produce the same random set every time.\n", "\n", "The explanation of how a computer attempts to generate random numbers is beyond the scope of this course since it involves higher level mathmatics. But if you are interested in this topic check out:\n", "* https://en.wikipedia.org/wiki/Pseudorandom_number_generator\n", "* https://en.wikipedia.org/wiki/Random_seed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Understanding a seed\n", "\n", "Setting a seed allows us to start from a seeded psuedorandom number generator, which means the same random numbers will show up in a series. Note, you need the seed to be in the same cell if your using jupyter to guarantee the same results each time. Getting a same set of random numbers can be important in situations where you will be trying different variations of functions and want to compare their performance on random values, but want to do it fairly (so you need the same set of random numbers each time)." ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randint(0,100)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randint(0,100)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "74" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The value 101 is completely arbitrary, you can pass in any number you want\n", "random.seed(101)\n", "# You can run this cell as many times as you want, it will always return the same number\n", "random.randint(0,100)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "24" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randint(0,100)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "74\n", "24\n", "69\n", "45\n", "59\n" ] } ], "source": [ "# The value 101 is completely arbitrary, you can pass in any number you want\n", "random.seed(101)\n", "print(random.randint(0,100))\n", "print(random.randint(0,100))\n", "print(random.randint(0,100))\n", "print(random.randint(0,100))\n", "print(random.randint(0,100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random Integers" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randint(0,100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random with Sequences\n", "\n", "#### Grab a random item from a list" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mylist = list(range(0,20))" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.choice(mylist)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sample with Replacement\n", "\n", "Take a sample size, allowing picking elements more than once. Imagine a bag of numbered lottery balls, you reach in to grab a random lotto ball, then after marking down the number, **you place it back in the bag**, then continue picking another one." ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[15, 14, 17, 8, 17, 2, 19, 17, 6, 1]" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.choices(population=mylist,k=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sample without Replacement\n", "\n", "Once an item has been randomly picked, it can't be picked again. Imagine a bag of numbered lottery balls, you reach in to grab a random lotto ball, then after marking down the number, you **leave it out of the bag**, then continue picking another one." ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[17, 19, 11, 14, 1, 3, 4, 10, 5, 15]" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.sample(population=mylist,k=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Shuffle a list\n", "\n", "**Note: This effects the object in place!**" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Don't assign this to anything!\n", "random.shuffle(mylist)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 11, 7, 12, 10, 16, 0, 2, 18, 13, 3, 5, 17, 1, 15, 6, 14, 19, 4, 8]" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random Distributions\n", "\n", "#### [Uniform Distribution](https://en.wikipedia.org/wiki/Uniform_distribution)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "23.852305703497635" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Continuous, random picks a value between a and b, each value has equal change of being picked.\n", "random.uniform(a=0,b=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [Normal/Gaussian Distribution](https://en.wikipedia.org/wiki/Normal_distribution)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.21390381464435643" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.gauss(mu=0,sigma=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Final Note: If you find yourself using these libraries a lot, take a look at the NumPy library for Python, covers all these capabilities with extreme efficiency. We cover this library and a lot more in our data science and machine learning courses." ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }