Home > Net >  sklearn DecisionTreeClassifier loop for max_depth
sklearn DecisionTreeClassifier loop for max_depth

Time:07-10

I am currently running a basic scikit-learn decision tree using the below code:

tree = tree.DecisionTreeClassifier(max_depth=10)

tree = tree.fit(X_train, y_train)

Is there a way to test multiple values of max_depth (e.g. between 10 and 15) without doing this individually, i.e. with a loop? I am not sure how to implement this.

CodePudding user response:

Perhaps you’d find this useful: GridSearch. Other than GridSearch, they also have RandomSearch. I believe there are also different libraries that do hyperparameters optimisation.

Basically what they do is you give them your model and a set of hyperparameters to test, and they give back the results.

You’d do it by

    params = { max_depth : [10, 11, 12, 13, 14, 15]}
    gs = GridSearchCV(tree, params)

    gs.fit(X_train, y_train)

Then you can see the results in gs.cv_results_

CodePudding user response:

for depth in [5, 10, 15, 20, 50]:
    tree = DecisionTreeClassifier(max_depth=depth)

    tree.fit(X_train, y_train)
    print(f'Depth: {depth}. Score: {tree.score(X_train, y_train)}')
  • Related