0

Trying this code for a decision tree for school project and getting this error:

#Building Decision Tree Classifier

DT_model = DecisionTreeClassifier()

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

cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)

RCV = RandomizedSearchCV(DT_model, param_dist, n_iter=50, scoring='roc_auc', n_jobs=-1, cv=5, random_state=1)


DT = RCV.fit(Train_X_std, y_train).best_estimator_
pred = DT.predict(Test_X_std)
pred_prob = DT.predict_proba(Test_X_std)
Classification_Summary(pred,pred_prob,1)

print('\n\033[1mInterpreting the output of Decision Tree:\n\033[0m')
tree.plot_tree(DT)
plt.show()

I keep getting this error and I can't figure out how to trouble shoot. Thanks in advance. This is the error that I keep getting. I have combed the logs but I can't seem to find it.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/4r/z339ff2521g9hm6t9_krqctw0000gn/T/ipykernel_59544/392456737.py in <module>
     13 
     14 
---> 15 DT = RCV.fit(Train_X_std, y_train).best_estimator_
     16 pred = DT.predict(Test_X_std)
     17 pred_prob = DT.predict_proba(Test_X_std)

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params)
    889                 return results
    890 
--> 891             self._run_search(evaluate_candidates)
    892 
    893             # multimetric is determined here because in the case of a callable

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/model_selection/_search.py in _run_search(self, evaluate_candidates)
   1765         """Search n_iter candidates from param_distributions"""
   1766         evaluate_candidates(
-> 1767             ParameterSampler(
   1768                 self.param_distributions, self.n_iter, random_state=self.random_state
   1769             )

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/model_selection/_search.py in __init__(self, param_distributions, n_iter, random_state)
    264                     dist[key], "rvs"
    265                 ):
--> 266                     raise TypeError(
    267                         "Parameter value is not iterable "
    268                         "or distribution (key={!r}, value={!r})".format(key, dist[key])

TypeError: Parameter value is not iterable or distribution (key='max_features', value=7)
Progman
  • 16,827
  • 6
  • 33
  • 48
  • Print out your `param_dist` object. You will probably see that `randint` returns a single number (I assume that is `random.randint`). You need to make that an iterable or a distribution. – jkr Aug 03 '23 at 18:49

0 Answers0