lab05_solutions.pdf - 7/1/2018 lab05_solutions Lab 5:...

This preview shows page 1 - 3 out of 16 pages.

7/1/2018lab05_solutions1/16Lab 5: SimulationsWelcome to lab 5! This week, we will go over iteration and simulations, and introduce the concept of randomness. All of this material is covered inChapter 9() andChapter 10(-distributions.html) of the textbook.The data used in this lab will contain salary data and statistics for basketball players from the 2014-2015 NBA season. This data was collected from sports analytic sitesbasketball-reference() andspotrac().First, set up the tests and imports by running the cell below.In [1]:# 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.pyplotasplotsplots.style.use('fivethirtyeight')# Don't change this cell; just run it.fromclient.api.notebookimportNotebookok = Notebook('lab05.ok')_ = ok.auth(inline=True)1. Nachos and ConditionalsIn Python, Boolean values can either beTrueorFalse. We get Boolean values when using comparison operators, among which are<(less than),>(greater than), and==(equal to). For a complete list, refer toBooleans and Comparison() at thestart of Chapter 9.Run the cell below to see an example of a comparison operator in action.In [2]:3 > 1 + 1We can even assign the result of a comparison operation to a variable.In [3]:result = 10 / 2 == 5resultArrays are compatible with comparison operators. The output is an array of boolean values.In [4]:make_array(1, 5, 7, 8, 3, -1) > 3=====================================================================Assignment: SimulationsOK, version v1.13.9=====================================================================ERROR| auth.py:91 | {'error': 'invalid_grant'}Open the following URL:After logging in, copy the code from the web page and paste it into the box.Then press the "Enter" key on your keyboard.Paste your code here: tFqX6ucyK0T9DKhT1h1Aq04Ym6N0VlSuccessfully logged in as [email protected]Out[2]:TrueOut[3]:TrueOut[4]:array([False,True,True,True, False, False], dtype=bool)
7/1/2018lab05_solutions2/16Waiting on the dining table just for you is a hot bowl of nachos! Let's say that whenever you take a nacho, it will have cheese, salsa, both, or neither (just a plain tortillachip).Using the function callnp.random.choice(array_name), let's simulate taking nachos from the bowl at random. Start by running the cell below several times, andobserve how the results change.In [5]:nachos = make_array('cheese', 'salsa', 'both', 'neither')np.random.choice(nachos)Question 1.Assume we took ten nachos at random, and stored the results in an array calledten_nachosas done below. Find the number of nachos with only cheeseusing code (do not hardcode the answer).Hint:Our solution involves a comparison operator and thenp.count_nonzero

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 16 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Pride and Prejudice, Simple random sample

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture