def test_id3_ut10(self, tn=10):
     """
     Tests information gain of Temperature in the examples and target_attribute PlayTennis.
     """        
     print('\n============ ID3 UT {} ======================='.format(tn))
     examples, colnames = bin_id3.parse_csv_file_into_examples('play_tennis.csv')
     avt = bin_id3.construct_attrib_values_from_examples(examples, colnames[1:])
     g   = bin_id3.gain(examples, 'PlayTennis', 'Temperature', avt)
     gt  = 0.029222565658954647
     err = 0.0001
     print('gain(Temperature)={}'.format(g))
     assert abs(g - gt) <= err
     print('\n============ ID3 UT {} passed ================'.format(tn))
 def test_id3_ut19(self, tn=19):
     """
     Let S = 
     1) {'Day': 'D1',  'Outlook': 'Sunny', 'Temperature': 'Hot', 'Humidity': 'High', 'Wind': 'Weak', 'PlayTennis': 'No'}
     2) {'Day': 'D2',  'Outlook': 'Sunny', 'Temperature': 'Hot', 'Humidity': 'High', 'Wind': 'Strong', 'PlayTennis': 'No'}
     3) {'Day': 'D8',  'Outlook': 'Sunny', 'Temperature': 'Mild', 'Humidity': 'High', 'Wind': 'Weak', 'PlayTennis': 'No'}
     4) {'Day': 'D9',  'Outlook': 'Sunny', 'Temperature': 'Cool', 'Humidity': 'Normal', 'Wind': 'Weak', 'PlayTennis': 'Yes'}
     5) {'Day': 'D11', 'Outlook': 'Sunny', 'Temperature': 'Mild', 'Humidity': 'Normal', 'Wind': 'Strong', 'PlayTennis': 'Yes'}
     Compute 
     1) Entropy(S);
     2) Gain(Temperature);
     3) Gain(Humidity);
     4) Gain(Wind);
     """
     print('\n============ ID3 UT {} ======================='.format(tn))
     examples, colnames = bin_id3.parse_csv_file_into_examples('play_tennis.csv')
     outlook_sunny_examples = bin_id3.find_examples_given_attrib_val(examples, 'Outlook', 'Sunny')
     avt = bin_id3.construct_attrib_values_from_examples(examples, colnames[1:])
     H   = bin_id3.entropy(outlook_sunny_examples, 'PlayTennis', avt)
     gH  = 0.9709505944546686
     err = 0.0001
     print('Entropy(S)={}'.format(H))
     assert abs(H - gH) <= err
     g   = bin_id3.gain(outlook_sunny_examples, 'PlayTennis', 'Temperature', avt)
     gt  = 0.5709505944546686
     err = 0.0001
     print('gain(Temperature)={}'.format(g))
     assert abs(g-gt) <= err
     g   = bin_id3.gain(outlook_sunny_examples, 'PlayTennis', 'Humidity', avt)
     gt  = 0.9709505944546686
     err = 0.0001
     print('gain(Humidity)={}'.format(g))
     assert abs(g-gt) <= err
     g   = bin_id3.gain(outlook_sunny_examples, 'PlayTennis', 'Wind', avt)
     gt  = 0.01997309402197489
     err = 0.0001
     print('gain(Wind)={}'.format(g))
     assert abs(g-gt) <= err
     print('\n============ ID3 UT {} passed ================'.format(tn))
 def test_id3_ut09(self, tn=9):
     """
     Tests information gain of Outlook in the examples and target_attribute PlayTennis.
     """        
     print('\n============ ID3 UT {} ======================='.format(tn))
     examples, colnames = bin_id3.parse_csv_file_into_examples('play_tennis.csv')
     avt = bin_id3.construct_attrib_values_from_examples(examples, colnames[1:])
     g   = bin_id3.gain(examples, 'PlayTennis', 'Outlook', avt)
     gt  = 0.2467498197744391
     err = 0.0001
     print('gain(Outlook)={}'.format(g))
     assert abs(g - gt) <= err
     print('\n============ ID3 UT {} passed ================'.format(tn))
 def test_id3_ut08(self, tn=8):
     """
     Tests information gain of Humidity in the examples and target_attribute PlayTennis.
     """        
     print('\n============ ID3 UT {} ======================='.format(tn))
     examples, colnames = bin_id3.parse_csv_file_into_examples('play_tennis.csv')
     avt = bin_id3.construct_attrib_values_from_examples(examples, colnames[1:])
     g   = bin_id3.gain(examples, 'PlayTennis', 'Humidity', avt)
     gt  = 0.15183550136234136
     err = 0.0001
     print('gain(Humidity)={}'.format(g))
     assert abs(g - gt) <= err
     print('\n============ ID3 UT {} passed ================'.format(tn))
 def test_id3_ut17(self, tn=17):
     """
     Find all examples with Outlook=Overcast and compute info gain of Wind.
     """
     print('\n============ ID3 UT {} ======================='.format(tn))
     examples, colnames = bin_id3.parse_csv_file_into_examples('play_tennis.csv')
     outlook_over_examples = bin_id3.find_examples_given_attrib_val(examples, 'Outlook', 'Overcast')
     avt = bin_id3.construct_attrib_values_from_examples(examples, colnames[1:])        
     g   = bin_id3.gain(outlook_over_examples, 'PlayTennis', 'Wind', avt)
     gt  = 0.0
     err = 0.0001
     print('gain(Wind)={}'.format(g))
     assert abs(g - gt) <= err
     print('\n============ ID3 UT {} passed ================'.format(tn))
 def test_id3_ut12(self, tn=12):
     """
     Find all examples with Outlook=Sunny and compute info gain of Temperature.
     """
     print('\n============ ID3 UT {} ======================='.format(tn))
     examples, colnames = bin_id3.parse_csv_file_into_examples('play_tennis.csv')
     outlook_sunny_examples = bin_id3.find_examples_given_attrib_val(examples, 'Outlook', 'Sunny')
     avt = bin_id3.construct_attrib_values_from_examples(examples, colnames[1:])
     g   = bin_id3.gain(outlook_sunny_examples, 'PlayTennis', 'Temperature', avt)
     gt  = 0.5709505944546686
     err = 0.0001
     print('gain(Temperature)={}'.format(g))
     assert abs(g - gt) <= err
     print('\n============ ID3 UT {} passed ================'.format(tn))
 def test_id3_ut07(self, tn=7):
     """
     Computes information gain of Wind in the examples and target_attribute PlayTennis.
     """
     print('\n============ ID3 UT {} ======================='.format(tn))
     examples, colnames = bin_id3.parse_csv_file_into_examples('play_tennis.csv')
     avt = bin_id3.construct_attrib_values_from_examples(examples, colnames[1:])
     # print('bin_id3: avt={}'.format(avt))
     g   = bin_id3.gain(examples, 'PlayTennis', 'Wind', avt)
     gt  = 0.04812703040826927
     err = 0.0001
     print('gain(Wind)={}'.format(g))
     assert abs(g - gt) <= err
     print('\n============ ID3 UT {} passed ================'.format(tn))