Background: Your task in this assignment is to write a program that will help doctors make disease diagnostics based on a patient’s symptom. This will be achieved by comparing a new patient’s symptoms to a database of patients containing both their symptoms and their diagnostic. Given a patient X with a certain set of symptoms, your diagnostic help to the doctor will be obtained by identifying, among the patients in the database, those whose symptoms are most similar to those of patient X. This is actually a commonly used approach in artificial intelligence, called k-‐nearest neighbors classification, although everything you need to know about k-‐nearest neighbors classification is contained in this assignment.
The file contains several functions that you will need to complete. It also contains the my_test() function, which calls each of the functions you will write. The expected (correct) output for several examples is given in the file my_test_output.txt. Use this to make sure that you understand what every function is expected to do and to test your own code. Note: You will not be able to run the my_test() function before you complete all the functions it calls. In order you to test your functions one at a time, comment out the portions of the my_test() function that call functions you have not yet written.
o Suppose you have a list of tuples and you want to sort the list based on the elements at a particular index of the tuple. Example: sort a list of tuples based on the values at index 1 of the tuples.
The List type has a sort() function, but how to tell the sort function what element of the tuple to look at? First, define a small function that takes an as argument a tuple and returns element at index 1 of the tuple:
def getKey1(item): return item[1]
Then, tell the sort() function to use the getKey1() function to select the keys to base the sorting upon. This is done by passing the getKey1 function as a keyword argument to the sort function. (Yes, functions can be passed as arguments to other functions!).
someList.sort(key=getKey1)
To sort in reverse order:
someList.sort(key=getKey1, reverse=True)
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, and see my_test() function for examples.
def symptom_similarity(my_symptoms, other_symptoms): “””
Args:
my_symptoms: tuple of a set of symptoms present and a set of symptoms absent other_symptoms: tuple of a set of symptoms present and set symptoms absent
Returns:
common_present + common_absent – common_present_absent – common_absent_present where common_present is the number of symptoms present in both patients common_absent is the number of symptoms absent in both patients common_present_absent is the number of symptoms present in my_patient and absent in other_patient
common_absent_present is the number of symptoms absent in my_patient and present in other_patient