Python Forum

Full Version: Issue from RandomizedSearchCV
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to find best n_estimators for random forest, when running following script, got error message:
TypeError: 'ABCMeta' object is not subscriptable

Please help solve the problem, thank you!

Here is the code:
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
from sklearn.pipeline import Pipeline
# from abc import ABC
# from collections.abc import Mapping

parameter_grid={'n_estimators':[1,2,3,4,5],'max_depth':[2,4,6,8,10],'min_samples_leaf': 
[1,2,4],'max_features':[1,2,3,4,5,6,7,8]}

number_models=4
random_RandomForest_class=RandomizedSearchCV(
estimator=Pipeline['clf'],
param_distributions=parameter_grid,
n_iter=number_models,
scoring='accuracy',
n_jobs=2,
cv=4,
refit=True,
return_train_score=True)
# Error message was generated after this block of code

random_RandomForest_class.fit(X_train,y_train)
predictions=random_RandomForest_class.predict(X)

print("Accuracy Score",accuracy_score(y,predictions));
print("Best params",random_RandomForest_class.best_params_)
print("Best score",random_RandomForest_class.best_score_)
You should post the entire error trace.

I think this is your problem.
estimator=Pipeline['clf'],
Maybe should be this?
estimator=Pipeline(['clf']),
(Aug-16-2023, 08:15 PM)deanhystad Wrote: [ -> ]You should post the entire error trace.

I think this is your problem.
estimator=Pipeline['clf'],
Maybe should be this?
estimator=Pipeline(['clf']),

The code is from here, under Answer 4 (4 Answer), all was posted above, no extra.
https://stackoverflow.com/questions/6076...classifier
The problem in the stack overflow post is we have no idea what "pipeline" is. It appears to be a dictionary of Pipeline objects. One thing it is not is the class Pipeline. We'll never know as the link pointing to the code is not valid.