I was hoping to hypertune my decisiontree model , however I keep running into this error: TypeError: DecisionTreeClassifier() got an unexpected keyword argument 'criterion'

here what I tried:

def randomsearch():
    data = pd.read_csv('test_data.csv', delimiter=',')
    x = data.iloc[:, :70]
    y = data['class'].values
    param_dist = {"max_depth": [3, None],
              "max_features": randint(1, 9),
              "min_samples_leaf": randint(1, 9),
              "criterion": ["gini", "entropy"]}
    tree = DecisionTreeClassifier()
    tree_cv = RandomizedSearchCV(tree, param_dist, cv=5)
    print("Tuned Decision Tree Parameters: {}".format(tree_cv.best_params_))
    print("Best score is {}".format(tree_cv.best_score_))

I had expected to receive the best parameters for my dataset. This error seems odd any reason why its showing up and how to bypass it?


import pandas as pd
from sklearn.model_selection import KFold
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.metrics import classification_report
from random import randint
import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split , cross_val_score , RandomizedSearchCV,GridSearchCV,RepeatedStratifiedKFold
from sklearn.pipeline import make_pipeline
from sklearn import preprocessing 
from scipy.stats import loguniform
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.model_selection import LeaveOneOut
import pickle
  • $\begingroup$ welcome to the site @John Adams. The code you've pasted looks correct to me. Can you add your imports and anything preceding this code as well as the errors from the stack trace? $\endgroup$ Oct 26, 2023 at 16:19
  • $\begingroup$ Issue is my function was named the same as my import $\endgroup$
    – John Adams
    Oct 27, 2023 at 20:56
  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Oct 29, 2023 at 3:21

1 Answer 1


The 'criterion' parameter looks good, but the problem seems to be the parameters 'max_features" and "min_samples_leaf" because you are using a single integer not an iterable. Try to add the values inside a list like this:

param_dist = {"max_depth": [3, None],
      "max_features": [randint(1, 9)],
      "min_samples_leaf": [randint(1, 9)],
      "criterion": ["gini", "entropy"]}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.