Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help getting this python code to produce the output I provide in an image below. I've written the code, I just need to

I need help getting this python code to produce the output I provide in an image below. I've written the code, I just need to fix it and get it to work. The program is suppose to read a .csv file:(https://docs.google.com/spreadsheets/d/1buksMCfdH9FQnwSYEJ6IFOjTRuZCGFIf2SE23U8xBQY/edit?usp=sharing)

instructions are below:

In this assignment you will use lists. As you loop through the input file, in load_data() instead of printing to the output file, you will load each value into a list. You will have lists for the age category (function you created in the last assignment), gender, blood pressure, cholesterol and class for each patient. You will need to add two functions as described below and call them where appropriate.

count_distinct(): takes a list as a parameter and writes the distinct categories in the list along with their counts to the output file. This function produces the healthy and sick count, the male and female count and the high, medium and low counts shown in the output file in Figure 1 with three separate function calls with the respective list (class, gender, age category).

listavg_by_category(): takes the blood pressure or cholesterol list and another list of categories (gender, age category or class) and writes the average blood pressure or cholesterol for each category to the output file. This function produces the average blood pressure by class and average cholesterol by class, the average blood pressure by gender and average cholesterol by gender, and the average blood pressure by age category and the average cholesterol by age category in Figure 1 with six separate function calls with the respective lists (blood pressure/cholesterol and class/gender/age category).

and produce this output in the output6b-09.txt file:

image text in transcribed

The error6b-09.txt file, after the program runs, should look like this:

9 class not valid 17 class not valid 32 age is non-numeric 35 bp is non-numeric 45 chol is non-numeric

All the logic is there, I just think I'm having some appending and list issues. The code can't contain any functions that aren't already seen in the code - no dictionaries either. I have to stick to the basics. Here is the code:

import sys

error = ('error6b-09.txt', 'w')

correct = ('output6b-09.txt', 'w')

gen_lst = []

age_lst = []

bp_lst = []

chol_lst = []

def age_category(age):

#age category function

if (age

return'low'

#if age is less than 45 return low

elif (age >= 45 and age

return 'med'

#if age is greater than or equal to 45 and less than 60 return med

elif (age >= 60):

return 'high'

def count_distinct(lst):

count = []

distinct = []

for items in lst:

if items not in distinct:

distinct.append(items)

count.append(1)

else:

count[distinct.index(items)] = count[distint.index(items)] + 1

for items in lst:

output.write(items + 'Count: ' + str(count[distinct.index(items)] + ' '))

def listavg_by_category(bp_or_chol, lst):

count = []

distinct = []

sum = []

counter = 0

for items in lst:

if items not in distinct:

distinct.append(items)

count.append(1)

sum.append(bp_or_chol[counter])

else:

count[distinct.indx(items)] = count[distinct.index(items)] + 1

sum[distinct.index(items)] = sum[distinct.index(items)] + bp_or_chol[counter]

counter = counter + 1

for item in distinct:

avg = float(sum[distinct.index(items)])/float(count[distinct.index(items)])

print(avg)

def load_data(file):

data = open(file)

#data = data.readline()

for row in data:

if row.startswith('patientID'):

continue

patID = row[:row.find(',')]

row = row[row.find(',')+1:]

age = row[:row.find(',')]

try:

age = int(age)

age_lst.append(age_category(age))

except:

print(str(patID) + ' age is non-numeric' + ' ')

row = row[row.find(',')+1:]

gen = row[:row.find(',')]

gen_lst.append(gen)

#correct.write(str(gen) + ' ')

row = row[row.find(',')+1:]

bp_l = row[:row.find(',')]

try:

bp_l = int(bp_l)

bp_lst.append(bp_l)

except:

error.write(str(patID) + ' bp is non-numeric' + ' ')

row = row[row.find(',')+1:]

ch = row[:row.find(',')]

try:

ch = int(ch)

chol_lst.append(ch)

except:

error.write(str(patID) + ' chol is non-numeric' + ' ')

row = row[row.find(',')+1:]

row = row[row.find(',')+1:]

cls = row.rstrip()

if cls != 'Healthy' and cls != 'Sick':

error.write(str(patID) + ' class not valid' + ' ')

cls_list.append(cls)

file = sys.argv[1]

load_data(file)

count_distinct(age_lst)

count_distinct(gen_lst)

count_distinct(cls_lst)

listavg_by_category(bp_lst, age_lst)

Figure 1:_output.txt output.txt Notepad Produced by six calls to File Edit Format iew Help Blood Pressure and Cholesterol by Class Sick Count: 137 Healthy Count: 161 Blood Pressure by CiaSS Sick Average: 134.5036496350365 Healthy Average: 129.0062111801242 istavg by c ate Produced by three calls to count distinct ) Sick Average: 251.62773722627736 ealthy Average: 242.8757763975155 Blood Pressure and Cholesterol by Gender Male Count: 202 Female Count: 96 Male Average: 130.7970297029703 Female Average: 133.08333333333334 Cholesterol by Gender Male Average: 240.05445544554456 Female Average: 261.3020833333333 Blood Pressure and Cholesterol by Age Group high Count: 88 med Count: 155 low Count: 55 Blood Pressure by Age Group high Average: 136.0568181818182 med Average: 131.54193548387096 low Average: 124.27272727272727 Cholesterol by Age Group high Average: 258.82954545454544 med Average: 246.84516129032258 low Average: 227.96363636363637 Figure 1:_output.txt output.txt Notepad Produced by six calls to File Edit Format iew Help Blood Pressure and Cholesterol by Class Sick Count: 137 Healthy Count: 161 Blood Pressure by CiaSS Sick Average: 134.5036496350365 Healthy Average: 129.0062111801242 istavg by c ate Produced by three calls to count distinct ) Sick Average: 251.62773722627736 ealthy Average: 242.8757763975155 Blood Pressure and Cholesterol by Gender Male Count: 202 Female Count: 96 Male Average: 130.7970297029703 Female Average: 133.08333333333334 Cholesterol by Gender Male Average: 240.05445544554456 Female Average: 261.3020833333333 Blood Pressure and Cholesterol by Age Group high Count: 88 med Count: 155 low Count: 55 Blood Pressure by Age Group high Average: 136.0568181818182 med Average: 131.54193548387096 low Average: 124.27272727272727 Cholesterol by Age Group high Average: 258.82954545454544 med Average: 246.84516129032258 low Average: 227.96363636363637

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

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago