Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python function below, using numpy Task 2.1: Computing Death Rates Our first task in this part of the homework is to implement compute_death_rate_first_n_days, which takes

python function below, using numpy

Task 2.1: Computing Death Rates

Our first task in this part of the homework is to implement compute_death_rate_first_n_days, which takes in three arguments, n, cases_cumulative and deaths_cumulative, and computes the average number of deaths recorded for every confirmed case that has been recorded from the first day to the nth day (inclusive). This should be done for each country.

The return value should be a np.ndarray such that the entry in the -th row corresponds to the death rate in the -th country as represented in cases_cumulative and deaths_cumulative.

For instance, if the returned value is np.array([0.5, 0.2]), it means that in the 0th country, for every 2 individuals who contracted the virus, one of them will, on average, die. In contrast, in the 1st country, for every 5 individuals who contracted the virus, only one of them will, on average, die.

Note: You may assume that the -th row in cases_cumulative represents the same country as the -th row in deaths_cumulative. Moreover, if there are no confirmed cases for a particular country, its average death rate should be zero. If the data includes less than n days of data, then you just return the result for all the days given in the data provided.

In this task, the goal is to learn how to deal with nan values using np.nan_to_num, which has the following signature: numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None). You should also check out the detailed description here because there are many functions of the same nature. Essentially, we can use np.nan_to_num to convert all the nan values in an np.ndarray to a specified value. Some examples of how np.nan_to_num can be used is shown below:

In [65]:

print(np.nan_to_num(np.inf)) #1.7976931348623157e+308
print(np.nan_to_num(-np.inf)) #-1.7976931348623157e+308
print(np.nan_to_num(np.nan)) #0.0
x = np.array([np.inf, -np.inf, np.nan, -128, 128])
np.nan_to_num(x)
np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) # Specify nan to be -9999, and both posinf and neginf to be 33333333
np.nan_to_num(y, nan=111111, posinf=222222) # Specify nan to be 111111, and both posinf and neginf to be 222222
1.7976931348623157e+308 -1.7976931348623157e+308 0.0 
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [65], in () 5 np.nan_to_num(x) 6 np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) # Specify nan to be -9999, and both posinf and neginf to be 33333333 ----> 7 np.nan_to_num(y, nan=111111, posinf=222222) NameError: name 'y' is not defined 

In [ ]:

 

In [26]:

def compute_death_rate_first_n_days(n, cases_cumulative, deaths_cumulative):
 '''
 Computes the average number of deaths recorded for every confirmed case
 that is recorded from the first day to the nth day (inclusive).
 Parameters
 ----------
 n: int
 How many days of data to return in the final array.
 cases_cumulative: np.ndarray
 2D `ndarray` with each row representing the data of a country, and the columns
 of each row representing the time series data of the cumulative number of
 confirmed cases in that country, i.e. the ith row of `cases_cumulative`
 contains the data of the ith country, and the (i, j) entry of
 `cases_cumulative` is the cumulative number of confirmed cases on the
 (j + 1)th day in the ith country.
 deaths_cumulative: np.ndarray
 2D `ndarray` with each row representing the data of a country, and the columns
 of each row representing the time series data of the cumulative number of
 confirmed deaths (as a result of COVID-19) in that country, i.e. the ith
 row of `n_deaths_cumulative` contains the data of the ith country, and
 the (i, j) entry of `n_deaths_cumulative` is the cumulative number of
 confirmed deaths on the (j + 1)th day in the ith country.
 
 Returns
 -------
 Average number of deaths recorded for every confirmed case from the first day
 to the nth day (inclusive) for each country as a 1D `ndarray` such that the
 entry in the ith row corresponds to the death rate in the ith country as
 represented in `cases_cumulative` and `deaths_cumulative`.
 Note
 ----
 `cases_cumulative` and `deaths_cumulative` are such that the ith row in the 
 former and that in the latter contain data of the same country. In addition,
 if there are no confirmed cases for a particular country, the expected death
 rate for that country should be zero. (Hint: to deal with NaN look at
 `np.nan_to_num`)
 '''
 
 # TODO: add your solution here and remove `raise NotImplementedError`
 import numpy as np
 
 arr = np.zeros(n) 
 arr1 = np.mean(cases_cumulative)
 arr2 = np.mean(deaths_cumulative)
 
 

In [22]:

# Test cases for Task 2.1
import numpy as np
 
result_all_zeros = compute_death_rate_first_n_days(100,np.zeros((5, 200)), np.zeros((5, 200)))
expected = np.zeros(5)
assert(np.all(result_all_zeros == expected))
 
sample_cumulative = np.array([[1,2,3,4,8,8,10,10,10,10]])
sample_death = np.array([[0,0,0,1,2,2,2,2,5,5]])
expected2 = np.array([0.5])
assert(compute_death_rate_first_n_days(10, sample_cumulative, sample_death) == expected2)
 
expected3 = np.array([0.25])
assert(compute_death_rate_first_n_days(5, sample_cumulative, 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions

Question

How have your cultural experiences influenced your development?

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago