In: Computer Science
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.
# 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