From a 14-row PlayTennis table, compute information gain for the outlook and wind attributes and print the chosen root split.
From a 14-row PlayTennis table, compute information gain for the outlook and wind attributes and print the chosen root split.
Answer
import math # rows: (outlook, wind, play) data = [ ('sunny', 'weak', 'no'), ('sunny', 'strong', 'no'), ('overcast', 'weak', 'yes'), ('rainy', 'weak', 'yes'), ('rainy', 'weak', 'yes'), ('rainy', 'strong', 'no'), ('overcast', 'strong', 'yes'), ('sunny', 'weak', 'no'), ('sunny', 'weak', 'yes'), ('rainy', 'weak', 'yes'), ('sunny', 'strong', 'yes'), ('overcast', 'strong', 'yes'), ('overcast', 'weak', 'yes'), ('rainy', 'strong', 'no'), ] def entropy(rows): n = len(rows) if n == 0: return 0.0 counts = {} for r in rows: counts[r[-1]] = counts.get(r[-1], 0) + 1 return -sum((c / n) * math.log2(c / n) for c in counts.values()) def info_gain(rows, attr): n = len(rows) groups = {} for r in rows: groups.setdefault(r[attr], []).append(r) rem = sum((len(g) / n) * entropy(g) for g in groups.values()) return entropy(rows) - rem g_out = info_gain(data, 0) g_wind = info_gain(data, 1) best = 'outlook' if g_out >= g_wind else 'wind' print(f'gain(outlook)={g_out:.3f} gain(wind)={g_wind:.3f} -> split on {best}')