A random sample of 25,000 superheroes has been loaded into your session as an array called sample_indices. This sample is a list of indices that corresponds to each superhero's index selected from the heroes list.A function named calc_bmi_lists has also been created and saved to a file titled bmi_lists.py. For convenience, it is displayed below:def calc_bmi_lists(sample_indices, hts, wts): # Gather sample heights and weights as lists s_hts = [hts[i] for i in sample_indices] s_wts = [wts[i] for i in sample_indices] # Convert heights from cm to m and square with list comprehension s_hts_m_sqr = [(ht / 100) ** 2 for ht in s_hts] # Calculate BMIs as a list with list comprehension bmis = [s_wts[i] / s_hts_m_sqr[i] for i in range(len(sample_indices))] return bmisNotice that this function performs all necessary calculations using list comprehension (hence the name calc_bmi_lists()). Dig deeper into this function and analyze the memory footprint for performing your calculations using lists:Load the memory_profiler package into your IPython session.Import calc_bmi_lists from bmi_lists.Once you've completed the above steps, use %mprun to profile the calc_bmi_lists() function acting on your superheroes data. The hts array and wts array have already been loaded into your session.After you've finished coding, answer the following question:How much memory do the list comprehension lines of code consume in the calc_bmi_lists() function? (i.e., what is the total sum of the Increment column for these four lines of code?)
Question
A random sample of 25,000 superheroes has been loaded into your session as an array called sample_indices. This sample is a list of indices that corresponds to each superhero's index selected from the heroes list.A function named calc_bmi_lists has also been created and saved to a file titled bmi_lists.py. For convenience, it is displayed below:def calc_bmi_lists(sample_indices, hts, wts): # Gather sample heights and weights as lists s_hts = [hts[i] for i in sample_indices] s_wts = [wts[i] for i in sample_indices] # Convert heights from cm to m and square with list comprehension s_hts_m_sqr = [(ht / 100) ** 2 for ht in s_hts] # Calculate BMIs as a list with list comprehension bmis = [s_wts[i] / s_hts_m_sqr[i] for i in range(len(sample_indices))] return bmisNotice that this function performs all necessary calculations using list comprehension (hence the name calc_bmi_lists()). Dig deeper into this function and analyze the memory footprint for performing your calculations using lists:Load the memory_profiler package into your IPython session.Import calc_bmi_lists from bmi_lists.Once you've completed the above steps, use %mprun to profile the calc_bmi_lists() function acting on your superheroes data. The hts array and wts array have already been loaded into your session.After you've finished coding, answer the following question:How much memory do the list comprehension lines of code consume in the calc_bmi_lists() function? (i.e., what is the total sum of the Increment column for these four lines of code?)
Solution
To answer your question, you need to follow these steps:
-
Load the memory_profiler package into your IPython session. You can do this by running the command
%load_ext memory_profilerin your IPython session. -
Import the
calc_bmi_listsfunction from thebmi_listsmodule. You can do this by running the commandfrom bmi_lists import calc_bmi_lists. -
Use the
%mprunmagic command to profile thecalc_bmi_lists()function. You can do this by running the command%mprun -f calc_bmi_lists calc_bmi_lists(sample_indices, hts, wts).
After running these commands, you will see a table output from the %mprun command. This table includes a column labeled 'Increment', which shows the memory consumed by each line of code in the calc_bmi_lists() function.
To find out how much memory the list comprehension lines of code consume, you need to sum up the 'Increment' values for these lines of code. These lines are:
s_hts = [hts[i] for i in sample_indices]s_wts = [wts[i] for i in sample_indices]s_hts_m_sqr = [(ht / 100) ** 2 for ht in s_hts]bmis = [s_wts[i] / s_hts_m_sqr[i] for i in range(len(sample_indices))]
Please note that the actual memory consumption can vary depending on your specific data and environment.
Similar Questions
Using %mprun: Hero BMIYou'd like to calculate the body mass index (BMI) for a selected sample of heroes. BMI can be calculated using the below formula:A random sample of 25,000 superheroes has been loaded into your session as an array called sample_indices. This sample is a list of indices that corresponds to each superhero's index selected from the heroes list.A function named calc_bmi_lists has also been created and saved to a file titled bmi_lists.py. For convenience, it is displayed below:def calc_bmi_lists(sample_indices, hts, wts): # Gather sample heights and weights as lists s_hts = [hts[i] for i in sample_indices] s_wts = [wts[i] for i in sample_indices] # Convert heights from cm to m and square with list comprehension s_hts_m_sqr = [(ht / 100) ** 2 for ht in s_hts] # Calculate BMIs as a list with list comprehension bmis = [s_wts[i] / s_hts_m_sqr[i] for i in range(len(sample_indices))] return bmisNotice that this function performs all necessary calculations using list comprehension (hence the name calc_bmi_lists()). Dig deeper into this function and analyze the memory footprint for performing your calculations using lists:Load the memory_profiler package into your IPython session.Import calc_bmi_lists from bmi_lists.Once you've completed the above steps, use %mprun to profile the calc_bmi_lists() function acting on your superheroes data. The hts array and wts array have already been loaded into your session.After you've finished coding, answer the following question:How much memory do the list comprehension lines of code consume in the calc_bmi_lists() function? (i.e., what is the total sum of the Increment column for these four lines of code?)
A list of 480 superheroes has been loaded into your session (called heroes) as well as a list of each hero's corresponding publisher (called publishers).You'd like to filter the heroes list based on a hero's specific publisher, but are unsure which of the below functions is more efficient.def get_publisher_heroes(heroes, publishers, desired_publisher): desired_heroes = [] for i,pub in enumerate(publishers): if pub == desired_publisher: desired_heroes.append(heroes[i]) return desired_heroesdef get_publisher_heroes_np(heroes, publishers, desired_publisher): heroes_np = np.array(heroes) pubs_np = np.array(publishers) desired_heroes = heroes_np[pubs_np == desired_publisher] return desired_heroesInstructions 1/425 XP1234Use the get_publisher_heroes() function and the get_publisher_heroes_np() function to collect heroes from the Star Wars universe. The desired_publisher for Star Wars is 'George Lucas'.
A list of 480 superheroes has been loaded into your session (called heroes) as well as a list of each hero's corresponding publisher (called publishers).You'd like to filter the heroes list based on a hero's specific publisher, but are unsure which of the below functions is more efficient.def get_publisher_heroes(heroes, publishers, desired_publisher): desired_heroes = [] for i,pub in enumerate(publishers): if pub == desired_publisher: desired_heroes.append(heroes[i]) return desired_heroesdef get_publisher_heroes_np(heroes, publishers, desired_publisher): heroes_np = np.array(heroes) pubs_np = np.array(publishers) desired_heroes = heroes_np[pubs_np == desired_publisher] return desired_heroesInstructions 2/425 XP234QuestionWithin your IPython console, load the line_profiler and use %lprun to profile the two functions for line-by-line runtime. When using %lprun, use each function to gather the Star Wars heroes as you did in the previous step.
Use np_height_m and np_weight_kg to calculate the BMI of each player. Use the following equation: BMI=weight(kg)height(m)2Save the resulting numpy array as bmi
John is a fitness trainer and needs to calculate the Body Mass Index (BMI) for his clients to assess their health status. Write a program for him that takes the weight in kilograms and height in meters of a client as input, calculates their BMI, and determines if they fall within the healthy range.Note: The healthy range of BMI is 18.5 to 24.9 (both inclusive).Input format :The input consists of two double values: the weight in kilograms and the height in meters.Output format :The first line of output prints "BMI: X" where X is a double value, rounded off to two decimal places.The second line prints one of the following:If the BMI is within the valid range, print "Healthy Range".Otherwise, print "Not in Healthy Range".Refer to the sample output for formatting specificationsCode constraints :10.0 ≤ weight ≤ 300.00.00 ≤ height ≤ 3.00Sample test cases :Input 1 :65.81.75Output 1 :BMI: 21.49Healthy RangeInput 2 :124.31.87Output 2 :BMI: 35.55Not in Healthy Range
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.