Skip to content

Instantly share code, notes, and snippets.

@vineethm1627
Last active November 15, 2020 12:46
Show Gist options
  • Save vineethm1627/b2c77df970dc490c88707f3785e0ab8a to your computer and use it in GitHub Desktop.
Save vineethm1627/b2c77df970dc490c88707f3785e0ab8a to your computer and use it in GitHub Desktop.
# Import necessary modules
from scipy.stats import randint
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import RandomizedSearchCV
# Setup the parameters and distributions to sample from: param_dist
param_dist = {"max_depth": [3, None],
"max_features": randint(1, 9),
"min_samples_leaf": randint(1, 9),
"criterion": ["gini", "entropy"]}
# Instantiate a Decision Tree classifier: tree
tree = DecisionTreeClassifier()
# Instantiate the RandomizedSearchCV object: tree_cv
tree_cv = RandomizedSearchCV(tree, param_distributions = param_dist, cv=5, n_iter = 5)
# Fit it to the data
tree_cv.fit(X, y)
# Print the tuned parameters and score
print("Tuned Decision Tree Parameters: {}".format(tree_cv.best_params_))
print("Best score is {}".format(tree_cv.best_score_))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment