Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 3: Percentage calculation Write a function named get_percentage (table, column, category), which returns the percentage of failed students (whose average score is below
Task 3: Percentage calculation Write a function named get_percentage (table, column, category), which returns the percentage of failed students (whose average score is below 50%) that belong to the given category in the given column. Make sure you round the percentage to two decimal places before returning. gender male male For example, consider the following set of rows from the given data set, with the average score appended. female female male female percentage = male male ethnicity EB C C C C Number of failed students in the given category Total number of failed students B parental level of education associate's degree high school associate's degree associate's degree some college associate's degree associate's degree some college lunch free/reduce d standard standard free/reduce d free/reduce d standard standard standard test preparation course nothing nothing completed nothing nothing nothing nothing completed math reading writing score score score 69 58 54 23 39 83 58 71 61 47 62 44 32 x 100% 76 52 69 57 42 65 31 88 Avg score 44 37.00 55 73 62.33 49.00 60.33 34.00 82.33 55.00 71.00 For the above set of rows, we have 3 failed students whose average score is below 50%. If we call the function for the second column (ethnicity) and for the group C category, get_percentage (exam_data, 2, 'C') the functions should return 66.67 because 2 out of 3 failed students belong to group C ethnicity. For the same set of data above, if we call the function for the fifth column (test preparation course) and for nothing (no preparation course), get_percentage (exam_data, 5, 'nothing') the functions should return 100 because all of the 3 failed students did not complete the test preparation course. Note: the column numbers start from 1. That is, the first column is column 1, second column is column 2 and so on. Note: Your function should work for the whole data set. The above small snippet of the data set was used to explain the functionality of the function. Input: A table containing all the data that is available in the given data file, a column number and a category in that given column. Output: The percentage of failed students who belong to that category. For example: >>> exam_data = read_data('exams.txt') >>> add_average_score (exam_data) >>> get_percentage (exam_data, 2, 'A') 10.48 >>> get_percentage (exam_data, 2, 'C') 43.55 >>>get_percentage (exam_data, 3, 'HS') 24.19 >>> get_percentage (exam_data, 3, 'MD') 3.23 >>> get_percentage (exam_data, 4, 'R') 66.94 >>> get_percentage (exam_data, 5, 'nothing') 85.48 Task 4: Category percentages Write a function named category_percentages (table) that returns a table with the failure percentages for all the categories in columns 2 (ethnicity), 3 (parental education level), 4 (lunch type) and 5 (status of the test preparation course). Use the get_percentage (table, column, category) function from task 3 (don't re-write the code for percentage calculation). The output table of this function should have one row for each of the considered field (column) of the data set (ie. first row is for ethnicity, second row is for parental education level etc.). Each row of the output table contains the percentage values for each category of that field (ie. first row first column Note: When you add the percentages to the output table for each of the considered fields, follow the order given in task 1 part (a), (b) and (c) for the ethnicity, the parental education level and lunch type. For test preparation status, store the percentage of those who completed the preparation first, followed by those who did not. Please refer to the table below for the order of the fields (columns) and the order of the category for each field. Ethnicity A, B, C, D, E Parents' education (refer to task 1 for abbreviations). C, AD, HS, S, BD, MD For example: Lunch type Test preparation >>> exam_data = read_data('exams.txt') >>> add_average_score(exam_data) >>> category_percentages (exam_data) S, R Input: A table containing all the data that is available in the given data file, with the average score appended. Output: A table containing the failed percentages for each category (with one row per field). nothing, completed [[10.48, 20.16, 43.55, 16.94, 8.87], [25.81, 10.48, 24.19, 31.45, 4.84, 3.23], [33.06, 66.94], [85.48, 14.52]] Task 5: Write the results of the analysis to a separate text file Write a function named record_results (table, output_filename='analysis_results.txt') that writes the details of the failed percentages, into the given text file, output_filename. Add the title "Failed percentages" as the first line in the file. Note: You have to reuse the functions written in the previous tasks. Do not rewrite the code written in the previous tasks here. Input: A table containing all the data that is available in the given data file with the average score appended. Output: The given file updated with the details of the failed percentages. For example: >>> exam_data = read_data('exams.txt') >>> add_average_score(exam_data) >>> record_results(exam_data, 'analysis_results.txt') should create the analysis_results.txt file and store the results into the file as shown below. analysis_results.txt - Notepad File Edit Format View Help Failed percentages Ethnicity A, 10.48 B, 20.16 C,43.55 D, 16.94 E,8.87 Parent education level C, 25.81 AD, 10.48 HS, 24.19 S,31.45 BD,4.84 MD, 3.23 Lunch type 5,33.06 R, 66.94 Test preparation none, 85.48 completed, 14.52 Task 2: Average mark calculation Write a function named add_average_score (table), which calculates the average score of all three given scores for each student and append it to the end of the table (as the column next to the writing score). Make sure you round the average score to two decimal places. Note: Your function should NOT return a new table. It should append the new column to the given table (taken as the input to the function), instead. Input: A table containing all the student data that is available in the given data file (table) Output: The same table with the average score appended to the end of the table (for each row). For example: >>> exam_data read_data('exams.txt') >>> add_average_score (exam_data) >>> exam_data[:2] # The first two rows of the table [['male', 'A', 'HS', 'S', 'completed', 67.0, 67.0, 63.0, 65.67], ['female', 'D', 'S', 'R', 'nothing', 40.0, 59.0, 55.0, 51.33]] Analysis of failures You are going to do some analysis on the failed students to see what factors might be affecting their poor performance. You need to see if the ethnic group, parental education level, type of lunch and test preparation play a role in the performance outcome. Let us decompose the problem into two parts. Task 1: Read the data into a table Implement a function named read_data (filename), with the following specification, which reads a given file and returns the contents in a table. While reading the contents you need to carry out the following tasks before saving the contents to the table. a. Store the ethnic group using the letter only (ie. group A stored as A, group B stored as B etc.) Store a short form for the parental level of education as shown below. b. i. C for some college ii. AD for associate's degree iii. iv. HS for high school S for some high school V. BD for bachelor's degree vi. MD for master's degree c. Store a short form for lunch as shown below i. S for standard ii. R for free/reduced d. You need to convert all the numerical values to float. Hint: you may use the isalpha string method (you may assume none of the fields in the data file are alphanumeric). Input: The name of the data file (filename) Output: A table with all the rows of data in the given file. For example: >>> exam_data = read_data('exam.txt') >>> exam_data[:2] # The first two rows of the table [['male', 'A', 'HS', 'S', 'completed', 67.0, 67.0, 63.0], ['female', 'D', 'S', 'R', 'nothing', 40.0, 59.0, 55.0]] (Note: this is only for testing your code. The table you output should have all the rows of data).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started