Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessons
Curves and trees · ML-R10 · 12–18 MIN

Control tree complexity

Use depth and minimum leaf size with validation evidence.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferCompute and inspect the evidence

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

Illustrative data
ColumnValues
x0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
target2, 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. Each leaf predicts its fitting-target mean. More depth permits finer partitions. This training fit does not establish better validation performance.Fitted predictions05101501020max_depth=5: 16 leaves

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 with X_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
  1. The requested result, with the values, labels and row order described by this task.
STEP60 · first 8 rows
xy
010.4571
0.1694928.44002
0.33898311.1257
0.50847511.4108
0.6779667.07345
0.8474588.04673
1.0169510.1918
1.186449.52564
Your task
  1. Search tree max_depth values 3, 5 and None using the supplied five training folds and negative RMSE.
  2. Store positive mean validation RMSE in answer, in that candidate order.
answer
Hints

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.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.