lab05.pdf - lab05[1 Initialize OK from client.api.notebook...

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

lab05 April 26, 2020 [1]: # Initialize OK from client.api.notebook import Notebook ok = Notebook( 'lab05.ok' ) ===================================================================== Assignment: Simulations OK, version v1.12.5 ===================================================================== 1 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. [2]: # 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' ) 1
===================================================================== Assignment: Simulations OK, version v1.12.5 ===================================================================== 1.1 1. Nachos and Conditionals 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. [3]: 3 > 1 + 1 [3]: True We can even assign the result of a comparison operation to a variable. [4]: result = 10 / 2 == 5 result [4]: True Arrays are compatible with comparison operators. The output is an array of boolean values. [5]: make_array( 1 , 5 , 7 , 8 , 3 , -1 ) > 3 [5]: array([False, True, True, True, False, False]) 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(...) . 1.1.1 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. [7]: nachos = make_array( 'cheese' , 'salsa' , 'both' , 'neither' ) np . random . choice(nachos) [7]: 'neither' 2
To repeat this process multiple times, pass in an int n as the second argument to return n different random choices. 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. [8]: np . random . choice(nachos, 10 ) [8]: array(['both', 'both', 'salsa', 'both', 'cheese', 'salsa', 'cheese', 'neither', 'salsa', 'neither'], dtype='<U7') To count the number of times a certain type of nacho is randomly chosen, we can use np.count_nonzero 1.1.2 np.count_nonzero np.count_nonzero counts the number of non-zero values that appear in an array. When an array of boolean values are passed through the function, it will count the number of True values (remember that in Python, True is coded as 1 and False is coded as 0.)

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture