# lab05.pdf - In [1]: # Initialize OK from...

• 27
• 100% (20) 20 out of 20 people found this document helpful

This preview shows page 1 - 4 out of 27 pages.

The preview shows page 2 - 4 out of 27 pages.
In [1]:# Initialize OKfromclient.api.notebookimportNotebookok=Notebook('lab05.ok')Lab 5: SimulationsWelcome to Lab 5!We will go overiteration()andsimulations(), as well asintroduce the concept ofrandomness()andconditional probability().The data used in this lab will contain salary data and other statistics for basketball players from the2014-2015 NBA season. This data was collected from the following sports analytic sites:BasketballReference()andSpotrac().First, set up the tests and imports by running the cell below.In [2]:# Run this cell, but please don't change it.# These lines import the Numpy and Datascience modules.importnumpyasnpfromdatascienceimport*# These lines do some fancy plotting magicimportmatplotlib%matplotlibinlineimportmatplotlib.pyplotaspltplt.style.use('fivethirtyeight')# Don't change this cell; just run it.fromclient.api.notebookimportNotebookok=Notebook('lab05.ok')=====================================================================Assignment: SimulationsOK, version v1.12.5==========================================================================================================================================Assignment: SimulationsOK, version v1.12.5=====================================================================
1. Nachos and ConditionalsIn Python, the boolean data type contains only two unique values:TrueandFalse. Expressionscontaining comparison operators such as<(less than),>(greater than), and==(equal to)evaluate to Boolean values. A list of common comparison operators can be found below!Run the cell below to see an example of a comparison operator in action.In [3]:3 > 1 + 1We can even assign the result of a comparison operation to a variable.In [4]:result= 10 / 2 == 5resultOut[3]:TrueOut[4]:
Arrays are compatible with comparison operators. The output is an array of boolean values.In [5]:make_array(1,5,7,8,3,-1One day, when you come home after a long week, you see a hot bowl of nachos waiting on thedining table! Let's say that whenever you take a nacho from the bowl, it will either have onlycheese, onlysalsa,bothcheese and salsa, orneithercheese nor salsa (a sad tortilla chipindeed).Let's try and simulate taking nachos from the bowl at random using the function,np.random.choice(...).np.random.choicenp.random.choicepicks one item at random from the given array. It is equally likely to pick anyof the items. Run the cell below several times, and observe how the results change.Out[5]:array([False,True,True,True, False, False]))> 3
In [6]:nachos=make_array('cheese','salsa','both','neither')np.random.choice(nachos)To repeat this process multiple times, pass in an intnas the second argument to returnndifferent random choices. By default,np.random.choicesampleswith replacementand returnsanarrayof items.

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 27 pages?