جواب «Depth vs Overfitting» چالش 5 از جلسهی 51
سؤال
حلقه را خودت بنویس: برای max_depth از ۱ تا ۵ درختی روی ۴۰ دانشآموز آموزش (با min_samples برابر ۲) بساز و دقت آموزش و آزمون و فاصله را چاپ کن. خروجی: depth 1: train 75% test 78% gap -3، depth 2: train 80% test 80% gap 0، depth 3: train 90% test 85% gap 5، depth 4: train 95% test 75% gap 20 و depth 5: train 100% test 72% gap 28.
راهنما
for depth in range(1, 6): و tree = build(rows, labels, 0, depth, 2)؛ آزمون با trows, tlabels.
یک جواب درست
import random
names = ["study_hours", "sleep_hours", "seat_row"]
def make_students(n):
rows, labels = [], []
for i in range(n):
study = random.randint(0, 10)
sleep = random.randint(4, 10)
seat = random.randint(1, 6)
passed = study + sleep // 2 >= 8
if random.random() < 0.15: # life is noisy: 15% surprises
passed = not passed
rows.append([study, sleep, seat])
labels.append("pass" if passed else "fail")
return rows, labels
random.seed(4)
rows, labels = make_students(40) # train
trows, tlabels = make_students(40) # test
def gini(group):
if len(group) == 0:
return 0.0
score = 1.0
for c in sorted(set(group)):
p = group.count(c) / len(group)
score -= p * p
return score
def split(rows, labels, f, t):
left = [labels[i] for i in range(len(rows)) if rows[i][f] < t]
right = [labels[i] for i in range(len(rows)) if rows[i][f] >= t]
return left, right
def weighted_gini(left, right):
n = len(left) + len(right)
return len(left) / n * gini(left) + len(right) / n * gini(right)
def best_split(rows, labels):
best = None
for f in range(len(rows[0])):
for t in sorted(set(row[f] for row in rows))[1:]:
left, right = split(rows, labels, f, t)
score = weighted_gini(left, right)
if best is None or score < best[0]:
best = (score, f, t)
return best
def majority(labels):
return max(sorted(set(labels)), key=labels.count)
def build(rows, labels, depth, max_depth, min_samples):
if depth == max_depth or len(rows) < min_samples or gini(labels) == 0:
return {"leaf": majority(labels)}
best = best_split(rows, labels)
if best is None:
return {"leaf": majority(labels)}
score, f, t = best
left_rows, left_labels, right_rows, right_labels = [], [], [], []
for i in range(len(rows)):
if rows[i][f] < t:
left_rows.append(rows[i])
left_labels.append(labels[i])
else:
right_rows.append(rows[i])
right_labels.append(labels[i])
left = build(left_rows, left_labels, depth + 1, max_depth, min_samples)
right = build(right_rows, right_labels, depth + 1, max_depth, min_samples)
if "leaf" in left and "leaf" in right and left["leaf"] == right["leaf"]:
return left # both sides say the same thing: the question is useless
return {"feature": f, "threshold": t, "left": left, "right": right}
def predict(node, row):
while "leaf" not in node:
if row[node["feature"]] < node["threshold"]:
node = node["left"]
else:
node = node["right"]
return node["leaf"]
def accuracy(tree, rows, labels):
right = sum(1 for i in range(len(rows)) if predict(tree, rows[i]) == labels[i])
return round(right / len(rows) * 100)
for depth in range(1, 6):
tree = build(rows, labels, 0, depth, 2)
train = accuracy(tree, rows, labels)
test = accuracy(tree, trows, tlabels)
print(f"depth {depth}: train {train}% test {test}% gap {train - test}")
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن