This solution isn't very "Pythonic", but it's easy to follow. You could just call the function in a loop or nested loop or something similar.
dt = DecisionTreeClassifier(criterion='entropy', min_samples_leaf=150, min_samples_split=100)
Is the standard call to use a decision tree, just loop over the values you want to use and replace min_samples_leaf and min_samples_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, roc_curve, auc
from sklearn.model_selection import train_test_split
min_samples_leafs = [50, 100, 150]
min_samples_splits =[50, 100, 150]
for sample_leafs in min_samples_leafs:
for sample_splits in min_samples_splits:
dt = DecisionTreeClassifier(criterion='entropy', min_samples_leaf=sample_leafs, min_samples_split=sample_splits)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
dt = dt.fit(X_train, y_train)
y_pred_train = dt.predict(X_train)
y_pred_test = dt.predict(X_test)
print("Training Accuracy: %.5f" %accuracy_score(y_train, y_pred_train))
print("Test Accuracy: %.5f" %accuracy_score(y_test, y_pred_test))
print('sample_leafs: ', sample_leafs)
print('sample_leafs: ', sample_splits)
print('\n')
Output:
Training Accuracy: 0.96689
Test Accuracy: 0.96348
sample_leafs: 50
sample_leafs: 50
Training Accuracy: 0.96689
Test Accuracy: 0.96348
sample_leafs: 50
sample_leafs: 100
Training Accuracy: 0.96509
Test Accuracy: 0.96293
sample_leafs: 50
sample_leafs: 150
Training Accuracy: 0.96313
Test Accuracy: 0.96256
sample_leafs: 100
sample_leafs: 50
Training Accuracy: 0.96313
Test Accuracy: 0.96256
sample_leafs: 100
sample_leafs: 100
Training Accuracy: 0.96313
Test Accuracy: 0.96256
sample_leafs: 100
sample_leafs: 150
Training Accuracy: 0.96188
Test Accuracy: 0.96037
sample_leafs: 150
sample_leafs: 50
Training Accuracy: 0.96188
Test Accuracy: 0.96037
sample_leafs: 150
sample_leafs: 100
Training Accuracy: 0.96188
Test Accuracy: 0.96037
sample_leafs: 150
sample_leafs: 150
You can make this a function by passing the lists like so
def do_decision_tree_stuff(min_samples_leafs, min_samples_splits):
You call the function like this
do_decision_tree_stuff([50, 100, 150], [50, 100, 150])