I am working on hyper-tuning random forest classifier with following parameters in random search CV

In [100]: # defining model

Model = RandomForestClassifier(random state=1)

# Parameter grid to pass in RandomSearchCV

param grid =
{ "n_estimators": [200,250,300], "min_samples_leaf": np.arange(1, 4), "max_features": [np.arange(0.3, 0.6, 0.1),'sqrt'],"max_samples": np.arange(0.4, 0.7, 0.1)}

#Calling RandomizedSearchcV

randomized_cv = RandomizedSearchCV(estimator=Model, param     distributions=param grid, n_iter=10, n_jobs = -1, scoring=metrics.make_scorer(metrics.recall_score))

#Fitting parameters in RandomizedSearchcv

randomized cv.fit(X train, y train)
print ("Best parameters are {} with CV score={}:" .format (randomized_cv.best params_,randomized_cv.best_score_))

 File "/Users/thiyaga/opt/anaconda3/lib/python3.9/site- packages/joblib/parallel.py", line 262, in
 return [func(*args, **kwargs)
 File "/Users/thiyaga/opt/anaconda3/lib/python3.9/site-   packages/joblib/parallel.py", line 262, in <listcomp>
 return [func(*args, **kwargs)
 File "/Users/thiyaga/opt/anaconda3/lib/python3.9/site- packages/sklearn/utils/fixes.py", line 222, in
 return self. function(*args, **kwargs)
 File "/Users/thiyaga/opt/anaconda3/lib/python3.9/site- packages/sklearn/ensemble/_forest.py", line 169, in parall
 el build trees
 tree.fit(X, y, sample weight=curr sample weight, check input=False)
 File "/Users/thiyaga/opt/anaconda3/lib/python3.9/site-packages/sklearn/tree/classes.py",line 903, in fit
 File "/Users/thiyaga/opt/anaconda3/lib/python3.9/site-packages/sklearn/tree/_classes.py",line 273, in fit
if self.max features > 0.0:
ValueError: The truth value of an arrav with more than one  element is ambiguous. Use a.an() or a.all()
warnings.warn ("Estimator fit failed. The score on this train-test"

output is

Best parameters are

 {'n estimators': 250,
 'min samples leaf': 1,
 'max samples': 0.6,
 'max features':'sqrt'}

with CV score=0.6996248466921577; along with this warning message. To avoid warning message imported

import warnings
from sklearn.exceptions import FitFailedWarning 

still warning message appears.


1 Answer 1


In the param_grid "max_features": [np.arange(0.3, 0.6, 0.1),'sqrt'], this code essentially means "max_features":[[0.3 0.4 0.5],'sqrt'], which is a nested array, this is ambiguous for RandomSearchCV.

Instead of that, you can expand and write as "max_features": [0.3 0.4 0.5,'sqrt']. This helps RandomSearchCV to look into the array and returns the best max_feature value.

Hope this solves :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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