{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook for class 4\n", "\n", "This is the notebook I was typing into in class 4, with some notes.\n", "\n", "First we explored different data types." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "my_float = 1.0" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_float" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see what type of thing a variable refers to with the `type` function. We will see more of functions later." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_float)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice the lack of a decimal point in the following statement." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "my_other_thing = 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gives us an `int`:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_other_thing" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_other_thing)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are about to make a string. Here I am trying to make the string \"I love Python\", but I get an error - why?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (<ipython-input-7-273633364422>, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"<ipython-input-7-273633364422>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m my_string = I love python\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "my_string = I love python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looked there was a problem with the spaces, so I replace the spaces with underscores:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'I_love_python' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-8-0f708e6c0151>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_string\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mI_love_python\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'I_love_python' is not defined" ] } ], "source": [ "my_string = I_love_python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now it becomes clear that Python thought that I was trying to refer to a variable name that does not exist.\n", "\n", "Here's a string - it needs quotes:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "my_string = \"I love python\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I love python'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_string" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that Python displays strings with quotes, by default (at least in the Notebook):" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I love python'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'I love python'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you use the `print` function, it does not add the quotes." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I love python\n" ] } ], "source": [ "print(my_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here I go through the class exercise:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "a = 1.0\n", "b = 1\n", "c = 'Hello'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here I add a float to itself:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The addition produces a `float`:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a + a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding two `int`s gives an `int`:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b + b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding two strings concatenates the strings:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HelloHello'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c + c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding a `float` to an `int` gives a `float`:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python won't let you add `float`s (or `int`s) to a string:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'float' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-20-e81e582b6fa9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'float' and 'str'" ] } ], "source": [ "a + c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember *assignment* - a name, followed by `=`, followed by an expression:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "a = 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It sets the value of `a` (in this case):" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a *comparison* expression. `<` is a *comparison operator*, and returns the result of the comparison _less than_." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we look at the value returned from this expression:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "my_logical = a < 10" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_logical" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's a `bool` type (Boolean)." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_logical)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Booleans can be either `True` or `False`:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a > 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember assignment (again). It has the single `=` between the name (on the left) and the expression (on the right)." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "a = 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is something very different - an equality comparison operator. This is two equals signs: `==`. It's an expression. It tests whether the thing on the left is equal to the thing on the right, returning `True` or `False`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we consider *call expressions*." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "a = 2 / 3" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6666666666666666" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we *call* the function `round`, with the *argument* `a` - the number we want it to round." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can pass `round` two arguments. The second argument is the number of decimal points we want it to round to." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.67" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(a, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also give this argument a *name*. In that case, it is called a *keyword argument*." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.67" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(a, ndigits=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To find what the names of the arguments are, look at the help:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "round?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have to get the name of the argument right, otherwise Python\n", "complains." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'number_of_digits' is an invalid keyword argument for this function", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-36-c89cc06512a3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mround\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumber_of_digits\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'number_of_digits' is an invalid keyword argument for this function" ] } ], "source": [ "round(a, number_of_digits=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check the names, from the help:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "round?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we get on to arrays." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here I am calling a function with no arguments. It's a call expression, but in this case, the function needs no arguments. It returns a single random number:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7558424036608197" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.uniform()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once we have checked the help, we find there is a `size` argument, that we can specify. This allows us to ask for 100 random numbers." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.36693208, 0.87758662, 0.81019582, 0.82214536, 0.36394251,\n", " 0.07969345, 0.35343822, 0.97806641, 0.9532617 , 0.59773898,\n", " 0.58081863, 0.62741994, 0.95847418, 0.03567867, 0.05212555,\n", " 0.71711456, 0.17662301, 0.11707422, 0.41300096, 0.06000016,\n", " 0.7726217 , 0.43842319, 0.14607717, 0.01461987, 0.45210905,\n", " 0.11072198, 0.82793531, 0.01498911, 0.30554031, 0.24935128,\n", " 0.15916459, 0.54524344, 0.03558632, 0.48099707, 0.43274497,\n", " 0.82487948, 0.3796686 , 0.60998452, 0.28689629, 0.59365581,\n", " 0.31763947, 0.92812618, 0.44717822, 0.05692277, 0.97550456,\n", " 0.76643058, 0.19427462, 0.65746642, 0.02530174, 0.42710621,\n", " 0.16244242, 0.41119599, 0.16460324, 0.03615299, 0.5873492 ,\n", " 0.04265181, 0.09245906, 0.56257469, 0.64322541, 0.62147685,\n", " 0.75644138, 0.27570146, 0.11715281, 0.48179549, 0.44877749,\n", " 0.88395977, 0.33036868, 0.19958962, 0.37841863, 0.58654954,\n", " 0.87654479, 0.86413769, 0.3123292 , 0.20707659, 0.24201805,\n", " 0.74959689, 0.06625441, 0.02026559, 0.97464529, 0.63600724,\n", " 0.06385651, 0.34978269, 0.37378996, 0.06817189, 0.67991121,\n", " 0.17659475, 0.19225224, 0.30393928, 0.32401367, 0.57006609,\n", " 0.36204343, 0.1393311 , 0.03584481, 0.93263328, 0.73635803,\n", " 0.92246816, 0.72562353, 0.00144158, 0.80831398, 0.05020239])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.uniform(size=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here I put these into a variable:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "randoms = np.random.uniform(size=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's a new type - `array`:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(randoms)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the things that `array`s have, is a *length*. We can get that with the `len` function:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(randoms)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.99759455, 0.18332171, 0.48756725, 0.26591066, 0.15436288,\n", " 0.95128538, 0.66077124, 0.55775149, 0.03508935, 0.5412619 ,\n", " 0.64847867, 0.34489874, 0.69598301, 0.06871524, 0.25808015,\n", " 0.58960472, 0.72211686, 0.27325624, 0.67260077, 0.83995721,\n", " 0.79970784, 0.95336243, 0.964208 , 0.83936253, 0.05656299,\n", " 0.90039992, 0.1338113 , 0.13691248, 0.14159298, 0.34965063,\n", " 0.61237528, 0.16934558, 0.96038391, 0.42721043, 0.98938284,\n", " 0.95646816, 0.18415332, 0.81226052, 0.73249926, 0.30732336,\n", " 0.87148763, 0.81042182, 0.73458396, 0.00581783, 0.96601248,\n", " 0.21221345, 0.92395521, 0.54040162, 0.52355445, 0.02329994,\n", " 0.50524813, 0.00135702, 0.44433152, 0.55002481, 0.63741974,\n", " 0.95615915, 0.49641958, 0.67045131, 0.21201677, 0.86107248,\n", " 0.8180656 , 0.17918569, 0.51762375, 0.7829002 , 0.83993162,\n", " 0.75212422, 0.86351651, 0.71620174, 0.16334626, 0.12899758,\n", " 0.41677654, 0.72401677, 0.72959161, 0.37035538, 0.65291767,\n", " 0.6762217 , 0.10313215, 0.5606955 , 0.03125565, 0.26660795,\n", " 0.23112951, 0.59380057, 0.67975397, 0.25033702, 0.83819111,\n", " 0.38698292, 0.86193146, 0.90357501, 0.61546889, 0.36227131,\n", " 0.75510565, 0.54165405, 0.96419388, 0.65234739, 0.61019126,\n", " 0.4283649 , 0.08000637, 0.56819196, 0.86529496, 0.0179533 ])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "randoms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we do a comparison on a single random number:" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.12583938290027896" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_random = np.random.uniform()\n", "a_random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For our simulation, we want to check whether the random number is less than 0.26. If so, we label this as a black juror." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_random < 0.26" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to do this 100 times. We can do this with arrays, in one shot:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, False, False, True, False, False, False, True,\n", " False, False, False, False, True, True, False, False, False,\n", " False, False, False, False, False, False, True, False, True,\n", " True, True, False, False, True, False, False, False, False,\n", " True, False, False, False, False, False, False, True, False,\n", " True, False, False, False, True, False, True, False, False,\n", " False, False, False, False, True, False, False, True, False,\n", " False, False, False, False, False, True, True, False, False,\n", " False, False, False, False, True, False, True, False, True,\n", " False, False, True, False, False, False, False, False, False,\n", " False, False, False, False, False, False, True, False, False, True], dtype=bool)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "black_yn = randoms < 0.26\n", "black_yn" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can use the `np.count_nonzero` function to count how many `True` values there are. This is our first simulation of a jury pool, of 100 jurors." ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.count_nonzero(black_yn)" ] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".Rmd", "format_name": "rmarkdown", "format_version": "1.0", "jupytext_version": "0.8.1" } }, "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }