100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

Data C8 Lab 5: Simulations | Spring 2025, Complete Solutions

Rating
-
Sold
-
Pages
13
Grade
A
Uploaded on
17-04-2025
Written in
2024/2025

Data C8 Lab 5: Simulations | Spring 2025, Complete Solutions










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
April 17, 2025
Number of pages
13
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers

Content preview

Lab 5: Simulations
Welcome to lab 5! This week, we will go over iteration and simulations, and introduce the concept of randomness. All of this material is covered in Chapter 9
(https://www.inferentialthinking.com/chapters/09/randomness.html) and Chapter 10 (https://www.inferentialthinking.com/chapters/10/sampling-and-empirical-
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
sites basketball-reference (http://www.basketball-reference.com) and spotrac (http://www.spotrac.com).

First, set up the tests and imports by running the cell below.


In [ ]: # 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 plots
plots.style.use('fivethirtyeight')

# Don't change this cell; just run it.
from client.api.notebook import Notebook
ok = Notebook('lab05.ok')
_ = ok.auth(inline=True)




1. Nachos and Conditionals ¶

In Python, Boolean values can either be True or False . We get Boolean values when using comparison operators, among which are < (less than), > (greater
than), and == (equal to). For a complete list, refer to Booleans and Comparison (https://www.inferentialthinking.com/chapters/09/randomness.html#Booleans-and-
Comparison) at the start of Chapter 9.

Run the cell below to see an example of a comparison operator in action.


In [ ]: 3 > 1 + 1



We can even assign the result of a comparison operation to a variable.


In [ ]: result = == 5
result



Arrays are compatible with comparison operators. The output is an array of boolean values.


In [ ]: make_array(1, 5, 7, 8, 3, -1) > 3



Waiting 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
tortilla chip).

Using the function call np.random.choice(array_name) , let's simulate taking nachos from the bowl at random. Start by running the cell below several times,
and observe how the results change.


In [ ]: 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 called ten_nachos as done below. Find the number of nachos with only
cheese using code (do not hardcode the answer).

Hint: Our solution involves a comparison operator and the np.count_nonzero method.

, In [ ]: ten_nachos = make_array('neither', 'cheese', 'both', 'both', 'cheese', 'salsa', 'both', 'neither', 'cheese', 'bo
th')
number_cheese = np.count_nonzero(ten_nachos == 'cheese') #SOLUTION
number_cheese

In [ ]: _ = ok.grade('q1_1')



Conditional Statements

A conditional statement is made up of many lines that allow Python to choose from different alternatives based on whether some condition is true.

Here is a basic example.

def sign(x):
if x > 0:
return 'Positive'

How the function works is if the input x is greater than 0 , we get the string 'Positive' back.

If we want to test multiple conditions at once, we use the following general format.

if <if expression>:
<if body>
elif <elif expression 0>:
<elif body 0>
elif <elif expression 1>:
<elif body 1>
...
else:
<else body>

Only one of the bodies will ever be executed. Each if and elif expression is evaluated and considered in order, starting at the top. As soon as a true value is
found, the corresponding body is executed, and the rest of the expression is skipped. If none of the if or elif expressions are true, then the else body is
executed. For more examples and explanation, refer to Section 9.1 (https://www.inferentialthinking.com/chapters/09/1/conditional-statements.html).



Question 2. Complete the following conditional statement so that the string 'More please' is assigned to say_please if the number of nachos with cheese in
ten_nachos is less than 5 .

Hint: You should not have to directly reference the variable ten_nachos .


In [ ]: say_please = '?'

if ...:
say_please = 'More please'

say_please


In [ ]: say_please = '?'

if number_cheese < 5:
say_please = 'More please'

say_please

In [ ]: _ = ok.grade('q1_2')



Question 3. Write a function called nacho_reaction that returns a string based on the type of nacho passed in as an argument. From top to bottom, the
conditions should correspond to: 'cheese' , 'salsa' , 'both' , 'neither' .


In [ ]: def nacho_reaction(nacho):
if ...:
return 'Cheesy!'
# next condition should return 'Spicy!'
...
# next condition should return 'Wow!'
...
# next condition should return 'Meh.'
...

spicy_nacho = nacho_reaction('salsa')
spicy_nacho

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Jumuja Liberty University
View profile
Follow You need to be logged in order to follow users or courses
Sold
547
Member since
4 year
Number of followers
415
Documents
2647
Last sold
14 hours ago

3.9

115 reviews

5
60
4
15
3
20
2
4
1
16

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions