Understand the idea
A deep tree can memorise small groups. Depth and minimum leaf size constrain flexibility. Impurity importance describes this fitted model, not causal importance.
Illustrative data
| Column | Values |
|---|---|
x | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
target | 2, 2.3, 1.7, 2.1, 5, 5.4, 4.7, 5.2, 9, 9.2, 8.7, 9.4, 13, 13.3, 12.8, 13.1 |
Click the highlighted code to change the result.
example_model = DecisionTreeRegressor(max_depth=, random_state=42)
example_model.fit(example_X_train, example_y_train)max_depth=5: 16 leaves.
What the syntax does
'max_depth'- Names the tree parameter that limits the number of splits along a path.
max_depth:3,5,None
Supplied for this practice
These objects are prepared afresh before your editor runs.
X- Feature dataframe for the declared population, preserving row indices.
X = df[['x']] y- Target series, aligned with
X.y = df.y X_train/X_test/y_train/y_test- Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with
X_train. Final-test targets, aligned withX_test.X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) folds- Supplied folds; its definition below specifies the values and role.
folds = KFold(5, shuffle=True, random_state=42)
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
x | y |
|---|---|
| 0 | 10.4571 |
| 0.169492 | 8.44002 |
| 0.338983 | 11.1257 |
| 0.508475 | 11.4108 |
| 0.677966 | 7.07345 |
| 0.847458 | 8.04673 |
| 1.01695 | 10.1918 |
| 1.18644 | 9.52564 |
- Search tree max_depth values 3, 5 and None using the supplied five training
foldsand negative RMSE. - Store positive mean validation RMSE in
answer, in that candidate order.
answerHints
Think: Depth should be chosen from held-out training evidence rather than test error.
Tools: GridSearchCV, max_depth and negative-RMSE scoring.
Approach: Compare the stated depth grid with the supplied folds, then convert mean scores to positive errors.
Explained solution
search=GridSearchCV(DecisionTreeRegressor(random_state=42),{'max_depth':[3,5,None]},cv=folds,scoring='neg_root_mean_squared_error').fit(X_train,y_train)
answer=-search.cv_results_['mean_test_score']
Matching folds makes complexity comparisons fair; sign conversion reports error in the familiar smaller-is-better direction.