Question

In: Computer Science

Write a python program for the following question. Complete the symptom similarity function, which measures the...

Write a python program for the following question.

Complete the symptom similarity function, which measures the similarity between the symptoms of two patients. See below for an explanation of how the similarity is computed.

def symptom_similarity(symptoms_A: Tuple[Set], symptoms_B: Tuple[Set]) -> int:

'''Returns the similarity between symptoms_A and symptoms_B.
symptoms_A and symptoms_B are tuples of a set of symptoms present and a set of symptoms absent.
The similarity measure is computed by the following equations:
present_present + absent_absent - present_absent - absent_present
where:

present_present is the number of symptoms present in both patients

absent_absent is the number of symptoms absent in both patients

present_absent is the number of symptoms present in patientA and absent in patientB

absent_present is the number of symptoms absent in patientA and present in patientB.

>>>symptom_similarity(yang, maria)
1

#yang = ({"coughing", "runny_nose", "sneezing"},{"headache","fever"})
#maria = ({"coughing", "fever", "sore_throat", "sneezing"},{"muscle_pain"}
# As you can see the common part in these two tuple is "coughing' so similarity is 1.
''''

Question 2: similarity to patients (18 points)
Complete the similarity to patients function, which uses the function symptom similarity to select
the patients (from the entire population of patients in our database) with the highest similarity between
the their symptoms and the symptoms of one patient. See below for an explanation of exactly what is
expected:

def similarity_to_patients(my_symptoms : Tuple[Set], all_patients: Dict[str, Tuple[Set]]) ->
Set[int]:
"""
returns a set of the patient IDs with the highest similarity between my_symptoms
(which is a tuple of symptoms present and absent) and the symptoms of
all the patients stored in the dictionary all_patients.
>>> similarity_to_patients(yang, all_patients_symptoms)
{45437, 73454}
#PATIENTS ARE ASSIGNED ID and those ID stores their symptoms. So we need to compare "yang's" symptoms to other patients symptoms in the database.
"""

I will drop a like for even attempting to give a fruitful solution.

Solutions

Expert Solution

# A part
def symptom_similarity(symptoms_A, symptoms_B):
    present_A, absent_A = symptoms_A 
    # Here present_A and absent_A represent symptoms present and 
    # absent in symptoms_A respectively.
    present_B, absent_B = symptoms_B
    # Here present_B and absent_B represent symptoms present and
    # absent in symptoms_B respectively.
    
    present_present = len(present_A & present_B)
    # Here present_present is the number of symptoms 
    # which are present in both A and B patients
    
    absent_absent = len(absent_A & absent_B)
    # Here absent_absent is the number of symptoms 
    # which are absent in both A and B patients
    
    present_absent = len(present_A & absent_B)
    # Here present_absent is the number of symptoms 
    # which are present in A patient and absent in B patient.
    
    
    absent_present = len(absent_A & present_B)
    # Here absent_present is the number of symptoms 
    # which are absent in A and present in B patient.
    
    similarity = present_present + absent_absent - present_absent - absent_present
    # This is the given formula for calculating symptom similarity
    
    return similarity

# B part
def similarity_to_patients(my_symptoms, all_patients):
    similarity = set()
    # This similarity set will contain all those IDs of patients which has
    # maximum similarity with my_symptoms.
    
    maxi = 0
    # Initializing a maxi varibale for keep tracking of maximum similarity.
    
    # Looping through all patients inorder to get the similarity.
    for patient_id in all_patients:
        patient = all_patients[patient_id]
        
        # Finding similarity between my_symptoms and each patient.
        similarity_with_patient = symptom_similarity(my_symptoms, patient)
        
        # If similarity_with_patient is more than maxi, then it means we 
        # current patient has more simlarity then the earlier ones.
        # So we will update the maxi and also update the similarity set.
        if maxi < similarity_with_patient:
            maxi = similarity_with_patient
            similarity = set()
            similarity.add(patient_id)
            
        # If similarity_with_patient is equal to maxi than we have to add its id to
        # similarity set, because we want all the patients with maximum similarity.
        if maxi == similarity_with_patient:
            similarity.add(patient_id)
        
    return similarity

# For Example - 

# Let our two patients be yang and maria.
# A tuple of symptoms(present and absent both) assigned to yang and maria.

yang = ({"coughing", "runny_nose", "sneezing"},{"headache","fever"})
maria = ({"coughing", "fever", "sore_throat", "sneezing"},{"muscle_pain"})

similarity = symptom_similarity(yang, maria)
# Here we get the symptoms similarity between yang and maria in similarity varibale.

print("The Symptom Similarity is", similarity)

# Let all patients symptoms be this - 
all_patients_symptoms = {56374: ({"headache","fever"}, 
                                 {"coughing", "runny_nose","sneezing"}),
                           45437: ({"coughing", "runny_nose"},
                                   {"headache","fever"}),
                           16372: ({"coughing", "sore_throat"},
                                   {"fever"}),
                           54324: ({"vomiting", "coughing","stomach_pain"},
                                   {"fever"}),
                           35249: ({"sore_throat", "coughing","fever"},
                                   {"stomach_pain", "runny_nose"}),
                           44274: ({"fever", "headache"},
                                   {"stomach_pain", "runny_nose","sore_throat", "coughing",}), 
                           74821: ({"vomiting", "fever"},
                                   {"headache"}),
                           94231: ({"stomach_pain", "fever","sore_throat",
                                    "coughing","headache"},
                                   {"vomiting"}),
                           }


similarity = similarity_to_patients(yang, all_patients_symptoms)
# Getting set of IDs of patients of heighest similarity between yang and all patients.

print("The set of highest Symptom Similarity between yang and all patients is", similarity)

This is the running code.
If you find this helpful please upvote.

I've also added B part now.

Let me know in comments if you get stuck somewhere.

Updated at 9:50 pm UTC,16 October 2020


Related Solutions

Question 3: getting diagnostics, Write a python program   Write the getting diagnostics function, which reports the...
Question 3: getting diagnostics, Write a python program   Write the getting diagnostics function, which reports the most frequent diagnostic from the patients with the highest symptoms similarity returned by the function similarity to patients. See below for an explanation of exactly what is expected. def getting_diagnostics(patient_set : Set[int], diagnostic_by_patient: Dict[int, str]) -> str: """ Returns a string representing the most frequent diagnostic from the set of diagnostics (stored in the dictionary diagnostic_by_patient) of the patient_set (that is a set of...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:...
Write a complete Python program that asks the user for a line of text (not just...
Write a complete Python program that asks the user for a line of text (not just a single word), and then tells the user whether the string is a palindrome (same forward and backward) if you ignore case and non-alphabetic characters. The following methods and functions may be useful: str.upper(), str.isalpha() -> bool, reversed(str) -> sequence, str.find(str) -> int, str.replace(str, str), str.center(int). Unless otherwise specified, they all return a string.
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT