# lab05.pdf - Lab 5 Simulations Welcome to Lab 5 We will go...

• Lab Report
• 30
• 94% (18) 17 out of 18 people found this document helpful

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

Lab 5: Simulations Welcome to Lab 5! We will go over iteration () and simulations () , as well as introduce the concept of randomness () . The data used in this lab will contain salary data and other statistics for basketball players from the 2014-2015 NBA season. This data was collected from the following sports analytic sites: Basketball Reference () and Spotrac () . First, set up the tests and imports by running the cell below. In [1]: 1. Nachos and Conditionals ===================================================================== Assignment: Simulations OK, version v1.12.5 ===================================================================== # Run this cell, but please don't change it. # These lines import the Numpy and Datascience modules. import numpy as np from datascience import * # These lines do some fancy plotting magic import matplotlib % matplotlib inline import matplotlib.pyplot as plt plt.style.use( 'fivethirtyeight' ) # Don't change this cell; just run it. from client.api.notebook import Notebook ok = Notebook( 'lab05.ok' )
In Python, the boolean data type contains only two unique values: True and False . Expressions containing 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 [2]: Out[2]: 3 > 1 + 1
We can even assign the result of a comparison operation to a variable. In [3]: Arrays are compatible with comparison operators. The output is an array of boolean values. In [4]: array([False, True, True, True, False, False]) Out[3]: True Out[4]: result = 10 / 2 == 5 result make_array( 1 , 5 , 7 , 8 , 3 , - 1 ) > 3
One day, when you come home after a long week, you see a hot bowl of nachos waiting on the dining table! Let's say that whenever you take a nacho from the bowl, it will either have only cheese , only salsa , both cheese and salsa, or neither cheese nor salsa (a sad tortilla chip indeed). Let's try and simulate taking nachos from the bowl at random using the function, np.random.choice(...) . np.random.choice np.random.choice picks one item at random from the given array. It is equally likely to pick any of the items. Run the cell below several times, and observe how the results change. In [5]: To repeat this process multiple times, pass in an int n as the second argument. By default, np.random.choice samples with replacement and returns an array of items. Run the next cell to see an example of sampling with replacement 10 times from the nachos array. 'neither' 'cheese', 'both', 'neither'], dtype='<U7') nachos = make_array( 'cheese' , 'salsa' , 'both' , 'neither' np.random.choice(nachos) np.random.choice(nachos, 10 In [6]: Question 1. Assume we took ten nachos at random, and stored the results in an array called ten_nachos as done below. Find the number of nachos with only cheese using code (do not hardcode the answer). Out[5]: 'neither' 'cheese', 'both', 'neither'], dtype='<U7') nachos = make_array( 'cheese' , 'salsa' , 'both' , 'neither' np.random.choice(nachos) np.random.choice(nachos, 10 Out[6]: array(['salsa', 'neither', 'cheese', 'both', 'salsa', 'cheese', 'both' , )