Question: #stuck on 4 - 10 please explain import numpy as np import pandas as pd # set the random seed for repeatability np.random.seed(0) # Here's
#stuck on 4 - 10 please explain
import numpy as np import pandas as pd
# set the random seed for repeatability np.random.seed(0)
# Here's an example of how simulate 100 flips of a coin that # that is weighted 50% towards heads. flips = np.random.choice(['H', 'T'], 100, p=[0.7, 0.3]) print('100 flips: ', flips)
# # Part 1: Simulation #
# Simulation parameters
# the marginal probability of measles p_measles = 0.05 # the probability of a positive test given measles p_pos_if_measles = 0.90 # the probability of a positive test given no measles p_pos_if_okay = 0.05 # the total number of people in the simulation n = 10000
# We will create two boolean NumPy arrays, each containing n boolean values. # has_measles[i] is True if the ith person has measles # tests_positive[i] is True if the ith person tests positive for measles
#@ 1 # First we simulate who has measles and who does not. # Using p_measles, randomly initialize the has_measles array. # (assignment to has_measles) has_measles=np.random.choice([True, False], size=n, p=[p_measles, 1-p_measles])
# Check the values in has_measles. # bincount() is similar to Pandas's value_counts(), but for NumPy arrays. print('Problem 1: ', np.bincount(has_measles))
#@ 2 # Set the value of num_measles to the number of True values in has_measles. # (assignment to num_measles) num_measles = np.sum(has_measles)
# check the answer print('Problem 2: {}'.format(num_measles))
#@ 3 # Initialize the tests_positive array to all False values # (assignment to tests_positive) tests_positive = np.zeros(n, dtype=bool)
#@ 4 # Now we simulate, for the people with measles, whether they test # positive for measles or not. # Using p_pos_if_measles, randomly initialize the values of # tests_positive for people with measles. # Hint: think about a slightly easier problem: set values in # tests_positive to True if they correspond to elements in has_measles # that are True. # (update tests_positive)
# Check the answer print('Problem 4: ', np.bincount(tests_positive))
#@ 5 # Now we simulate, for the people without measles, whether they test # positive for measles or not. # Using p_pos_if_okay, randomly initialize the values of tests_positive # for people without measles. # (update tests_positive)
# Compute a confusion matrix using Pandas crosstab() # use pandas crosstab print('Problem 5: ', pd.crosstab(has_measles, tests_positive))
#@ 6 # Using the has_measles and tests_positive arrays, compute the probability # that a person has measles given positive test results. # (expression)
#@ 7 # Using the has_measles and tests_positive arrays, compute the probability # that a person has does not have measles given negative test results. # (expression)
#@ 8 # Using the has_measles and tests_positive arrays, compute the probability # that a person has measles given negative test results. # (expression)
#@ 9 # Create a data frame with two columns: # 'measles', which will contain the values in the has_measles array # 'tests_positive', which will contain the values in the tests_positive array # (assignment to df)
# Create a grouped bar plot showing the number of people with or # without measles, and the results of their tests. pd.crosstab(df['measles'], df['tests_positive']).plot.bar()
#@ 10 # Take your answers above and create a function that will perform the simulation # and return the estimated probability that a person has measles given # positive test results # (define a function)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
