SHARE
TWEET

Untitled

a guest May 23rd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def train_classifier(training_records):
  2.     """ Return a dict containing the midpoint between averages
  3.        among each class (malignant and benign) of each attribute.
  4.        (See the A5 writeup for a more complete description)
  5.        Precondition: training_records is a list of patient record
  6.                      dictionaries, each of which has the keys
  7.                      in the global variable ATTRS
  8.        Postcondition: the returned dict has midpoint values calculated
  9.                       from the training set for all 10 attributes except
  10.                       "ID" and"class".
  11.    """
  12.     malignant_tumors = {}
  13.     benign_tumors = {}
  14.     tumor_avgs = {}
  15.     malignant_count = 0
  16.     benign_count = 0
  17.    
  18.     # Fill dictionaries with the correct attributes
  19.     for attribute in ATTRS[1:-1]:
  20.         malignant_tumors.update({attribute: 0.0})
  21.         benign_tumors.update({attribute: 0.0})
  22.         tumor_avgs.update({attribute: 0.0})
  23.  
  24.     # Fill malignant_tumors and benign_tumors dictionaries with the sums of all
  25.     # corresponding keys from training_records with the class 'M' or 'B'
  26.     for i in range(len(training_records)):
  27.         if training_records[i]['class'] == 'M':
  28.             for attribute in ATTRS[1:-1]:
  29.                 malignant_tumors[attribute] += training_records[i][attribute]
  30.             malignant_count += 1
  31.         else:
  32.             for attribute in ATTRS[1:-1]:
  33.                 benign_tumors[attribute] += training_records[i][attribute]
  34.             benign_count += 1
  35.    
  36.     # Find the averages for all keys in malignant_tumors and benign_tumors,
  37.     # then fill tumor_avgs with the average of all keys in malignant_tumors and
  38.     # benign tumors.
  39.     for attribute in ATTRS[1:-1]:
  40.         malignant_tumors[attribute] /= malignant_count
  41.         benign_tumors[attribute] /= benign_count
  42.         tumor_avgs[attribute] = (
  43.             (malignant_tumors[attribute] + benign_tumors[attribute]) * 0.5
  44.             )
  45.        
  46.     return tumor_avgs
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top