Overfitting in tree depth selection
Review this Python model-evaluation routine.
It always selects max_depth=None with accuracy 1.0. Is that the best model?
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 16 min
Mark a line and say what kind of problem it is.0 findings
1from sklearn.tree import DecisionTreeClassifier
2from sklearn.metrics import r2_score, accuracy_score
3
4def pick_best_depth(X, y, depths=(2, 5, 10, None)):
5 best, best_score = None, -1
6 for d in depths:
7 model = DecisionTreeClassifier(max_depth=d, random_state=0)
8 model.fit(X, y)
9 score = accuracy_score(y, model.predict(X)) # how good is it?
10 print(d, score)
11 if score > best_score:
12 best, best_score = d, score
13 print("best depth:", best)
14 return best
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.