2

I am using sklearn to train different models. I want to pass the decision tree classifier of sklearn, different values of the same parameter and plot a graph. I want to do this for many such parameters. So, I want to create a general function which can handle all the parameters and their values.

My question is that is there a way to assign the parameter name (not value) to a variable and pass it to my function.

Eg.- Decision tree takes the max_depth, min_samples_leaf etc. arguments. I want to try different values of both parameters one at a time and plot results for both max_depth and min_samples_leaf separately.

2 Answers 2

10

Use a dictionary and pass it with **.

kwargs = {
    "max_depth": value,
    "min_samples_leaf": value,
}
fun(**kwargs)
Sign up to request clarification or add additional context in comments.

4 Comments

The problem is that "param1" is a string and DecisionTreeClassifier(or any function) does not take a string as the parameter. It is equivalent to using DecisionTreeClassifier("param1"=value) but I want DecisionTreeClassifier(param1=value)
But Python turns the string into the parameter name. That's how it works: book.pythontips.com/en/latest/args_and_kwargs.html
After opening kwargs (using kwargs.items()), I store max_depth in a variable names param1 in fun and then I use DecisionTreeClassifier(param1=value). But this gives an error: "param1 is not a valid argument".
Don't, just do DecisionTreeClassifier(**kwargs). That's the Pythonic way of doing what you want.
0

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])

2 Comments

This solves the problem for two variables only but, does not generalize for many variables.
Then pass more lists, or a list of lists, or a dictionary as recommended below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.