-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid3
111 lines (54 loc) · 2 KB
/
id3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import math
import pandas as pd
def entropy(data):
"""
Calculate the entropy of a dataset.
Assumes the last column contains the labels.
"""
labels = data.iloc[:, -1]
total = len(labels)
counts = labels.value_counts()
entropy_value = 0
for count in counts:
prob = count / total
entropy_value -= prob * math.log2(prob)
return entropy_value
def info_gain(data, feature):
total_entropy = entropy(data)
values = data[feature].unique()
weighted_entropy = 0
for value in values:
subset = data[data[feature] == value]
weighted_entropy += (len(subset) / len(data)) * entropy(subset)
return total_entropy - weighted_entropy
def id3(data):
labels = data.iloc[:, -1]
if len(labels.unique()) == 1:
return labels.iloc[0]
best_feature = max(data.columns[:-1], key=lambda f: info_gain(data, f))
tree = {best_feature: {}}
for value in data[best_feature].unique():
subset = data[data[best_feature] == value].drop(columns=[best_feature])
tree[best_feature][value] = id3(subset)
return tree
def predict(tree, instance):
if not isinstance(tree, dict):
return tree
feature = next(iter(tree))
value = instance.get(feature)
if value not in tree[feature]:
return "Unknown value for feature '{}': {}".format(feature, value)
return predict(tree[feature][value], instance)
def predict(tree, instance):
if not isinstance(tree, dict):
return tree
feature = next(iter(tree))
value = instance.get(feature)
if value not in tree[feature]:
return "Unknown value for feature '{}': {}".format(feature, value)
return predict(tree[feature][value], instance)
tree = id3(data)
print("Decision Tree:", tree)
new_sample = {'Outlook': 'Sunny', 'Temperature': 'Cool', 'Humidity': 'High', 'Wind': 'Strong'}
prediction = predict(tree, new_sample)
print("Prediction for new sample:", prediction)